This snippet looks at including the total savings as a line item in the slide out mini cart. This isn’t standard WooCommerce functionality but inspired by this eCommerce conversions video I decided to make my own.
The final result should look like this.
Because of the way the mini cart hooks are setup I had to use some order CSS to arrange the “You save” bar at the top.
Add the following code within the Shoptimizer Child theme’s functions.php file or via the Code Snippets plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
if ( ! function_exists( 'shoptimizer_mini_cart_total_discounts' ) ) { /** * */ function shoptimizer_mini_cart_total_discounts() { global $woocommerce; $discount_total = 0; foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { $_product = $values['data']; if ($_product->is_on_sale()) { $regular_price = $_product->get_regular_price(); $sale_price = $_product->get_sale_price(); if (empty($regular_price)){ //then this is a variable product $available_variations = $_product->get_available_variations(); $variation_id=$available_variations[0]['variation_id']; $variation= new WC_Product_Variation( $variation_id ); $regular_price = $variation ->regular_price; $sale_price = $variation ->sale_price; } $discount = ceil(($regular_price - $sale_price) * $values['quantity'] ); $discount_total += $discount; } } if ($discount_total > 0) { ?> <p class="woocommerce-mini-cart__total total discounts-total"> <strong><?php echo esc_html__( 'You save', 'shoptimizer' ); ?></strong> <span class="woocommerce-Price-amount amount">-<?php echo wc_price($discount_total + $woocommerce->cart->discount_cart); ?></span> </p> <style> .widget_shopping_cart p.total.discounts-total { color: green; font-size: 13px; margin-bottom: -1.2em; order: 1; border-top: 1px solid #e2e2e2; } .widget_shopping_cart p.total.discounts-total strong { font-weight: normal; } .shoptimizer-mini-cart-wrap .widget_shopping_cart .discounts-total .amount { margin: 0; font-weight: normal; color: green; } .shoptimizer-mini-cart-wrap .widget_shopping_cart .discounts-total .amount bdi { color: green; } .widget_shopping_cart p.total { order: 2; border: none; } .widget_shopping_cart p.buttons { order: 3; } .shoptimizer-mini-cart-wrap .cart-drawer-below { order: 4; } </style> <?php } } } add_action( 'woocommerce_widget_shopping_cart_before_buttons', 'shoptimizer_mini_cart_total_discounts', 10); |