6
votes

When a user is on my woocommerce store checkout, I want to set the tax to 0 if they choose a certain payment gateway like "paypal_pro", "cheque", "bacs", or in my case "wdc_woo_credits". Which is a woocredits plugin that allows users to pay with credits instead of a credit card.

I know this function sets the taxes correctly because when I print_r($cart_object) I see I set all the taxes to 0, but yet the checkout still applies the tax in the total.

I think I need a function that recalculates the taxes after I set it using the function below.

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>';
}

This function detects what payment selection they choose and re-runs the update checkout function. But yet the tax is still in the total.

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
}
1

1 Answers

4
votes

After lots of googling, i found out that I don't need to mess with the cart totals at all. I need to alter the current logged in customer.

I can do it with this function which removes tax for the current logged in user.

WC()->customer->set_is_vat_exempt( true );

all this goes in functions.php. You still need the woocommerce_review_order_before_payment function from above which triggers the ajax. So here's the complete code to hide tax on a certain payment gateway for only logged in users.

// 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
}