How to display the total price and total discount on the shopping cart page?

I can not understand how on the shopping cart page to get a General discount for all products ?
5e10f3fdca2a7276613543.png

So it displays 2 values, and how to display the total discount ?

$items = WC()->cart->get_cart();

foreach($items as $item => $values) {
 $price_sale = $values['data']->get_sale_price();
}
April 4th 20 at 00:48
1 answer
April 4th 20 at 00:50
Solution
Damn well Google is full, here's an example tutorial about all the options
https://jeroensormani.com/showing-sale-prices-cart...
I have your website doesn't open - Tyrel_Kub commented on April 4th 20 at 00:53
@Meghan.Bayer, It's not mine, well naglie other - Caterina.Blan commented on April 4th 20 at 00:56
@alivi, I understand that you need to cart-totals.php to create loops ?

They have everything in the foreach loop, like so:

<?php foreach ( WC()->cart->get_coupons() as $code => $coupon ) : ?>
 <div class="cart-discount coupon-<?php echo esc_attr( sanitize_title( $code ) ); ?>">
 <span><?php wc_cart_totals_coupon_label( $coupon ); ?></span>
 <span data-title="<?php echo esc_attr( wc_cart_totals_coupon_label( $coupon, false ) ); ?>"><?php wc_cart_totals_coupon_html( $coupon ); ?></span>
</div>
 <?php endforeach; ?>

<?php foreach ( WC()->cart->get_fees() as $fee ) : ?>
 <div class="fee">
 <span><?php echo esc_html( $fee->name ); ?></span>
 <span data-title="<?php echo esc_attr( $fee->name ); ?>"><?php wc_cart_totals_fee_html( $fee ); ?></span>
</div>
 <?php endforeach; ?>


I throw in a discount calculation:

$items = WC()->cart->get_cart();
<span class="order-info-sale">
 <?php 
 $discount_total = 0;
 foreach ( $items 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();
 $discount = ($regular_price - $sale_price) * $values['quantity'];
 $discount_total += $discount;
 } 
}
 if ( $discount_total > 0 ) { 
 echo number_format($discount_total + WC()->cart->discount_cart, 0, ", ' ') . sprintf( get_woocommerce_currency_symbol() );
}
?>
 </span>
- Tyrel_Kub commented on April 4th 20 at 00:59
@Meghan.Bayer, Yes - Caterina.Blan commented on April 4th 20 at 01:02
@alivi, understand, and optimize a bunch of cycles, is this normal ? - Tyrel_Kub commented on April 4th 20 at 01:05
Table yourself, there may be a single loop to do everything, but can not explain now - Caterina.Blan commented on April 4th 20 at 01:08
@aliviif you do one cycle, then the entire layout and information is duplicated

Because $items = WC()->cart->get_cart() - removes all items from the basket, and if you run the cycle, then there is duplication - Tyrel_Kub commented on April 4th 20 at 01:11
@Meghan.Bayer,
removes all items from the cart

no conclusions, and receives, and how you data rasporyaditel, is another question - Caterina.Blan commented on April 4th 20 at 01:14
@alivi, but is there any similar link that you got rid of me ? - Tyrel_Kub commented on April 4th 20 at 01:17
@Meghan.Bayer, I've sent you the first result from Google - Caterina.Blan commented on April 4th 20 at 01:20
@alivi, and how to write ? )
I was looking for this: sale order cart woocommerce, but nothing really found - Tyrel_Kub commented on April 4th 20 at 01:23
For example, get total sale in cart - Caterina.Blan commented on April 4th 20 at 01:26
@alivi, okay, thank you - I will understand - Tyrel_Kub commented on April 4th 20 at 01:29
@alivi, with shorter cycle I figured out

You can just file cart-totals.php his top start
$discount_total = 0;
$items = WC()->cart->get_cart();
foreach ( $items as $cart_item_key => $values) {
//
}

And there to make some operations, and then incremental in variables and output it in the layout, then dubbing is nowhere to be - Tyrel_Kub commented on April 4th 20 at 01:32
@Meghan.Bayer, Yes - Caterina.Blan commented on April 4th 20 at 01:35

Find more questions by tags WordPressWooCommerce