9

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?

fmw42
  • 46,825
  • 10
  • 62
  • 80
Ashish Patel
  • 3,551
  • 1
  • 15
  • 31
  • Check this one https://stackoverflow.com/a/27166704/4778809 – Vikas Yadav Jul 13 '18 at 08:24
  • @VikasYadav: above link is for programatically adding discount into cart, i am already applying coupon from check out page, at that time coupon is applying to tax as well, which i don't want to do. – Ashish Patel Jul 13 '18 at 10:09
  • This is simply too complex (or broad), because you can't use just one hook for that as there is many coupons kind that are calculated in different ways (so taxes can be set in items lines or in totals lines). Also you should have to find the correct hooks related to cart and also to order objects. So this seem to be really huge. Automatic/Woocommerce should have implemented an option (just has Fee API) to get them with or without taxes, but unfortunately it's not the case. – LoicTheAztec Jul 14 '18 at 09:54
  • For the Fee Api when used as a discount (a negative fee), as you have seen, the taxes argument does work for a negative fee, as it applies the discount to the taxes, but it's much more easier to tweak… – LoicTheAztec Jul 14 '18 at 10:01
  • I edited your question for clarity, it is pending approval. Please note that you had `function.php` when it should be `functions.php`. That is the correct filename you should use and I wanted to make sure that you were aware of that and also that is what you have. – domdambrogia Jul 19 '18 at 17:21
  • 1
    Have you looked at the woocommerce documentation? It states that the coupons are applied before taxes. https://docs.woocommerce.com/document/coupon-management/ – domdambrogia Jul 19 '18 at 17:24

3 Answers3

1

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

0

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!

0

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();
}
Rajeev Singh
  • 1,724
  • 1
  • 6
  • 23