【问题标题】:Change checkout currency based on the product category根据产品类别更改结帐货币
【发布时间】:2020-10-24 10:04:01
【问题描述】:

我需要根据产品类别更改购物车和结帐货币。因此,如果类别是“服装”,那么购物车和结账时的货币应该是欧元(并且费用需要以欧元进行)。否则,如果产品没有附加“服装”类别,则保持默认美元货币。

这段代码基本上改变了“服装”类别的默认货币,但仅在产品页面上,在购物车和结帐时没有更改。我需要在购物车和结帐时更改它,并且收费需要以该货币发生。

add_filter('woocommerce_currency', 'change_existing_currency_symbol', 10, 2);
function change_existing_currency_symbol( $currency ) {
global $post, $product;

if ( has_term( 'clothing', 'product_cat' ) ) {

return 'EUR';
}else{
return $currency;
}
}

我了解购物车和结帐仅允许每种产品使用一种货币。因此,设置一些限制以允许每个购物车只允许一种产品可以这样发生:

add_filter( 'woocommerce_add_to_cart_validation', 'bbloomer_only_one_in_cart', 99, 2 );
   
function bbloomer_only_one_in_cart( $passed, $added_product_id ) {
   wc_empty_cart();
   return $passed;
}

【问题讨论】:

  • 如果卡上有“服装”产品和非“服装”产品,它需要如何反应?
  • 通过限制 - 每个购物车只有一个产品,因为 Woocommerce 购物车只能支持一种货币。
  • add_filter( 'woocommerce_add_to_cart_validation', 'bbloomer_only_one_in_cart', 99, 2 ); function bbloomer_only_one_in_cart( $passed, $added_product_id ) { wc_empty_cart(); return $passed; }
  • 您是否在 woocommerce 设置中安装并激活了两种货币?

标签: php wordpress woocommerce hook-woocommerce


【解决方案1】:

我进行了一些研究,发现在按类别或按产品动态更改货币时还有很多额外的复杂性。

我假设购物车仍需要计算总数,并提供 1 种结帐货币。这意味着,总数也必须进行转换。

通过this plugin's 代码可以找到您的最佳答案...但我警告您,这比更改符号要复杂得多。

【讨论】:

    【解决方案2】:

    也许,这是一个解决方案(我无法尝试):

    add_filter('option_woocommerce_currency', function ($currency) {
        // is current page is product-single
        if(is_single('product')) {
            return has_term('clothing', 'product_cat') ? 'EUR' : $currency;
        }
        // for every else pages
        global $woocommerce;
        foreach($woocommerce->cart->get_cart() as $product) {
            if (has_term('clothing', 'product_cat', $product['product_id'])) {
                return 'EUR';
            }
        }
        return $currency;
    }, 10, 1);
    

    PS:它不会在产品列表页面上完成这项工作。

    【讨论】:

    • 谢谢。我刚刚对此进行了测试-它成功地将购物车上的货币更改为欧元。但是当我继续结帐时,我可以看到它恢复为美元。有什么方法可以在结帐时进行设置?
    • 我已将答案更新为使用option_${option} filter
    • 谢谢你,但这没有用。结帐货币仍为美元。
    • 如果您在过滤器的开头直接输入return 'EUR';,结帐是否在EUR
    • 我现在遇到了 Wordpress 严重错误。不知道为什么,因为您的代码通过语法检查器没有问题。
    【解决方案3】:
    add_filter('woocommerce_currency', 'change_existing_currency_symbol', 10, 2);
    function change_existing_currency_symbol( $currency ) {
    global $post, $product;
    $defaultCurrency = 'EUR';
    $categoriesCurrencies = [ 
              'audio'=>'EUR', 
              'mobile'=>'LEK'
    ];
     return $categoriesCurrencies[$product->category] ?: $defaultCurrency;
    }
    

    【讨论】:

      【解决方案4】:

      我认为这是您问题的答案,尽管它不是推荐的方式,因为它不适用于产品列表页面。请尝试使用此代码并告诉我您的想法。

      add_filter('woocommerce_currency', 'change_existing_currency_symbol', 10, 1);
      function change_existing_currency_symbol( $currency ) {
      
          if ( is_product() ) {
              if ( has_term( 'clothing', 'product_cat' ) ) return 'EUR';
          } else if ( is_cart() || is_checkout() ) {
              global $woocommerce;
              foreach ( $woocommerce->cart->get_cart() as $product ) {
                  if ( has_term('clothing', 'product_cat', $product['product_id'] ) ) return 'EUR';
              }
          }
      
          return $currency;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-19
        相关资源
        最近更新 更多