doudi1750 2019-05-20 21:43
浏览 56
已采纳

通过WooCommerce中的钩子有条件地改变产品税类

I have implemented a b2b area into my WooCommerce Shop about 18 months ago. Recently I have updated all plugins and wordpress itself. Switching the tax class on variable products doesn't work anymore. The code below worked so far, but stopped working. What am I missing?

add_filter('woocommerce_product_get_price', 'switch_price', 99, 2);
add_filter('woocommerce_product_variation_get_price', 'switch_price', 99, 2);
function switch_price($price, $product){

    if(isset($_COOKIE["customerType"])){
      if($_COOKIE["customerType"] == "business"){
          $product->set_tax_class("Zero Rate");
      }
    }

    return $price;
}
  • 写回答

1条回答 默认 最新

  • dongrou2920 2019-05-21 10:43
    关注

    To make it work, you will better target the WC_Product method get_tax_class() through dedicated related composite hooks, this way:

    add_filter('woocommerce_product_get_tax_class', 'switch_product_tax_class', 100, 2 );
    add_filter('woocommerce_product_variation_get_tax_class', 'switch_product_tax_class', 100, 2 );
    function switch_product_tax_class( $tax_class, $product ){
        if( isset($_COOKIE["customerType"]) && $_COOKIE["customerType"] == 'business' ){
            return "Zero Rate";
        }
        return $tax_class;
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and works.


    Based on WC_Customer is_vat_exempt property, you could also try to use the following instead:

    add_action( 'template_redirect', 'vat_exempt_b2b_customers' );
    function vat_exempt_b2b_customers() {
        if( isset($_COOKIE["customerType"]) && $_COOKIE["customerType"] === 'business' 
        && ! WC()->customer->is_vat_exempt() ){
            WC()->customer->set_is_vat_exempt( true );
        }
    }
    

    Code goes in functions.php file of your active child theme (or active theme).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题