Home Adding a custom cart negative fee tax issue in woocommerce
Reply: 1

Adding a custom cart negative fee tax issue in woocommerce

Surjan Raghuwanshi
Surjan Raghuwanshi Published in 2018-01-12 23:22:57Z

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 Reply to 2018-01-14 18:46:07Z

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…

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.383116 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO