WooCommerce: Slashed Cart Subtotal if Coupon @ Cart

This is a nice snippet to let users know what the original cart amount was by slashing the cart subtotal and showing the cart subtotal after discounts on the same line (Cart totals > subtotal).

You can then hide the coupon code line if you wish!

WooCommerce: show slashed original and discounted subtotal @ Cart

PHP Snippet: show slashed original and discounted subtotal @ WooCommerce Cart

/**
 * @snippet       Cart subtotal slashed if coupon applied @ WooCommerce Cart
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @testedwith    WooCommerce 6
 * @community     https://businessbloomer.com/club/
 */
 
add_filter( 'woocommerce_cart_subtotal', 'bbloomer_slash_cart_subtotal_if_discount', 9999, 3 );

function bbloomer_slash_cart_subtotal_if_discount( $cart_subtotal, $compound, $cart ){
   if ( $cart->get_cart_discount_total() > 0 ) {
		return wc_format_sale_price( $cart->get_subtotal(), $cart->get_subtotal() - $cart->get_cart_discount_total() );
	}
   return $cart_subtotal;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

6 thoughts on “WooCommerce: Slashed Cart Subtotal if Coupon @ Cart

  1. I just want to say thank you for sharing all these useful snippets for free. I learned a lot on how to access WC objects, etc. from your code and your hooks reference pages. I was coding a big project and kept having to look up how to do X and Y, to which the answer seemed to always be, “just use this plugin!”

    No!! No more!!! Plugins just are bloat and want to give me stupid popups I keep dismissing to sell me crap I don’t want. I’ve become more confident in coding things for myself thanks to looking at these snippets so I don’t have to deal with that nonsense every time I need simple functionality. Thank you very much!

  2. Hi Rodolfo, very nice thank you! It doesn’t consider the savings from the bundles. A little update would be great 🙂

    Regards,
    Marc

    1. Hello Marc, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. Hi rodolfo,

    Thank you for this snippet to display cart sub total at front end, Is there any way to do same at backend. When someone applied a coupon to order. its not displaying sub total. only displaying discount amount, tax and total amount after discount.

    Discount: $12.00
    Shipping: $0.00
    Order total: $12.00

    1. Hey Sunny, thanks for your comment! Yes, I’m sure this is possible. The function would be similar but you need to find the “hook” that corresponds to the order total in the admin 🙂

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *