1

I have 2 products and when user buy it I want award userpoints depend on quantity.

For this task I use specific module (Rules array create and commerce rules extra) it let me create array base on quantity. If user buy on of my product all work correct but when product is mix, all work wrong. My rules double userpoints.

What I want: I have products "A" and "B". Product "A" costs 15$ and product "B" costs 25$. When user buy "A" I want award 20 userpoints, for "B" I want award 30 userpoints also if user increase quantity of product I want award userpoint for each number (example: Ax2=40 userpoints; (Ax2=40 Bx1=30)=70 userpoints ).

In my rules I check condition: I check order for contains sku.

What I have: When user buy one of product my rule work correct. But when user buy both product (A and B) my rules double userpoints and user get double amount (A=20 points, B=30 points amount = 100 instead 50)

How can I avoid this trouble?

This is my rule for first product:

{ "rules_grantpointaftercheckout" : {
"LABEL" : "GrantPointAfterCheckOut",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [
  "commerce_rules_extra",
  "rules_array_create",
  "userpoints_rules",
  "commerce_checkout"
],
"ON" : { "commerce_checkout_complete" : [] },
"IF" : [
  { "commerce_rules_extra_product_sku_contains" : {
      "commerce_order" : [ "commerce_order" ],
      "product_id" : "15_min",
      "operator" : "\u003E=",
      "value" : "1"
    }
  }
],
"DO" : [
  { "array_create" : {
      "USING" : { "size" : [ "commerce-order:commerce-order-quantity" ] },
      "PROVIDE" : { "array_added" : { "array_added" : "Added array" } }
    }
  },
  { "LOOP" : {
      "USING" : { "list" : [ "array-added" ] },
      "ITEM" : { "list_item" : "Current list item" },
      "DO" : [
        { "userpoints_action_grant_points" : {
            "user" : [ "site:current-user" ],
            "points" : "20",
            "tid" : "0",
            "entity" : [ "" ],
            "operation" : "sdadsasad",
            "display" : "1",
            "moderate" : "approved"
          }
        }
      ]
    }
  }
]
 }
 }

This is my rule for second product:

{ "rules_grantpointaftercheckout_cloned_" : {
"LABEL" : "GrantPointAfterCheckOut (cloned)",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [
  "commerce_order",
  "rules_array_create",
  "userpoints_rules",
  "commerce_checkout"
],
"ON" : { "commerce_checkout_complete" : [] },
"IF" : [
  { "commerce_order_contains_product" : {
      "commerce_order" : [ "commerce_order" ],
      "product_id" : "30_min",
      "operator" : "\u003E=",
      "value" : "1"
    }
  }
],
"DO" : [
  { "array_create" : {
      "USING" : { "size" : [ "commerce-order:commerce-order-quantity" ] },
      "PROVIDE" : { "array_added" : { "array_added_2" : "Added array 2" } }
    }
  },
  { "LOOP" : {
      "USING" : { "list" : [ "array-added-2" ] },
      "ITEM" : { "list_item2" : "Current list item 2" },
      "DO" : [
        { "userpoints_action_grant_points" : {
            "user" : [ "site:current-user" ],
            "points" : "30",
            "tid" : "0",
            "entity" : [ "" ],
            "operation" : "sdadsasad",
            "display" : "1",
            "moderate" : "approved"
          }
        }
      ]
    }
  }
]
}
}

This is update, Rules with Conditional Rules module.

Description:

I installed module "Conditional Rules" and make rules with logic like that: If: Order contains a particular product (commerce-order:commerce-line-items:0:order) -> Create an array (commerce-order:commerce-line-items:0:quantity) -> Loop (Created array) -> Grant userpoint. then I add condition again and make second part of rule, but now it check 1 line item. I create 2 rules for both product. If I test it alone all work great, but when I add both product to the cart, userpoints grant only for position avoid quantity.

Rule for A product

{ "rules_a_userpoints" : {
"LABEL" : "A_userpoints",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [
  "rules_conditional",
  "rules_array_create",
  "userpoints_rules",
  "commerce_order",
  "commerce_checkout"
],
"ON" : { "commerce_checkout_complete" : [] },
"DO" : [
  { "CONDITIONAL" : [
      {
        "IF" : { "commerce_order_contains_product" : {
            "commerce_order" : [ "commerce-order:commerce-line-items:0:order" ],
            "product_id" : "A_sku",
            "operator" : "\u003E=",
            "value" : "1"
          }
        },
        "DO" : [
          { "array_create" : {
              "USING" : { "size" : [ "commerce-order:commerce-line-items:0:quantity" ] },
              "PROVIDE" : { "array_added" : { "array_added_a_line_0" : "array_added_a_line_0" } }
            }
          },
          { "LOOP" : {
              "USING" : { "list" : [ "array_added_a_line_0" ] },
              "ITEM" : { "loop_a_line_0" : "loop_a_line_0" },
              "DO" : [
                { "userpoints_action_grant_points" : {
                    "user" : [ "site:current-user" ],
                    "points" : "20",
                    "tid" : "0",
                    "entity" : [ "" ],
                    "operation" : "2123",
                    "display" : "1",
                    "moderate" : "approved"
                  }
                }
              ]
            }
          }
        ]
      },
      { "ELSE" : [
          { "CONDITIONAL" : [
              {
                "IF" : { "commerce_order_contains_product" : {
                    "commerce_order" : [ "commerce-order:commerce-line-items:1:order" ],
                    "product_id" : "A_sku",
                    "operator" : "\u003E=",
                    "value" : "1"
                  }
                },
                "DO" : [
                  { "array_create" : {
                      "USING" : { "size" : [ "commerce-order:commerce-line-items:1:quantity" ] },
                      "PROVIDE" : { "array_added" : { "array_added_a_line_1" : "array_added_a_line_1" } }
                    }
                  },
                  { "LOOP" : {
                      "USING" : { "list" : [ "array-added-a-line-1" ] },
                      "ITEM" : { "loop_a_line_1" : "loop_a_line_1" },
                      "DO" : [
                        { "userpoints_action_grant_points" : {
                            "user" : [ "site:current-user" ],
                            "points" : "20",
                            "tid" : "0",
                            "entity" : [ "" ],
                            "operation" : "adw",
                            "display" : "1",
                            "moderate" : "approved"
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]
}
}

Rule for B product

{ "rules_b_userpoints" : {
"LABEL" : "B_userpoints",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [
  "rules_conditional",
  "rules_array_create",
  "userpoints_rules",
  "commerce_order",
  "commerce_checkout"
],
"ON" : { "commerce_checkout_complete" : [] },
"DO" : [
  { "CONDITIONAL" : [
      {
        "IF" : { "commerce_order_contains_product" : {
            "commerce_order" : [ "commerce-order:commerce-line-items:0:order" ],
            "product_id" : "B_sku",
            "operator" : "\u003E=",
            "value" : "1"
          }
        },
        "DO" : [
          { "array_create" : {
              "USING" : { "size" : [ "commerce-order:commerce-line-items:0:quantity" ] },
              "PROVIDE" : { "array_added" : { "array_added_b_line_0" : "array_added_b_line_0" } }
            }
          },
          { "LOOP" : {
              "USING" : { "list" : [ "array-added-b-line-0" ] },
              "ITEM" : { "loop_b_line_0" : "loop_b_line_0" },
              "DO" : [
                { "userpoints_action_grant_points" : {
                    "user" : [ "site:current-user" ],
                    "points" : "30",
                    "tid" : "0",
                    "entity" : [ "" ],
                    "operation" : "sdf",
                    "display" : "1",
                    "moderate" : "approved"
                  }
                }
              ]
            }
          }
        ]
      },
      { "ELSE" : [
          { "CONDITIONAL" : [
              {
                "IF" : { "commerce_order_contains_product" : {
                    "commerce_order" : [ "commerce-order:commerce-line-items:1:order" ],
                    "product_id" : "B_sku",
                    "operator" : "\u003E=",
                    "value" : "1"
                  }
                },
                "DO" : [
                  { "array_create" : {
                      "USING" : { "size" : [ "commerce-order:commerce-line-items:1:quantity" ] },
                      "PROVIDE" : { "array_added" : { "array_added_b_line_1" : "array_added_b_line_1" } }
                    }
                  },
                  { "LOOP" : {
                      "USING" : { "list" : [ "array-added-b-line-1" ] },
                      "ITEM" : { "loop_b_line_1" : "loop_b_line_1" },
                      "DO" : [
                        { "userpoints_action_grant_points" : {
                            "user" : [ "site:current-user" ],
                            "points" : "30",
                            "tid" : "0",
                            "entity" : [ "" ],
                            "operation" : "sf",
                            "display" : "1",
                            "moderate" : "approved"
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]
}
}

It's my debugging rules log:

Rule Update the order status on checkout completion fires.
        0 ms Rule Update the order status on checkout completion fires.
        0.069 ms Evaluating the action commerce_order_update_state. [edit]
        15.016 ms Rule Update the order status on checkout completion has fired.
    20.585 ms Evaluating conditions of rule A_userpoints. [edit]
    20.598 ms AND evaluated to TRUE.

    Rule A_userpoints fires.
        0 ms Rule A_userpoints fires.
        3.964 ms The condition commerce_order_contains_product evaluated to TRUE [edit]
        4.071 ms Evaluating the action array_create. [edit]
        4.146 ms Added the provided variable array_added_a_line_0 of type list [edit]
        4.293 ms Looping over the list items of array_added_a_line_0 [edit]
        5.989 ms Evaluating the action userpoints_action_grant_points. [edit]
        15.5 ms Rule A_userpoints has fired.
    36.183 ms Evaluating conditions of rule B_userpoints. [edit]
    36.197 ms AND evaluated to TRUE.

    Rule B_userpoints fires.
        0 ms Rule B_userpoints fires.
        0.88 ms The condition commerce_order_contains_product evaluated to TRUE [edit]
        0.972 ms Evaluating the action array_create. [edit]
        1.046 ms Added the provided variable array_added_b_line_0 of type list [edit]
        1.202 ms Looping over the list items of array-added-b-line-0 [edit]
        1.376 ms Evaluating the action userpoints_action_grant_points. [edit]
        4.833 ms Rule B_userpoints has fired.

Well, I have new information after I enabled Devil module.

When I increase quantity of first line my userpoint, it automatically add to my array 2 item and how I can assume my second rule use the same array. I think it is reason of my trouble.

Ok if I remove array module, how can I start loop via quanlity of product? Maybe anybody suggest another way for my task?

1

Problem in your current rule

I'd have to do some Rules debugging to be 100% sure, but I'm "pretty" sure that the core of your problem is the Loop in each of your 2 Rules. Because your rule seems to be iterating over each of your products (A and B in your sample), and for each iteration you grant those userpoints. So if you have 2 iterations (products, like A and B), you'll grant the products twice (which explains the "double" part of your question). Moreover, I bet that if you'd have a testcase with a 3rd product C, you'd tripple the user points that get granted.

Possible solution

To get your rule to work like you want it to work, you need to "rework" your Rules Condition (for both rules) towards a Rules Condition inside your Rules Loop, so that the user points are only granted provided the appropriate Rules Condition is satisfied.

But using a Rules Condition within Rules Actions is not possible ... you might think. Indeed ... except if you also use the Conditional Rules module (that's exactly what this module is for). As an alternative, you could create a Rules Component, with a Rules Action equivalent to the user points to be granted, and with a Rules Condition equivalent to your current Rules Condition. And then perform that Rules Component inside your Rules Loop. For more details on that, refer to the answer to "What's the alternative to using the Conditional Rules module?".

Rules debugging

If none of the above helps, then move on to Rules debugging, as explained in my answer to "How can I display the value of a variable inside a condition?".

Depending on what the result of that type of debugging is, you may also want to (temporary) enable the Devel module, so that you can (temporary) add a Rules Action like "Display a value".

If appropriate, update your question to add more details about how the Rules debugging log looks like (I might update my answer afterwards depending on what that log would show).

  • Pierre.Vriens, thank for support! I try to do that but I don't work correct too. – NashGC Feb 20 '17 at 9:16
  • @NashGC : I'd be curious to see the results of your debugging (added to your question as an update) ... – Pierre.Vriens Feb 20 '17 at 9:34
  • I installed module "Conditional Rules" and make rules with logic like that: If: Order contains a particular product (commerce-order:commerce-line-items:0:order) -> Create an array (commerce-order:commerce-line-items:0:quantity) -> Loop (Created array) -> Grant userpoint. then I add condition again and make second part of rule, but now it check 1 line item. I create 2 rules for both product. If I test it alone all work great, but when I add both product to the cart, userpoints grant only for position avoid quantity – NashGC Feb 20 '17 at 9:36
  • @NashGC can you extend your question by adding an update (at the end) with something like "I used the conditional rules module (as suggested in the answer), and now this is how my updated rules look like: ..." (and also integrate your prior comment in there) – Pierre.Vriens Feb 20 '17 at 9:41
  • Ok, I did it =) – NashGC Feb 20 '17 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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