'Apply woocommerce coupon only to the cart total, not to the tax line

I am working on a WooCommerce site. When I apply a coupon on the checkout page, it automatically applies to the tax as well.

I only want the coupon code to apply to the cart total. I have searched through google for a related hook or plugin, but can not find a working solution.

This is my current code hook in my functions.php, but it doesn't work as expected.

add_action('woocommerce_product_tax_class', 'set_tax_class');
function set_tax_class () { 
    if (!empty($woocommerce->cart->applied_coupons)){
        $tax_class = 'gratuty';
    }
    return $tax_class;
}

I have also tried the woocommerce_cart_calculate_fees hook too, but it did not work.

Which hook should I use to update cart, when coupon is applied, without changing the tax?



Solution 1:[1]

I believe you have to check out Tax in WooCommerce >> Settings >> Tax. In this, you have to select checkbox with "No, I will enter prices exclusive of Tax".

Also, check out following the link to know more about Taxes: https://github.com/woocommerce/woocommerce/wiki/How-Taxes-Work-in-WooCommerce

Or Apart from that, you can also understand this code to Apply a Coupon Programmatically:

function apply_matched_coupons(){
    $coupon_code = 'freeweek'; 
    if ( WC()->cart->has_discount( $coupon_code ) ) return;     
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        // this is your product ID
        $autocoupon = array( 40 );
        if( in_array( $cart_item['product_id'], $autocoupon ) ) {   
            WC()->cart->add_discount( $coupon_code );
            wc_print_notices();
        }
    }
}

Hope this helps!

Solution 2:[2]

You can add coupon code automatedly on the cart and checkout on both pages. Suppose customer Add product to the cart but does not open the cart page and directly move shop page to checkout page then issue with an auto-apply coupon for cart page

So you should call both hooks (cart page and checkout page)

/**
 * Apply a Coupon Programmatically 
 * Add coupon when the user views the cart page.
 * Add coupon when the user views checkout page.
 */

// Add coupon when user views cart page.
add_action('woocommerce_before_cart_table', 'woo_apply_matched_coupons');

// Add coupon when user views checkout page.
add_action('woocommerce_before_checkout_form', 'woo_apply_matched_coupons');

function woo_apply_matched_coupons() {
    global $woocommerce;

    $coupon_code = 'ABCD125467';   // your coupon code

    // If coupon not Applied!.
    if (!$woocommerce->cart->add_discount(sanitize_text_field($coupon_code))) {
        wc_print_notices();
    } 

    // Manually recalculate totals.  If you do not do this, a refresh is required before the user will see updated totals when the discount is removed.
    $woocommerce->cart->calculate_totals();
}

Solution 3:[3]

wo-commerce has already option to apply the coupon on cart totallike this

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sukhjeet ToorWebRabbits
Solution 2 Rajeev Singh
Solution 3 Syed Israr Ahmad