Apply woocommerce coupon only to the cart total, not to the tax line
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!