What I'm trying to do is to allow people to buy roles on our site. Most of these will be anonymous users who do not have accounts on our site. I've followed the directions from a post here: http://www.drupalcommerce.org/node/381#comment-1624

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]

The user gets an error after they have completed the checkout, and they do not receive the role. Here's the error:

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).

However, if the user DOES have an account, they are given the role just fine. So it's clearly some problem with anonymous users. Either that, or I have not set up my rules correctly. Any ideas?

Comments

rszrama’s picture

Category: task » support
Status: Active » Fixed

Yeah, you cannot give anonymous users roles, and since you're using the [site:current-user] token, that's exactly what your Rule is attempting to do. What you really want is to assign the role to [commerce-order:owner].

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

trevorkjorlien’s picture

I have accomplished what I wanted to do, but I had to get a better understanding of how Rules and Entities work. What I was trying to do before was to add a role to an anonymous user who didn't have an account on the site, which, as I understand now, is impossible. What needs to be done before adding the role to a new user is to create a new entity (i.e., a new user), and then add the role to that new user.

For anybody who is try to do what I outlined above, this is how you pull it off:

1) I cloned the built-in Checkout Rule "Create a new account for an anonymous order".
2) I gave it a new name (in my case "Purchase Individual Role"
3) Under the three existing conditions of the cloned role, I added a new condition:

Order contains a particular product
Parameter: Order: [commerce_order], Product SKU: ROLE-individual

Note: my SKU will differ from yours. Be sure to put in the proper SKU for the role that you are selling.

4) Under the existing actions, I added a new action:

Add user role
Parameter: User: [account-created], Roles: individual

Note: like my SKUs, your role will probably be different. Select the role you are selling.

---

I also found that it didn't work after doing this. What I believe the cause is the weight given to the rules. The original rule that I cloned had a weight of "2", and the cloned rule I outlined above had a weight higher than that. I gave the original rule a higher weight and the role was then applied to the new user.

trevorkjorlien’s picture

Further, I sell more than one role on my site. Right now, I have to set up a different rule for each role that I'd like to sell.

Ideally, it would be great if I could create one rule that detects the SKU/product in the shopping cart, then applies the role to the user based on that SKU.

I imagine it could be done (using Components, or something). It's a bit above my head, so for now I have multiple rules. But if anybody knows how to sell the roles and combine them into one "master" rule to sell and apply the roles that are selected for purchase, that would seem more... "zen" ... to me.

MarketStone’s picture

Thanks for that trevorkjorlien, Your way is better then Randy Fay's http://vimeo.com/channels/commerceguys/page:2 (love Randy Fay's Work!) being right in commerce Checkout Rule is better then a rule out side of commerce. This kinda over rides the commerce subscription module. i will be getting back to what You wrote in the post above, i need this also. The way i see it in the product You need to add role reference field then make the action create role equal to that field. i will post it after i get it.

Thanks for the how-to post,

ressa’s picture

I grant a role after the customer buys a product with this rule. Import and substitute YOUR_SKU_GOES_HERE with your SKU. About the role (user_add_role), number 4 is the id of the role the user is given, it might be different in your situation:

{ "rules_grant_role_if_license_purchased" : {
    "LABEL" : "Grant role if license purchased",
    "PLUGIN" : "reaction rule",
    "WEIGHT" : "6",
    "REQUIRES" : [ "commerce_order", "rules", "commerce_checkout" ],
    "ON" : [ "commerce_checkout_complete" ],
    "IF" : [
      { "commerce_order_contains_product" : {
          "commerce_order" : [ "commerce-order" ],
          "product_id" : "YOUR_SKU_GOES_HERE",
          "operator" : "\u003E=",
          "value" : "1"
        }
      }
    ],
    "DO" : [
      { "user_add_role" : {
          "account" : [ "commerce-order:owner" ],
          "roles" : { "value" : { "4" : "4" } }
        }
      }
    ]
  }
}
leo pitt’s picture

I had the same issue as the original poster - changing the weight of my rule assignment rule to activate after "Create a new account for an anonymous order" worked.

@trevorkjorlien #4

Perhaps this can be achieved by applying a taxonomy term to each product and using that to control which role is applied?

rajeevk’s picture

@leopitt --

I also need this fucntionality.

Though I have 3 roles to apply but it will reduce my effort as I am setting expire date too with different product & that is resulting me with 12 rules set...

Thanks.

rajeevk’s picture

@ressa --

Thanks Man...it simply worked like charm...

jhemsley30’s picture

My question is how does an anonymous user know how to log in to get his access? If the user is being assigned a username based on his email associated with the purchase...... and they don't create a password.....how can they log in to retrieve the membership access content?? Is there a way to place a password field in the checkout process so the user can log in after their purchase?

Also, I keep getting this message: "Notice: Trying to get property of non-object in rules_action_user_add_role()". Although, the role assignment seems to be working properly.

Thanks,
James

rszrama’s picture

Anonymous users who have accounts created for them during the checkout process should receive the new account notification e-mail upon checkout completion.

vivdrupal’s picture

Works with Example Payment method but not with Paypal Sandbox
Adding additional role to logged in user - on successful payment.

My rules export --

{ "rules_custom_role_assign_business_" : {
    "LABEL" : "Custom - role assign  (business)",
    "PLUGIN" : "reaction rule",
    "WEIGHT" : "-4",
    "REQUIRES" : [ "commerce_order", "rules", "commerce_payment" ],
    "ON" : [ "commerce_payment_order_paid_in_full" ],
    "IF" : [
      { "commerce_order_contains_product" : {
          "commerce_order" : [ "commerce_order" ],
          "product_id" : "sp_business",
          "operator" : "=",
          "value" : "1"
        }
      }
    ],
    "DO" : [
      { "user_add_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "225899113" : "225899113" } }
        }
      }
    ]
  }
}
rszrama’s picture

Remember, unless your site is online and can actually receive IPNs from PayPal, the order will never be marked as paid. I'm guessing that's why your rule works fine for the example payment method (it immediately creates the payment transaction) and not for PayPal.

PeterX-dupe’s picture

Status: Closed (fixed) » Active

Hello! my functions are OK, but there is always this error message.
What can I do?

Notice: Trying to get property of non-object in rules_action_user_add_role() (Line 45 off /www/htdocs/XXXXX/XXXX/sites/all/modules/rules/modules/user.eval.inc).

---------------------------------
{ "rules_role1" : {
"LABEL" : "Role1",
"PLUGIN" : "reaction rule",
"WEIGHT" : "4",
"REQUIRES" : [ "rules", "commerce_order", "entity" ],
"ON" : [ "commerce_order_update" ],
"IF" : [
{ "data_is" : { "data" : [ "commerce-order:state" ], "value" : "completed" } },
{ "commerce_order_contains_product" : {
"commerce_order" : [ "commerce_order" ],
"product_id" : "5001 XP",
"operator" : "\u003E=",
"value" : "1"
}
}
],
"DO" : [
{ "user_add_role" : {
"account" : [ "commerce-order:owner" ],
"roles" : { "value" : { "4" : "4" } }
}
}
]
}
}

rszrama’s picture

Status: Active » Closed (fixed)

Sounds to me your order doesn't have an owner yet or something. You may need to explicitly save the order earlier after the user account is created for it. That said, let's not use this unrelated issue to carry that on further; there may even be a bit of a bug report for you to post in Rules.

derekawalker’s picture

Issue summary: View changes

Might want to checkout this module, too: https://drupal.org/project/commerce_license