1

I have a shipping discount in my woocommerce site, I would like to show it on cart page and checkout page, for that I used add_fee() method.

WC()->cart->add_fee( 'Shipping Discount', -20, false );

It subtract 20 from total amount, but when I go to check order in orders inside admin it gives discount on tax too according to tax rate I configured. Is there any alternative to add discount

How can I do this in woocommerce?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

1

For negative cart fee there is a bug related to taxes as they are applied on all tax classes even if the it's not taxable.

Now you can make a custom global cart discount This way where you will be able to define the type of discount you want, the discount amount and the source price to be discounted.

This code will display the he correct cart discounted gran total

Once submitted this discounted gran total will be passed to the order.

The code:

// Apply a discount globally on cart
add_filter( 'woocommerce_calculated_total', 'discounted_calculated_total', 10, 2 );
function discounted_calculated_total( $total, $cart ){
    $amount_to_discount = $cart->cart_contents_total;
    $percentage = 10; // 10% of discount
    $discounted_amount = $amount_to_discount * $discount / 100;

    $new_total = $total - $discounted_amount;
    return round( $new_total, $cart->dp );
}

Code goes in function.php file of the active child theme (or active theme).

Tested and works.

You should need to make some additional code to display the discount amount (in cart and checkout pages and order-view) and to pass it in the order as meta data and so on…

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • It is working but it is giving discount on tax too. For example the discount is 20 , order total is 100 and tax rate is 10% . Now after discount the total should be 80 ,but it is calculating tax discount on 20 that is 2 so total becomes 78. – Surjan Raghuwanshi Jan 13 '18 at 19:07
  • @SurjanRaghuwanshi Did you tried this new code… Some feed back will be appreciated. Thanks – LoicTheAztec Jan 17 '18 at 01:01