【问题标题】:How to get Tax amount programatically when user is Tax Exempt in WooCommerce?当用户在 WooCommerce 中免税时,如何以编程方式获取税额?
【发布时间】:2020-06-18 00:38:32
【问题描述】:

我正在使用我的插件创建免税规则,以免除某些用户纳税,通过使用

$customer->set_is_vat_exempt(true);

效果很好。然而,一些用例要求用户在购物车/结账时支付税款,即使整个网站的价格显示应该是免税的。

因此,如果不是免税的话,我正在尝试为正常的税额添加费用。我正在尝试使用"woocommerce_cart_calculate_fees" 执行此操作,但我怎样才能获得正常的购物车总税额?我尝试过的每个功能都给了我豁免版本。

谢谢。

【问题讨论】:

    标签: wordpress woocommerce hook-woocommerce


    【解决方案1】:

    这与干净高效的代码相反,但至少它有效:

    $tax_vat = 0;
    $tax_name = 'VAT';
                        $cart = WC()->cart;
                        foreach($cart->get_cart() as $item){
                            //Get product by supplying variation id or product_id
                            $product = wc_get_product( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] );
                            if ($product->is_taxable()){
                                $tax_rates = WC_Tax::get_rates( $product->get_tax_class() );
                                if (!empty($tax_rates)) {
                                    $tax_rate = reset($tax_rates);
                                    $tax_vat += (wc_get_price_excluding_tax($product) * $tax_rate['rate'] * $item['quantity'])/100;
                                    $tax_name = $tax_rate['label'];
                                }
                            }
                        }
    
                        $cart->add_fee( $tax_name, $tax_vat);
    

    【讨论】:

      猜你喜欢
      • 2021-01-01
      • 2021-05-03
      • 2020-09-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多