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