One of the challenges in trying to make the Rules UI more friendly to Drupal Commerce price data manipulation and comparison is that in order to convert major unit decimal values to minor unit integer values we must know the currency. However, in a Rules configuration form context, the currency could literally be anything. We can make a best guess by assuming that we'll only ever have to deal with enabled currencies, but even that could leave open an edge case where products have prices in currencies that are no longer enabled on the site.

One middle of the road solution is to actually add a price comparison condition that people can use to compare product prices, line item prices, and order totals. This condition can take a price field selector parameter, a decimal price field, and a currency code select list, and when the condition is actually evaluated it can use our currency conversion function to first convert the selected price to the currency chosen for the comparison. For single currency stores, instead of a select list, we'd just show the currency code as a suffix to the decimal textfield.

#2 price-comparison-1558380-2.patch8.49 KBvasike
PASSED: [[SimpleTest]]: [MySQL] 3,589 pass(es). View
#1 price-comparison.patch8.93 KBhelior
PASSED: [[SimpleTest]]: [MySQL] 3,567 pass(es). View


helior’s picture

Status: Active » Needs review
Issue tags: +1.3 review
8.93 KB
PASSED: [[SimpleTest]]: [MySQL] 3,567 pass(es). View

Here' an updated input form for RulesDataUICommercePrice which allows users to input price amount in major format, while converting to minor format (integer) on storage. Also a price comparison Rules condition plugin.

vasike’s picture

8.49 KB
PASSED: [[SimpleTest]]: [MySQL] 3,589 pass(es). View

i applied the patch and the "Price comparison" rules condition became available. i tried within a Tax Rate and it did work.

when i applied the patch i got a warning: "1 line adds whitespace errors".
i removed the whitespace and recreated the patch.

vasike’s picture

Status: Needs review » Reviewed & tested by the community

the solution looks just fine

rszrama’s picture

Status: Reviewed & tested by the community » Fixed

w00t! Committed this and spawned a follow-up issue to make the new RulesDataUICommercePrice input form work better with the Set a data value action. We need to be able to define a default price component type and initialize the components array; until then this new input form is really only useful for the Price comparison condition added by this patch.

See: #1798776: Add a default price component option to RulesDataUICommercePrice::inputForm()

Oh, and a minor change: instead of converting NULL amounts to 0, I throw an error message. I see that this was done because you can't have required elements in inputForm callbacks apparently, but from a price calculation standpoint, NULL means unpurchasable while 0 means free. For actions and conditions using this form directly, right now I want to force a numeric amount (even if it's 0) while preserving the meaning of NULL if you were to set a price field's amount to NULL like the default rule "Unset the price of disabled products in the cart" does.


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