通过 WooCommerce 3+ 中的挂钩更改产品价格

Change product prices via a hook in WooCommerce 3+

在 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;
 }

但是,不适用于变体产品。我尝试了以下挂钩,但没有成功:

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

唯一一个半途而废的是这个:

add_filter('woocommerce_variation_prices_price', array( $this, 'my_custom_price'), 99);

但这只是改变了整体价格,而不是选择的变化价格。见下图,价格为 BsF。 200,整体价格合适,200 x 2 = 400,但选择时的变化价格仍然显示 200:

注意:我需要它来实际更改,因此显示 html 挂钩将不起作用。

我是否遗漏了什么或有什么问题?

Update (December 2020)

  • 2 code versions for themes and plugins (works in Woocommerce 3.3.x too)
  • Cached variations prices in Woocommerce 3 (Update and addition):
    Now using woocommerce_get_variation_prices_hash filter hook much more efficient, instead of wc_delete_product_transients()… See this related thread
  • Added product price filter widget hooks (see at the end).

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

Introduced in Woocommerce 3, woocommerce_get_variation_prices_hash filter hook will allow to refresh variations cached prices in a much more efficient way, without deleting related transients anytime that this hooks are executed.

因此性能将保持提升(感谢 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