You are here

Add a price comparison condition

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.

Comments

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.

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.

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.