根据用户付款选择 woocommerce 设置结帐税

bgh*_*use 6 wordpress woocommerce hook-woocommerce

当用户在我的 woocommerce 商店结帐时,如果他们选择某个支付网关,例如“paypal_pro”、“check”、“bacs”或在我的情况下是“wdc_woo_credits”,我想将税设置为 0。这是一个 woocredits 插件,允许用户使用信用而不是信用卡付款。

我知道这个函数正确地设置了税,因为当我 print_r($cart_object) 我看到我将所有的税设置为 0,但结帐仍然应用了总数中的税。

我想我需要一个在使用下面的函数设置后重新计算税收的函数。

add_action( 'woocommerce_cart_calculate_fees','shipping_method_discount', 20, 1 );
function shipping_method_discount( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    $chosen_payment_method = WC()->session->get('chosen_payment_method');

    //if( $chosen_payment_method == 'cheque' ){
    if( $chosen_payment_method == 'wdc_woo_credits' ){
        $cart_object->set_cart_contents_taxes(0);
        $cart_object->set_cart_contents_tax(0);
        $cart_object->set_subtotal_tax(0);
        $cart_object->set_shipping_tax(0);
        $cart_object->set_total_tax(0);
        $cart_object->set_fee_tax(0);
        $cart_object->set_fee_taxes(0);
        $cart_object->set_subtotal_tax(0);
        foreach($cart_object->cart_contents as $product){
            $cart_object->cart_contents[$product['key']]['line_tax'] = 0;
            $cart_object->cart_contents[$product['key']]['line_subtotal_tax'] = 0;
            $cart_object->cart_contents[$product['key']]['line_tax_data']['subtotal'] = 0;
            $cart_object->cart_contents[$product['key']]['line_tax_data']['total'] = 0;
        }
    }
   //echo '<pre>'; print_r($cart_object); echo '</pre>';
}
Run Code Online (Sandbox Code Playgroud)

此功能检测他们选择的付款选择并重新运行更新结帐功能。但税收仍然在总数中。

add_action( 'woocommerce_review_order_before_payment', 'refresh_payment_methods' );
function refresh_payment_methods(){
    // jQuery code
    ?>
    <script type="text/javascript">
        (function($){
            $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
                $('body').trigger('update_checkout');
            });
        })(jQuery);
    </script>
    <?php
}
Run Code Online (Sandbox Code Playgroud)

bgh*_*use 4

经过大量谷歌搜索后,我发现我根本不需要弄乱购物车总数。我需要更改当前登录的客户。

我可以使用此功能来完成此操作,该功能可以为当前登录的用户免除税费。

WC()->customer->set_is_vat_exempt( true );
Run Code Online (Sandbox Code Playgroud)

所有这些都在functions.php 中。您仍然需要上面的 woocommerce_review_order_before_ payment 函数来触发 ajax。下面是仅对登录用户隐藏特定支付网关税费的完整代码。

// calculate fees on checkout page
add_action( 'woocommerce_cart_calculate_fees','shipping_method_discount', 20, 1 );
function shipping_method_discount( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    $chosen_payment_method = WC()->session->get('chosen_payment_method');

    //if( $chosen_payment_method == 'cheque' ){
    if( $chosen_payment_method == 'wdc_woo_credits' ){
        // if user buys with credits, dont allow tax.
        WC()->customer->set_is_vat_exempt( true );
    }else{
        // if user buys with credit card, allow tax
        WC()->customer->set_is_vat_exempt( false );
    }
}

// refresh the checkout page totals once user selects
add_action( 'woocommerce_review_order_before_payment', 'refresh_payment_methods' );
function refresh_payment_methods(){
    // jQuery code
    ?>
    <script type="text/javascript">
        (function($){
            $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
                $('body').trigger('update_checkout');
            });
        })(jQuery);
    </script>
    <?php
}
Run Code Online (Sandbox Code Playgroud)