This is the season of discounts. Every WooCommerce store is offering discounts of all description to take advantage of the holiday craze.
However, displaying the total price and the discount could pose some difficulties for WooCommerce Single Product pages and product listings. In many cases, the price breakdown is present at the bottom of the page. However, the developers could easily present an improved breakdown that displays the price after discounts and the discounted amount.
New Customer? Claim Your 30% Off Cloudways Promo Code
Welcome to Cloudways, where your website finds the perfect hosting solution. Claim your promo code and get 30% off for 3 months on all plans!
The following code snippet will add this functionality to the WooCommerce stores. It is important to note that with appropriate modifications, this snippet could be used to display other information such as:
- Total Sales
- Total Orders
- Total of All Shipping Costs
- Total of All Discounts Used
- Total of All Items Ordered
- Total Sales Per Item
Put the following snippet in the functions.php:
function cw_discount() { global $woocommerce; $cw_discount = 0; foreach ( $woocommerce->cart->get_cart() as $cw_cart_key => $values) { $_product = $values['data']; if ( $_product->is_on_sale() ) { $regular_price = $_product->get_regular_price(); $sale_price = $_product->get_sale_price(); $discount = ($regular_price - $sale_price) * $values['quantity']; $cw_discount += $discount; } } if ( $cw_discount > 0 ) { echo '<tr class="cart-discount"> <th>'. __( 'Secure', 'woocommerce' ) .'</th> <td data-title=" '. __( 'Secure', 'woocommerce' ) .' ">' . wc_price( $cw_discount + $woocommerce->cart->discount_cart ) .'</td> </tr>'; } } add_action( 'woocommerce_cart_totals_after_order_total', 'cw_discount'); add_action( 'woocommerce_review_order_after_order_total', 'cw_discount');
Here is is how the output of the snippet would look like. As you could see, the discount is shown on a separate line.
Conclusion
In this article I have discussed how to display total discount at WooCommerce cart page. with some modifications, you could display several other price attributes as well. If you need help with the code, do leave a comment below.
Owais Alam
is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at [email protected]