upgrade role on successful purchase
How would I upgrade a role on a specific purchase.
am trying to sell memberships to my site.
so maded a product memebership.
now all goes fine there.
but how to automaically upgrade the role to "member"
when they buy that specific product and the purchase is successful?
the checkout rules are very confusing and little documentation there.
any help appreciated.
Comments
I am very interested in this
I am very interested in this as well.
Unfortunately, even though
Unfortunately, even though they're confusing, a custom checkout completion rule is exactly what you need. If you only have the one product for sale in your store, then you can just add a new rule with no conditions that grants the order user the role... that would look something like this, but adjusted for your particular role:
{ "rules_grant_a_user_role_on_checkout" : {
"LABEL" : "Grant a user role on checkout",
"PLUGIN" : "reaction rule",
"REQUIRES" : [ "rules", "commerce_checkout" ],
"ON" : [ "commerce_checkout_complete" ],
"DO" : [
{ "user_add_role" : { "account" : [ "order:owner" ], "roles" : { "value" : { "3" : "3" } } } }
]
}
}
If you have additional products, then you'll need to add the necessary conditions... really we just need to get some decent Rules tutorials online.
We should probably also add a condition to make it easy to see if a particular product is on an order. I'll go open an issue for this now.
This can be accomplished through the default Rules UI
I set up a Rule using Rules UI that assigns a role upon successful checkout. I can confirm that this works when using Drupal Commerce and the example payment checkout method.
Here's how I did it:
I created a new Rule called "Assign [Role Name]"
I added two events: 1. Completing the checkout process and 2. When an order is first paid in full
I added a condition: Order contains a particular product, Parameter: Order: [commerce_order], Product SKU: [productsku]
I added an action: Add user role, Parameter: User: [site:current-user], Roles: [Role Name]
Get an error message following your steps...
I've followed these steps quite a few times, over and over, and the user doesn't receive the role. Here's the error that gets thrown to the user when they complete the checkout:
Notice: Trying to get property of non-object in rules_action_user_add_role() (line 45 of /Applications/MAMP/htdocs/solar7/sites/all/modules/rules/modules/user.eval.inc).
A few things to note: I'm trying to achieve this with anonymous users, purchasing a role on the site. They don't have accounts to begin with, so does this approach work? Also, I'm developing this locally, and I don't have SMTP set up to send new account emails. Would that also effect this?
Thanks mgladding.
Some testing...
OK, I think it's an anonymous user issue.
I've tested this by creating a dummy account, given "authenticated user" role. They are able to purchase the role fine.
Is there any way to reorder, or reconfigure the rules so that when an anonymous user buys the "student" role (in my case), they are first given the "authenticated user" role first? Then it should work, mimicking the test I've done.
Or am I totally overcomplicating this and missing a much simpler technique?
site:current-user probably the wrong user
I'd use commerce-order:owner as the user - the user that the order is for.
Purchasing roles
I'm also very interested in this as I maintain 2 membership sites using ubercards uc_roles functionality. I see that someone posted on how one can create the role upon time of purchase through a rule. But there needs to be another parameter in order to set an expiration.
Perhaps the flow goes something like this:
- Rule to add membership level + Add information to a date field to the user entity called 'membership expiration date'
- Rule to extend said membership when another purchase is made
- Rule to email users about said membership + expiration
- Rule to remove membership (set value to 0) on hook cron when users membership expires
A lot of things todo...
Spoke too soon
Looks like a module is in the works
http://drupal.org/project/commerce_subscription
Also, Role Expire module
This module allows you to set a default amount of days (say 365 for a year subscription) when a new user is created. Unfortunately, it's not all that complete, and one thing missing is that when a role is purchased and applied through Rules, the expiration is not set. If, however, you create the user in the usual fashion, it does get applied.
There is an issue thread dealing with this very situation that I've been keeping an eye on: http://drupal.org/node/1277154.
For now, Role Expire seems more mature than Commerce Subscription and a bit simpler as well. Once that Rules integration is complete, I think we'll have a good solution for selling memberships using Drupal Commerce.
Use what is there
I was able to do this by cloning the "Create a new account for an anonymous order" rule for each membership type.
I added an "Add User Role" action to the action list.
Data Selector: commerce-order:commerce-customer-billing:user
Roles Value: [YOUR-ROLE]
NOTE: I am sure that I could have consolidated the logic into one rule by checking the product being purchased and setting a variable, etc. This was just plain easier and it works ...
my rules version....
This is my rules version....
But, unfortunatelly... this is does not work....
Whrere is my wrong ?
{ "rules_add_member_basic_role" : {
"LABEL" : "Add member basic role",
"PLUGIN" : "reaction rule",
"REQUIRES" : [ "rules", "commerce_payment", "commerce_order", "commerce_checkout" ],
"ON" : [ "commerce_checkout_complete" ],
"IF" : [
{ "data_is" : { "data" : [ "commerce-order:owner:roles:1" ], "value" : "2" } },
{ "commerce_payment_order_balance_comparison" : { "commerce_order" : [ "commerce-order" ], "value" : "0" } },
{ "commerce_order_contains_product" : {
"commerce_order" : [ "commerce_order" ],
"product_id" : "550380211",
"operator" : "\u003E=",
"value" : "1"
}
}
],
"DO" : [
{ "user_add_role" : {
"account" : [ "commerce-order:owner" ],
"roles" : { "value" : { "4" : "4" } }
}
}
]
}
}
Plz help to solve this rules.
Thx in advance
Hello,
Hello,
did you solve the problem?
Best regards
Frank
Try adjusting the weight of the rule.
This had me stumped for a while, but worked fine after I adjusted the weight of the rule to a higher weight (eg. 10). This makes sure that the add user role is evaluated after the other rules that fire to create accounts/orders etc.