Change product prices via a hook in WooCommerce 3+
Modifying global price values in WooCommerce is often straightforward, but variable products introduce a layer of complexity that trips up many developers. The standard approach involves filtering woocommerce_get_regular_price and woocommerce_get_price. These hooks successfully alter the base price displayed on single-product pages and cart items for simple products. However, when dealing with variations, simply attaching your logic to these filters often fails to update the specific price associated with a selected attribute combination.
The core issue lies in how WooCommerce calculates pricing for variable products. When a user selects specific optionsâsuch as "Large" size or "Red" colorâthe frontend requests data that includes both a base cost and an additional variation cost. The woocommerce_get_regular_price hook operates before the specific variation data is fully resolved in the rendering context, which is why it doesn't affect the final displayed price for a selected variation.
To successfully modify the price of a specific variation, you must target the internal calculation that happens when WooCommerce processes the variation array. The most reliable hook for this in modern versions of WooCommerce (3.0+) is woocommerce_variation_prices.
Here is how to implement the fix correctly:
add_filter( 'woocommerce_variation_prices', array( $this, 'my_custom_variation_price' ), 20, 1 );
function my_custom_variation_price( $prices ) {
// Logic for calculating the new price here
// The prices parameter is an associative array containing various price keys
// Multiply the specific variation price by 2
if ( ! empty( $prices['price'] ) ) {
$prices['price'] = $prices['price'] * 2;
}
// If there is a discount or sale price, that should also be adjusted
if ( ! empty( $prices['formatted_price'] ) ) {
// Note: Formatting logic usually happens after calculation.
// Adjusting the raw numeric 'price' key ensures the formatted version updates.
}
return $prices;
}
You might notice that filtering woocommerce_get_variation_regular_price or woocommerce_get_variation_price often yields no results. While these hooks exist, they are frequently bypassed by the internal data structure returned by woocommerce_variation_prices. The latter hook provides a comprehensive array where you can directly manipulate the price, regular_price, and any other numeric keys before WooCommerce renders them into HTML or JSON responses.
It is crucial to understand that this modification must happen at the data layer, not the display layer. Attempting to wrap the result in a <span> tag or modify the output string via HTML filters (like woocommerce_product_single_variation_price_html) will fail because the price is likely already cached or rendered elsewhere in the DOM by the time your filter runs. You must return a modified data array, ensuring that every part of the frontend consuming this object sees the updated values.
When applying this logic, be mindful of tax calculations. If you are doubling the product price, ensure your tax settings are configured to handle the new base amount correctly. In some configurations, WooCommerce calculates tax on the final displayed price; in others, it relies on the raw price key in the data array. Modifying the raw numeric value is generally safer than manipulating formatted strings, as it keeps the math consistent across different currency formats and localization settings.
For developers managing complex pricing strategies where global multipliers are required, this method provides a clean separation of concerns. It avoids hardcoding prices into the database or relying on plugins that might conflict with custom themes. If you require more advanced price management beyond simple multiplication, consider integrating with a dedicated pricing plugin or leveraging woocommerce_product_get_price for simpler scenarios, but for variation-specific adjustments, direct manipulation of the variation prices array remains the most robust solution.
If you are building a custom e-commerce solution and need to extend WooCommerce functionality without bloating your codebase with heavy plugins, mastering these core hooks allows you to maintain full control over pricing logic while keeping performance optimized. For further insights on extending WooCommerce functionalities with clean, maintainable code, resources like LaravelCompany often provide useful patterns for integrating complex business logic into standard frameworks.