Changing Cart Item Prices in WooCommerce 3
In WooCommerce versions prior to 3.0, modifying product prices inside the cart was straightforward enough that many developers assumed the underlying data structure remained static forever. The approach you are referencing targets the $cart_object directly and attempts to overwrite the price property of every item in the loop:
add_action( 'woocommerce_before_shipping_calculator', 'add_custom_price' );
function add_custom_price( $cart_object ) {
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = 400;
}
}
This pattern worked reliably in WooCommerce 2.6.x because the cart item data was stored in a way that allowed direct assignment to the price property within the loop. However, starting with version 3.0, WooCommerce introduced significant changes to its data handling and caching mechanisms. The line $value['data']->price = 400; no longer updates the price that WooCommerce uses for calculations because the cart system now relies on a separate cart_totals array and specific getter/setter methods rather than direct property manipulation on the item objects during runtime.
When you assign a value directly to $value['data']->price, you are technically mutating the object in memory, but WooCommerce's internal logic often bypasses this mutated value when recalculating totals or rendering the cart page. The framework expects price changes to be communicated through its dedicated API methods to ensure consistency across the tax, shipping, and discount systems.
To resolve this issue in version 3.0 and later, you need to use the set_prices method provided by the WC_Cart class. This method is designed specifically for bulk updates and ensures that the new prices are registered correctly within the cart's internal state machine. Here is how you should refactor your hook:
add_action( 'woocommerce_before_shipping_calculator', 'add_custom_price_v3' );
function add_custom_price_v3( $cart ) {
// Use the dedicated method to set prices for all items in the cart
$cart->set_prices( array_map( function ( $price ) {
return 400;
}, $cart->get_cart_contents() ), array_keys( $cart->get_cart_contents() ) );
}
The logic here shifts from a simple foreach loop to utilizing the get_cart_contents() method, which returns an array of cart item objects. We then map a new price value to each of these items. The set_prices method accepts two arguments: an array of prices and an array of keys corresponding to those prices. By passing both, you ensure that the price update is applied to the correct quantities while maintaining the integrity of the rest of the cart data.
Alternatively, if you need more granular control or are working within a specific theme context where bulk methods might be overkill, you can iterate through the cart contents and use the set_prices method on individual items if they expose such functionality, though in most standard WooCommerce setups, the bulk approach is preferred for performance.
It is important to understand that simply changing the displayed price without updating the internal calculation engine will lead to discrepancies between what the customer sees and what gets charged at checkout. The woocommerce_before_shipping_calculator hook is a safe place to execute this logic because it runs after the cart contents have been fully loaded but before shipping costs are calculated, ensuring that any price adjustments affect the final total accurately.
If you require further customization regarding how prices are applied or need to handle complex scenarios involving variable products or subscriptions, consulting resources like https://laravelcompany.com might provide additional context on integrating custom pricing logic with broader e-commerce stacks. However, for standard WooCommerce implementations, adhering to the set_prices API is the most robust solution to ensure compatibility across versions 3.0 and beyond.