New dynamic data to display cart profit

Hi,

It would be great if we could display in users cart the total profit they made from the products in their cart.
For example, a user has two products in their cart. The price of one of them has been reduced from $45 to $35 on sale. So the user will get $10 from the purchase and it will be great to display it to users.

Screenshot_2

Hi @HOSEIN

This is possible with echo function (PHP). You have to calculate the discount sum, and then you can display it via dynamic data (echo) on the cart and checkout page.

If you’re using Gutenberg, then the savings are displayed on the checkout page (Cart page maybe, too).

Hi @HOSEIN

You can use this shortode :slight_smile:

function get_total_discount_cart_checkout() {   
   $discount_total = 0;  
   foreach ( WC()->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();
         $discount = ( (float)$regular_price - (float)$sale_price ) * (int)$values['quantity'];
         $discount_total += $discount;
      }
   }          
   if ( $discount_total > 0 ) {
      return wc_price( $discount_total + WC()->cart->get_discount_total() );
   }
   return '';
}

// Custom Shortcode
add_shortcode( 'get_total_discount', 'get_total_discount_shortcode' );

function get_total_discount_shortcode() {
   return get_total_discount_cart_checkout();
}
2 Likes

Hi @Lukas,

Thanks for sharing the great code🙏

I also used the function at first, but the problem is that when the shopping cart is updated, the value is not updated.

1 Like

Hi @HOSEIN

If you need further help with anything else, pls let me know - I’m happy to help :wink:

Hi @Lukas,

Thank you, my friend🙏