Change Product Prices Via a Hook in WooCommerce 3
The WooCommerce platform provides various hooks to modify product prices. While the solution presented using add_filter('woocommerce_get_regular_price') and add_filter('woocommerce_get_price') works for simple products, it encounters limitations when it comes to variation products.
Variation Product Pricing
To adjust variation product prices, the following updated hooks are recommended:
Simple, Grouped, and External Products:
Variations:
Variable Product Range:
Plugin Implementation
To implement these hooks within a plugin, consider the following:
add_filter('woocommerce_product_get_price', 'custom_price', 99, 2); add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2); add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2); add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2); add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 99, 3); add_filter('woocommerce_variation_prices_regular_price', 'custom_variable_price', 99, 3); function custom_price($price, $product) { $multiplier = 2; // Adjust as needed return (float) $price * $multiplier; } function custom_variable_price($price, $variation, $product) { $multiplier = 2; // Adjust as needed return (float) $price * $multiplier; }
Theme Implementation
If you prefer a theme-based approach, include the following code in your functions.php file:
add_filter('woocommerce_product_get_price', 'custom_price', 99, 2); add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2); add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2); add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2); add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 99, 3); add_filter('woocommerce_variation_prices_regular_price', 'custom_variable_price', 99, 3); function custom_price($price, $product) { $multiplier = 2; // Adjust as needed return (float) $price * $multiplier; } function custom_variable_price($price, $variation, $product) { $multiplier = 2; // Adjust as needed return (float) $price * $multiplier; }
Additional Notes and Enhancements
The above is the detailed content of How Can I Programmatically Change Product Prices in WooCommerce 3 ?. For more information, please follow other related articles on the PHP Chinese website!