In WooCommerce, modifying product prices requires the use of specific hooks. While this process is straightforward for simple products, variation products present a unique challenge.
Using the outdated hooks woocommerce_get_regular_price and woocommerce_get_price is no longer effective for variation products in WooCommerce 3 as they have been deprecated. To ensure compatibility, it's essential to utilize the appropriate hooks and apply them correctly.
To modify all product prices, including variation prices, implement the following hooks:
Plugin Version:
add_filter('woocommerce_product_get_price', 'custom_price', 99, 2); add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2); // Variations add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2); add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2); // Variable (price range) add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 99, 3); add_filter('woocommerce_variation_prices_regular_price', 'custom_variable_price', 99, 3);
Theme Version:
add_filter('woocommerce_product_get_price', 'custom_price', 99, 2 ); add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2 ); // Variations add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 ); add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );
Inside the custom_price and custom_variable_price functions, apply the desired price multiplier to the existing price to achieve the desired change.
Variable products display a range of prices. The woocommerce_variation_prices_price and woocommerce_variation_prices_regular_price hooks can be used to modify this range.
To handle cached prices in WooCommerce 3 , utilize the woocommerce_get_variation_prices_hash hook. This hook enables efficient price updates without deleting transients.
For filtered product prices using a widget, utilize the following hooks:
The above is the detailed content of How to Effectively Modify WooCommerce Product Prices (Including Variations) Using Hooks?. For more information, please follow other related articles on the PHP Chinese website!