Woocommerce的多个产品具有来自functions.php的自定义定价


Woocommerce multiple products with custom pricing from functions.php

我使用以下代码从functions.php更改价格。即使在购物车页面中,它也适用于单个产品。如何将代码用于不同定价的不同产品?这可能吗?

"团队"是一个分类法,"a团队"、"B团队"是术语。我将产品分配给"A团队"answers"B团队"。

例如:

对于"A-Team":product_id 14=200美元,15=600美元,17=800美元;

对于"B团队":product_id 14=$100,15=$300,17=$400;

原价:product_id 14=55美元,15=44美元,17=88美元;

在分类归档页面中:

如果"A-Team"存档产品应显示

[product_id 14=200美元,15=600美元,17=800美元;]

如果"B团队"存档产品应显示

[product_id 14=$100,15=$300,17=$400;]

我的代码:

function change_price($price, $productd){ 
   if($productd->id == 16): 
      return 160; 
   else: 
      return $price;
   endif;
add_filter('woocommerce_get_price','change_price', 10, 2);
add_filter('woocommerce_get_regular_price','change_price', 10, 2);
add_filter('woocommerce_get_sale_price','change_price', 10, 2);

提前谢谢。

我对代码进行了如下更改。这对我很有效。希望这能对其他有问题的人有所帮助。

`add_filter( 'woocommerce_get_price', function ($price, $productd ) { 
       if($team_id) :
          return get_post_meta($productd->id, 'tax_'.$team_id , true); 
       else:
          return $price;
       endif;
  }, 10, 2 );`

即使在"添加到购物车"功能上也没有问题。

谢谢,Satya

谢谢萨蒂亚和赫尔加特维金。我为用户创建了一个自定义字段,用户可以在其中设置货币(美元或欧元)。我为每个产品创建了两个自定义字段:"_price_usd"answers"_price_euro",每个字段都包含产品的美元价格和欧洲欧元价格。使用匿名函数解决了几个问题。现在我只需要根据用户的货币输入正确的后缀或前缀。这是我对你的代码的改编版本:

add_filter( 'woocommerce_get_price', function ($price, $productd ) { 
        $logged_in_user_id = get_current_user_id();
        $user_currency = get_user_meta( $logged_in_user_id, 'usd_euro', true );
        $new_price = strtolower("_price_".$user_currency);
        return get_post_meta($productd->id, $new_price, true); 
  }, 10, 2 );