更新 (2020 年 12 月)
- 主题和插件的 2 个代码版本(也适用于 Woocommerce 3.3.x)
- Woocommerce 3 中的缓存变化价格(更新和添加):
现在使用 woocommerce_get_variation_prices_hash 过滤器挂钩效率更高,而不是 wc_delete_product_transients()... 请参阅 this related thread
- 添加了产品价格过滤器小部件挂钩(见最后)。
1) 带有构造函数的插件版本:
您使用的钩子在 WooCommerce 3+ 中已弃用
要使其适用于所有产品价格,包括变体价格,您应该使用这个:
## The following goes inside the constructor ##
// Simple, grouped and external products
add_filter('woocommerce_product_get_price', array( $this, 'custom_price' ), 99, 2 );
add_filter('woocommerce_product_get_regular_price', array( $this, 'custom_price' ), 99, 2 );
// Variations
add_filter('woocommerce_product_variation_get_regular_price', array( $this, 'custom_price' ), 99, 2 );
add_filter('woocommerce_product_variation_get_price', array( $this, 'custom_price' ), 99, 2 );
// Variable (price range)
add_filter('woocommerce_variation_prices_price', array( $this, 'custom_variable_price' ), 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', array( $this, 'custom_variable_price' ), 99, 3 );
// Handling price caching (see explanations at the end)
add_filter( 'woocommerce_get_variation_prices_hash', array( $this, 'add_price_multiplier_to_variation_prices_hash' ), 99, 3 );
## This goes outside the constructor ##
// Utility function to change the prices with a multiplier (number)
public function get_price_multiplier() {
return 2; // x2 for testing
}
public function custom_price( $price, $product ) {
return (float) $price * get_price_multiplier();
}
public function custom_variable_price( $price, $variation, $product ) {
return (float) $price * get_price_multiplier();
}
public function add_price_multiplier_to_variation_prices_hash( $price_hash, $product, $for_display ) {
$price_hash[] = get_price_multiplier();
return $price_hash;
}
经过测试的代码(仅)在 WooCommerce 3+ 中完美运行。
2) 对于主题版本: functions.php 活动子主题(或活动主题)上的文件:
// Utility function to change the prices with a multiplier (number)
function get_price_multiplier() {
return 2; // x2 for testing
}
// Simple, grouped and external products
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 );
function custom_price( $price, $product ) {
return (float) $price * get_price_multiplier();
}
// 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 );
function custom_variable_price( $price, $variation, $product ) {
// Delete product cached price (if needed)
// wc_delete_product_transients($variation->get_id());
return (float) $price * get_price_multiplier();
}
// Handling price caching (see explanations at the end)
add_filter( 'woocommerce_get_variation_prices_hash', 'add_price_multiplier_to_variation_prices_hash', 99, 3 );
function add_price_multiplier_to_variation_prices_hash( $price_hash, $product, $for_display ) {
$price_hash[] = get_price_multiplier();
return $price_hash;
}
在 woocommerce 3+ 上测试并运行
对于销售中的产品,您有这些钩子:
-
woocommerce_product_get_sale_price (简单、分组和外部产品)
-
woocommerce_variation_prices_sale_price (可变产品(最小-最大))
-
woocommerce_product_variation_get_sale_price (产品变体)
缓存价格和 woocommerce 3:
变量缓存价格中涉及的 3 个过滤器挂钩是:
woocommerce_variation_prices_price
woocommerce_variation_prices_regular_price
woocommerce_variation_prices_sale_price
在 Woocommerce 3 中引入,woocommerce_get_variation_prices_hash 过滤钩子将允许以更有效的方式刷新变化缓存的价格,而不会在执行此钩子时删除相关的瞬态。
所以性能将保持提升(感谢Matthew Clark 指出了更好的方法)
见:Caching and dynamic pricing – upcoming changes to the get_variation_prices method
要使用小部件过滤产品价格 (最低和最高价格),请使用以下挂钩:
-
woocommerce_price_filter_widget_min_amount 只有一个参数 $price
-
woocommerce_price_filter_widget_max_amount 只有一个参数 $price