通过WooCommerce 3中的挂钩更改产品价格 [英] Change product prices via a hook in WooCommerce 3

查看:93
本文介绍了通过WooCommerce 3中的挂钩更改产品价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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:

注意:我需要对其进行实际更改,因此显示html挂钩无法正常工作.

有什么我想念的东西吗?

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

推荐答案

更新4 (2019年9月)

  • 两个主题和插件的代码版本(在Woocommerce 3.3.x中也适用)
  • Woocommerce 3中的缓存变化价格(更新和添加):
    现在使用woocommerce_get_variation_prices_hash过滤器挂钩比wc_delete_product_transients()效率更高,请参阅
  • 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 hooks that you are using are deprecated in 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;
}

经过测试的代码可以(仅)在WooCommerce 3+中正常运行.

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

2)对于主题版本: functions.php 关于活动子主题(或活动主题)的文件:

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

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

经过测试并可以在woocommerce 3+上使用

Tested and works on woocommerce 3+

对于销售中的产品,您有那些钩子:

For products in sale you have those hooks:

  • woocommerce_product_get_sale_price (简单,分组和外部产品)
  • woocommerce_variation_prices_sale_price (可变产品(最小-最大))
  • woocommerce_product_variation_get_sale_price (产品变体)
  • 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)

与变化的缓存价格有关的3个过滤器挂钩是:

The 3 filters hooks involved in variations cached prices are:

  • 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

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

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 指出了这种更好的方式)

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

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

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

    具有一个参数的
  • woocommerce_price_filter_widget_min_amount $price
  • 具有一个参数的
  • woocommerce_price_filter_widget_max_amount $price
  • woocommerce_price_filter_widget_min_amount that has one argument $price
  • woocommerce_price_filter_widget_max_amount that has one argument $price

这篇关于通过WooCommerce 3中的挂钩更改产品价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆