通过WooCommerce 3中的钩子改变产品价格

Kro*_*osL 14 php wordpress product woocommerce price

在WooCommerce中,我需要将所有产品价格乘以一个数字.所以我使用了以下内容(通过插件):

add_filter('woocommerce_get_regular_price', array( $this, 'my_custom_price'), 99);
add_filter('woocommerce_get_price', array( $this, 'my_custom_price'), 99);

function my_custom_price( $original_price ) {
  global $post, $woocommerce;

  //Logic for calculating the new price here
  $new_price = $original_price * 2;

  //Return the new price (this is the price that will be used everywhere in the store)
  return $new_price;
 }
Run Code Online (Sandbox Code Playgroud)

但是,这对变体产品不起作用.我试过以下钩子没有运气:

add_filter('woocommerce_get_variation_regular_price', array( $this, 'my_custom_price'), 99);
add_filter('woocommerce_get_variation_price', array( $this, 'my_custom_price'), 99);
Run Code Online (Sandbox Code Playgroud)

唯一有效的是这一个:

add_filter('woocommerce_variation_prices_price', array( $this, 'my_custom_price'), 99);
Run Code Online (Sandbox Code Playgroud)

但这只是改变了整体价格,而不是选定的变化价格.见下图,价格是BsF.200和整体价格是正确的,200 x 2 = 400,但选择时的变化价格仍显示200:

注意:我需要它实际更改,所以显示html钩子不会工作.

变动价格

有什么我想念的,或者有什么不对吗?

Loi*_*tec 16

更新3 (2018年9月)

  • 主题和插件的2个代码版本(也适用于Woocommerce 3.3.x)
  • Woocommerce 3中的缓存变化价格(更新和添加):
    现在使用woocommerce_get_variation_prices_hash过滤器钩子效率更高,而不是wc_delete_product_transients()...查看此相关主题

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, 1 );


## 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 $price * get_price_multiplier();
}

public function custom_variable_price( $price, $variation, $product ) {
    return $price * get_price_multiplier();
}

public function add_price_multiplier_to_variation_prices_hash( $hash ) {
    $hash[] = get_price_multiplier();
    return $hash;
}
Run Code Online (Sandbox Code Playgroud)

代码在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 $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 $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, 1 );
function add_price_multiplier_to_variation_prices_hash( $hash ) {
    $hash[] = get_price_multiplier();
    return $hash;
}
Run Code Online (Sandbox Code Playgroud)

测试并适用于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过滤器钩子将允许以更有效的方式刷新缓存价格的变化,而不会在执行此钩子的任何时候删除相关的瞬态.

因此,表演将继续得到提升(感谢马修克拉克指出这更好的方式)

请参阅:缓存和动态定价 - 即将对get_variation_prices方法进行的更改