【问题标题】:Disable tax for company and specific country in WooCommerce checkout在 WooCommerce 结帐中禁用公司和特定国家/地区的税
【发布时间】:2023-04-01 20:38:01
【问题描述】:

在 Woocommerce 结帐中,如果 billing_company 不为空,我会尝试取消税:

这是我的代码

add_action( 'woocommerce_checkout_update_order_review', 'bbloomer_taxexempt_checkout_based_on_zip' );

function bbloomer_taxexempt_checkout_based_on_zip( $post_data ) {
        global $woocommerce;
        $woocommerce->customer->set_is_vat_exempt( false );
        $Pay_options=$_POST['Pay_options'];
        parse_str($post_data);
        global $woocommerce;

        if ( $billing_company != null) $woocommerce->customer->set_is_vat_exempt( true );
}

IF 语句中,我还需要检查billing_country 是否为“克罗地亚”。

我怎样才能做到这一点?

【问题讨论】:

    标签: php wordpress woocommerce checkout country


    【解决方案1】:

    您可以使用以下 (不使用任何额外的 javascript 或 jQuery 代码) 使计费公司结帐字段更新结帐,并检查国家“HR”(克罗地亚) em>:

    add_filter( 'woocommerce_checkout_fields' , 'billing_company_trigger_update_checkout_on_change', 10, 1 );
    function billing_company_trigger_update_checkout_on_change( $fields ) {
    
        $fields['billing']['billing_company']['class'][] = 'update_totals_on_change';
    
        return $fields;
    }
    
    add_action( 'woocommerce_checkout_update_order_review', 'checkout_vat_exempt_based_on_billing_company', 10, 1 );
    function checkout_vat_exempt_based_on_billing_company( $post_data ) {
        parse_str($post_data, $results); // Since Php 7.2, the 2nd argument is recommended in parse_str() function
        extract($results);
    
        $customer = WC()->customer;
    
        // When billing company is filled and country is Croatia: Exempt taxes
        if ( ! empty($billing_company) && $billing_country === 'HR' && ! $customer->is_vat_exempt() ) {
            $customer->set_is_vat_exempt( true );
        }
        elseif ( $customer->is_vat_exempt() ){
            $customer->set_is_vat_exempt( false );
        }
    }
    

    要处理发货国家/地区而不是账单国家/地区,只需将代码中的变量 $billing_country 替换为 $shipping_country

    代码进入您的活动子主题(或活动主题)的 functions.php 文件中。经过测试并且可以工作。

    注意:由于一段时间以来,global $woocommerce$woocommerce 被简单地替换为 WC()

    【讨论】:

    • 感谢您的回答。我正在寻找类似的东西,但我想免除运费。我尝试使用$order = WC()->order;$order->set_shipping_total( 0 ); 更改上面的代码,但我的$order 变量是null.. 请指教!
    【解决方案2】:

    因为在 #billing_company 更改时,没有更新结帐的操作。所以你需要一个javascript来做到这一点

    add_action('woocommerce_after_checkout_form',function($checkout){
        ?>
        <script type="text/javascript">
        jQuery(function($){
            $(document).on('change','#billing_company',function(){
                $(document.body).trigger("update_checkout");
            });
        });
        </script>
        <?php
    });
    

    在 php 中,您可以使用您的代码和用户:$billing_country == 'HR' 来检查克罗地亚国家

    add_action( 'woocommerce_checkout_update_order_review', 'bbloomer_taxexempt_checkout_based_on_zip' );
    
    function bbloomer_taxexempt_checkout_based_on_zip( $post_data ) {
            global $woocommerce;
            $woocommerce->customer->set_is_vat_exempt( false );
            $Pay_options=$_POST['Pay_options'];
            parse_str($post_data);
            if ( $billing_company != null) 
                $woocommerce->customer->set_is_vat_exempt( true );
            if($billing_country == 'HR'){ // Croatia
                // Do what you need, for example set TAX
                $woocommerce->customer->set_is_vat_exempt( false );
            }
    
    }
    

    【讨论】:

    • 是的,我写了 jquery,但我没有复制它。我只需要 billing_country 检查。谢谢
    【解决方案3】:

    我认为,如果您在结帐页面上更新购物车,上述答案只会取消税费。它仍然会在订单确认等方面征税。以下是在所有地方免税的方法:

    
    // from https://elliottrichmond.co.uk/woocommerce-how-to-make-a-user-exempt-from-tax-charges/
    function wooc_user_tax_exempt_callback($wc_tax_enabled) {
        $cart = WC()->cart;
    
        $customer = WC()->customer;
    
        parse_str($post_data);
        // When billing company is filled and country is Croatia: Exempt taxes
        if ( ! empty($billing_company) && $billing_country === 'HR' && ! $customer->is_vat_exempt() ) {
            $wc_tax_enabled = false;
        }
        return $wc_tax_enabled;
    
    }
    add_filter( 'wc_tax_enabled', 'wooc_user_tax_exempt_callback', 10, 1);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      • 2019-07-29
      • 2015-04-09
      • 2018-07-25
      • 2018-05-05
      • 1970-01-01
      • 2017-05-09
      相关资源
      最近更新 更多