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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45806249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:09:27  来源:igfitidea点击:

Change product prices via a hook in WooCommerce 3

phpwordpresswoocommerceproductprice

提问by KronosL

IN WooCommerce, I need to multiply all product prices by a number. So I have used the following (via a plugin):

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

But, that doesn't work for variation products.I have tried the following hooks with no luck:

但是,这不适用于变体产品。我尝试了以下钩子但没有运气:

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

The only one that works half way is this one:

唯一能做一半的就是这个:

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

But that just changed the overall price, not the selected variation price. See the image below, price is BsF. 200 and the overall price is right, 200 x 2 = 400, but the variation price when selected still shows 200:

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

Note: I need it to actually change, so display html hooks wont work.

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

Variation Price

变动价格

Is there anything I'm missing, or something wrong?

有什么我遗漏的,或者有什么问题吗?

回答by LoicTheAztec

Update 4(September 2019)

  • 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_hashfilter hook much more efficient, instead of wc_delete_product_transients()… See this related thread
  • Added product price filter widget hooks (see at the end).

更新 4 (2019 年 9 月)

  • 主题和插件的 2 个代码版本(也适用于 Woocommerce 3.3.x)
  • Woocommerce 3 中的缓存变体价格(更新和添加)
    现在使用woocommerce_get_variation_prices_hash过滤器钩子效率更高,而不是wc_delete_product_transients()......请参阅此相关线程
  • 添加了产品价格过滤器小部件挂钩(见文末)

1) Plugin versionwith a constructor function:

1)带构造函数的插件版本

The hooks that you are using are deprecated in WooCommerce 3+

您使用的钩子在 WooCommerce 3+ 中已弃用

To make it work for all products prices, including variations prices, you should use this:

要使其适用于所有产品价格,包括变体价格,您应该使用:

## 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 (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( $hash ) {
    $hash[] = get_price_multiplier();
    return $hash;
}

The code tested and perfectly works (only) in WooCommerce 3+.

代码经过测试并在 WooCommerce 3+ 中完美运行(仅)。



2) For theme version:functions.phpfile on active child theme (or active theme):

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, 1 );
function add_price_multiplier_to_variation_prices_hash( $hash ) {
    $hash[] = get_price_multiplier();
    return $hash;
}

Tested and works on woocommerce 3+

在 woocommerce 3+ 上测试和工作



For products in sale you have those hooks:

对于销售中的产品,您有这些挂钩:

  • woocommerce_product_get_sale_price(Simple, grouped and external products)
  • woocommerce_variation_prices_sale_price(Variable products (min-max))
  • woocommerce_product_variation_get_sale_price(Products variations)
  • woocommerce_product_get_sale_price(简单、分组和外部产品)
  • woocommerce_variation_prices_sale_price(可变产品(最小-最大))
  • woocommerce_product_variation_get_sale_price(产品变化)


Cached prices and woocommerce 3:

缓存价格和 woocommerce 3:

The 3 filters hooks involved in variations cached prices are:

变化缓存价格中涉及的 3 个过滤器钩子是:

  • woocommerce_variation_prices_price
  • woocommerce_variation_prices_regular_price
  • woocommerce_variation_prices_sale_price
  • woocommerce_variation_prices_price
  • woocommerce_variation_prices_regular_price
  • woocommerce_variation_prices_sale_price

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

Woocommerce 3 中引入的woocommerce_get_variation_prices_hash过滤器钩子将允许以更有效的方式刷新缓存价格的变化,而无需在执行此钩子的任何时候删除相关的瞬态。

So performances will stay boosted (Thanks to Matthew Clarkthat pointed this better way)

所以性能将保持提升(感谢马修克拉克指出了这个更好的方式)

See: Caching and dynamic pricing – upcoming changes to the get_variation_prices method

请参阅:缓存和动态定价 - get_variation_prices 方法即将发生的变化



For filtering product prices with a widget(min and max price), use the following hooks:

要使用小部件(最低和最高价格)过滤产品价格,请使用以下挂钩:

  • woocommerce_price_filter_widget_min_amountthat has one argument$price
  • woocommerce_price_filter_widget_max_amountthat has one argument$price
  • woocommerce_price_filter_widget_min_amount有一个论点$price
  • woocommerce_price_filter_widget_max_amount有一个论点$price