Price Mutation Issues
-
Hi there!
We recently had an issue on our site, wholyme.com. Wholyme has manually set prices for GPB (£), USD ($) and Euro (€). We experienced an odd issue recently where if a user had added a product to cart in $ or €; it changed the £ price in the backend. £ is the default currency on the site.The issue is patched on our site, but will share more info with you:
Product links:
The Relief Balm costs £27.
- It was changed to £31.
- The number
31, is what the price of the product in Euros is.
The Relief Salts costs £16
- It was changed to £29.
- The number,
29is what the price of the product in USD is.
——
Our findings:
When do_action(‘woocommerce_after_calculate_totals’, [object]) runs, the price plugin executes the code shown on this link: https://d.pr/i/UQa4t2
This code updates the product price not only within the cart, but globally. Because it does not call save(), the change is not persisted on its own. However, if anything else in the cart process subsequently calls save(), the global product is updated using whichever regional price was applied at that point.
In this instance, the trigger was custom code, making it straightforward to fix. However, the same issue could be caused by any plugin or custom logic that mutates the product object and then calls save() during the cart lifecycle.
The likely reason for this approach is that some discount plugins retrieve pricing via
$product->get_price() during cart calculations. To support this, the alternative price is written directly to the product object, which can lead to unintended side effects.
A safer approach would be to dynamically add a filter during the cart process to modify the value returned by get_price() without mutating or saving the product itself. This filter could then be removed once the cart calculations are complete.We have a PDF version of this saved here: https://d.pr/f/UGTDem
The page I need help with: [log in to see the link]
You must be logged in to reply to this topic.