0

I'm using the Drupal Commerce, Checkout redirect and Auto assign role modules.

When a checkout is completed by an anonymous user on a Drupal Commerce website a Drupal account will be created.

If there is only one product everything is ok, but what happen when there are many products?

How can I make it so that:

  • If a user buys "Product 1" I will be auto-assing a role "Role 1"
  • If a user buys "Product 2" I will be auto-assing a role "Role 2"

Could anyone solve this question?

1 Answer 1

0

I needed something similar: user purchased an item (points) and depending on the purchased product a value (points) should be added to the user profile. The rule below is ready to accept (loop through) multiple products/lineItems. In my case I only need the first one:

 "value" : [ "commerce-order:commerce-line-items:0:line-item-id" ],
        "limit" : "1"

First I fetch the purchased products (line items) by property (line_item_id). The result is an array (list of purchased products).

Next you need to loop through this array/list and add the desired action to the individual/current purchased product. If you need a condition (product 1 -> role 1) you can use conditional rules. It is an awesome rules addon that lets you insert IF conditions within loops (so you dont have to create a component that checks the product).

{ "rules_purchasing_points" : {
"LABEL" : "PURCHASE Points",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "commerce_payment" ],
"ON" : { "commerce_payment_order_paid_in_full" : [] },
"DO" : [
  { "entity_query" : {
      "USING" : {
        "type" : "commerce_line_item",
        "property" : "line_item_id",
        "value" : [ "commerce-order:commerce-line-items:0:line-item-id" ],
        "limit" : "1"
      },
      "PROVIDE" : { "entity_fetched" : { "entity_fetched" : "Fetched entity" } }
    }
  },
  { "LOOP" : {
      "USING" : { "list" : [ "entity-fetched" ] },
      "ITEM" : { "list_item" : "Current list item" },
      "DO" : [
        { "data_convert" : {
            "USING" : { "type" : "decimal", "value" : [ "list-item:line-item-label" ] },
            "PROVIDE" : { "conversion_result" : { "conversion_result" : "Conversion result" } }
          }
        },
        { "component_rules_set_points" : {
            "recipient" : [ "site:current-user" ],
            "sender" : "1",
            "points" : [ "conversion-result" ]
          }
        },

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.