通过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钩子将无法工作。

变动价格

有什么我想念的东西吗?

LoicTheAztec

更新4 (2019年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 (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+中正常运行。


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

经过测试并在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指出了这种更好的方法)

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


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

  • woocommerce_price_filter_widget_min_amount 有一个论点 $price
  • woocommerce_price_filter_widget_max_amount 有一个论点 $price

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过挂钩更改产品价格的问题

通过挂钩更改WooCommerce 3+中特定类别的产品价格

Magento:通过获取产品编号来更改产品价格

在Woocommerce 3中获取产品价格

使用 Woocommerce 中的挂钩更新产品价格

根据 Woocommerce 中用户相关的购买动态更改产品价格

在Woocommerce单个产品页面中更改并保存产品价格

如果在Woocommerce 3中更新了价格,则更改产品状态

通过WooCommerce中的挂钩有条件地更改产品税类

从Woocommerce 3中的特定产品标签更改所有产品价格

通过自定义字段在Woocommerce中更改购物车中的产品价格

MySQL查询以更改WooCommerce中的可变产品价格

获取要在WooCommerce 3中显示的产品价格

在Woocommerce 3中更新和显示更新的产品价格

如果相应的产品变体在 WooCommerce 购物车中,则更改产品变体的价格

根据woocommerce中的产品价格显示%

有条件地更改Woocommerce中的特定产品价格

Woocommerce通过php更改产品图片

在Woocommerce中检查产品价格是否含税3+

WooCommerce 3中的有条件产品价格购物车问题

Woocommerce中基于产品价格的条件简码

恢复Woocommerce中特定运输方式的打折产品价格

如何在以下Woocommerce缺货产品文本中添加单个产品上的Woocommerce产品价格

在WooCommerce 3.3中更改产品缩略图的大小

在简短说明字段中更改产品状态Woocommerce

在 woocommerce 订单详细信息中更改产品网址

允许客户设置产品价格并通过WooCommerce中的某些验证将其添加到购物车

更新WooCommerce产品价格和库存

Woocommerce可变产品价格前缀