2

I would like to grant user points to a product owner in amount of 70% of product price after product is sold. I have a rule and part of component

Rule

Events:
Completing the checkout process

Actions:
Loop (commerce-order:commerce-line-items)

Rules Component

Conditions:
Entity has field (Entity: commerce-line-item, Field: commerce_product)

In action I can not put the value for field POINTS and I'm not sure about the user. Now I have: commerce-line-item:commerce-product:creator

Actions:
Grant points to a user:

What should I do more for my purpose? I have Drupal 7 installed.

3 Answers 3

0

It is complicated rule.

I can say that you need in rule or maybe in Rule component (I'm not sure) U need also add:

  1. "Calculate a value" action to calculate the points which should be added to user balance. commerce-price:amount * 0.7 (here will be created some new value. EX.: point_to_plus

  2. After use "Calculate a value" action one more time to sum current points and new. current points + point_to_plus = final result.

If you use the user points module first load user points. Some thing like that. Af course you need to loop over each line item of the order. I do know how to do it correct. Maybe some one will help you! Good luck!

0

First creat a field balance in user account

Rule

{ "rules_add_points" : {
    "LABEL" : "Add points",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "Commerce Checkout" ],
    "REQUIRES" : [ "rules", "commerce_checkout" ],
    "ON" : { "commerce_checkout_complete" : [] },
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "commerce-order:commerce-line-items" ] },
          "ITEM" : { "list_item" : "Current list item" },
          "DO" : [
            { "component_rules_calc_points" : { "line_item" : [ "list-item" ], "point" : "0.9" } }
          ]
        }
      }
    ]
  }
}

Component

{ "rules_calc_points" : {
    "LABEL" : "Calculation of point",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "line_item" : { "label" : "lin item", "type" : "commerce_line_item" },
      "point" : { "label" : "Points", "type" : "decimal" }
    },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "line-item" ], "field" : "commerce_product" } }
    ],
    "DO" : [
      { "data_calc" : {
          "USING" : {
            "input_1" : [ "line-item:commerce-unit-price:amount-decimal" ],
            "op" : "*",
            "input_2" : [ "point" ]
          },
          "PROVIDE" : { "result" : { "points2add" : "Points 2 add" } }
        }
      },
      { "data_calc" : {
          "USING" : {
            "input_1" : [ "line-item:commerce-product:creator:field-balance" ],
            "op" : "+",
            "input_2" : [ "points2add" ]
          },
          "PROVIDE" : { "result" : { "result2" : "Calculation result" } }
        }
      },
      { "data_set" : {
          "data" : [ "line-item:commerce-product:creator:field-balance" ],
          "value" : [ "result2" ]
        }
      }
    ]
  }
}
1
  • 1
    Sure you want to add a new answer instead of editing your first? Anyway to indent your code you can mark it and use CTRL + K.
    – bummi
    Apr 23, 2015 at 20:07
0

Even though this previous answer is a good start, it does not take into account that this question is about granting points using the User Points module (eg: the Rules Component included only REQUIRES rules (and not userpoints_rules). This answer provides a solution that actually grants such points using User Points.

1. Rules Component

Create a Rules Component, which looks like so (in Rules export format):

{ "rules_perform_an_action_on_a_selected_line_item" : {
    "LABEL" : "Perform an action on a selected line item",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "userpoints_rules" ],
    "USES VARIABLES" : {
      "selected_line_item" : { "label" : "Selected Line Item", "type" : "commerce_line_item" },
      "multiplication_factor" : { "label" : "Multiplication factor", "type" : "decimal" }
    },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "selected-line-item" ], "field" : "commerce_product" } }
    ],
    "DO" : [
      { "entity_fetch" : {
          "USING" : {
            "type" : "commerce_product",
            "id" : [ "selected-line-item:commerce-product:product-id" ]
          },
          "PROVIDE" : { "entity_fetched" : { "product_fetched" : "Fetched Product" } }
        }
      },
      { "drupal_message" : { "message" : "Some details about this product: Product ID = [product-fetched:product-id], Product SKU = [product-fetched:sku], Product Title = [product-fetched:title], Product Creator = [product-fetched:creator], Decimal Product Price = [product-fetched:commerce-price:amount-decimal]" } },
      { "data_calc" : {
          "USING" : {
            "input_1" : [ "product-fetched:commerce-price:amount-decimal" ],
            "op" : "*",
            "input_2" : [ "multiplication-factor" ]
          },
          "PROVIDE" : { "result" : { "points_calculated" : "Points calculated" } }
        }
      },
      { "data_convert" : {
          "USING" : { "type" : "integer", "value" : [ "points-calculated" ] },
          "PROVIDE" : { "conversion_result" : { "points_converted" : "Points Converted" } }
        }
      },
      { "userpoints_action_grant_points" : {
          "user" : [ "product-fetched:creator" ],
          "points" : [ "points-converted" ],
          "tid" : "0",
          "entity" : [ "selected-line-item:order" ],
          "operation" : "Grant points to product creator",
          "display" : "1",
          "moderate" : "default"
        }
      }
    ]
  }
}

If you have the User Points module enabled, you should be able to just import this Rules Component in your own site.

Some more details about it:

  • Rules Condition: entity selected-line-item has field commerce_product.
  • Rules Actions:
    • Fetch entity by ID, to make all data about the Commerce product available for further processing (e.g: the Product creator who will receive the user points ...).
    • Show a message (just for demo / testing purposes, feel free to delete this Rules Action if you don't need/want it anymore).
    • Calculate a value, i.e. to multiply the (decimal) product price of the line item, with some multiplication_factor (like the "* amount of 70% of product*" in the question, provided as a parameter for the Rules Component, with data type decimal), and store the result in points_calculated.
    • Convert data type, to convert (decimal) points_calculated to (integer) points_converted (note the default rounding behavior being used, feel free to adapt to "down" or "up" if you prefer). The reason why this conversion is needed, is because only integers can be selected as tokens in the subsequent "Grant points to a user" Rules Action (i.e it's not possible to select (decimal) points_calculated for that).
    • Grant points to a user, i.e. to grant points-converted to product-fetched:creator (the creator of the Commerce product). Feel free to adapt the various options (e.g: Category, reason, etc) for this Rules Action to fit your needs.

2. Rule that invokes the Rules Component

Create a rule which invokes the Rules Component from the previous step, and which looks like so (in Rules export format):

{ "rules_process_product_fields_at_checkout" : {
    "LABEL" : "Process product fields at checkout",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "commerce_checkout" ],
    "ON" : { "commerce_checkout_complete" : [] },
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "commerce-order:commerce-line-items" ] },
          "ITEM" : { "line_item" : "Current line item" },
          "DO" : [
            { "drupal_message" : { "message" : "Current line item has label \u0027[line-item:line-item-label]\u0027 and id \u0027[line-item:line-item-id]\u0027" } },
            { "component_rules_perform_an_action_on_a_selected_line_item" : {
                "selected_line_item" : [ "line-item" ],
                "multiplication_factor" : "0.7"
              }
            }
          ]
        }
      }
    ]
  }
}

You should be able to just import this Rules Component in your own site. The Rules Event is obvious (at checkout time), while there is no need for any Rules Conditions. About the rules Actions:

  • Perform a Rules Loop, to iterate over each line item in the cart.
  • Within that Rules Loop, show a message (just for demo / testing purposes also) and invoke the Rules Component from Step 1, using these values for the 2 parameters:
    • the "selected_line_item" being processed within the Rules Loop.
    • the "multiplication_factor" which in this case is (hardcoded) "0.7" (the 70% as in the question), feel free to change to whatever you want (or just use "1.0" to not take that parameter into consideration.

3. Quantity of each line item

The above Rules Component does not take into account the quantity of each line item. However to also cover that, all that's needed is to add another Rules Action in the Rules Component to perform a similar "Calculate a value" (to multiply points_calculated also by the available selected-line-item:quantity token).

4. Demo

Consider a shopping cart which looks like so at checkout time:

Product                 Price       Quantity    Total

Black Bowler - Small    125,00 €    2           250,00 €
Red T-Shirt - Small     11,00 €     3           33,00 €

                                    Subtotal    233,89 €
                                    VAT (21%)   49,11 €
                                    Order total 283,00 €

After completing the checkout, these are the relevant messages shown (for demo/testing purposes only):

Current line item has label 'HAT05' and id '12'
Some details about this product: Product ID = 5, Product SKU = HAT05, Product Title = Black Bowler - Small, Product Creator = Dries.Buytaert, Decimal Product Price = 125.00

Current line item has label 'SHRT01' and id '13'
Some details about this product: Product ID = 6, Product SKU = SHRT01, Product Title = Red T-Shirt - Small, Product Creator = Dries.Buytaert, Decimal Product Price = 11.00

And for the product creator, who had not earned any User Points yet, this is how the User Points report looks like afterwards (sort = most recent first):

Points for Dries.Buytaert

Points  Date                        Reason                          Status

8       Tue, 02/06/2018 - 13:19     Grant points to product creator Approved
88      Tue, 02/06/2018 - 13:19     Grant points to product creator Approved

Total points
Approved points 96
Pending points  0

Do the math: 88 (= 125.00 * 0.7, rounded) User Points for the Black Bowler, and 8 (= 11.00 * 0.7, rounded) User Points for the Red T-Shirt.

5. Homework (= open question)

If a user (= "current user") "buys" products from a product creator that equals "current user", then I don't think that user should be granted any User Points for doing so. But that was not part of the question here (solution to the problem, changes the problem ...).

Your Answer

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

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