在 WooCommerce 结帐中禁用公司和特定国家/地区的税收

在 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是“克罗地亚”。


我怎样才能做到这一点?

http://img3.mukewang.com/60cdabf10001326206420515.jpg

红糖糍粑
浏览 233回答 3
3回答

SMILET

您可以使用以下内容(不使用任何额外的 javascript 或 jQuery 代码)使账单公司结帐字段更新结帐,并检查国家“HR” (克罗地亚):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()

红颜莎娜

因为在 #billing_company change 上,没有更新结帐的操作。所以你需要一个javascript来做到这一点add_action('woocommerce_after_checkout_form',function($checkout){&nbsp; &nbsp; ?>&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; jQuery(function($){&nbsp; &nbsp; &nbsp; &nbsp; $(document).on('change','#billing_company',function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(document.body).trigger("update_checkout");&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });&nbsp; &nbsp; </script>&nbsp; &nbsp; <?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 ) {&nbsp; &nbsp; &nbsp; &nbsp; global $woocommerce;&nbsp; &nbsp; &nbsp; &nbsp; $woocommerce->customer->set_is_vat_exempt( false );&nbsp; &nbsp; &nbsp; &nbsp; $Pay_options=$_POST['Pay_options'];&nbsp; &nbsp; &nbsp; &nbsp; parse_str($post_data);&nbsp; &nbsp; &nbsp; &nbsp; if ( $billing_company != null)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $woocommerce->customer->set_is_vat_exempt( true );&nbsp; &nbsp; &nbsp; &nbsp; if($billing_country == 'HR'){ // Croatia&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Do what you need, for example set TAX&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $woocommerce->customer->set_is_vat_exempt( false );&nbsp; &nbsp; &nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP