Display You Saved in Cart Totals and Order Review Row
Display "You saved" message in cart totals and order review row.
// Show You Saved (Price) in Cart Totals and Order Review Row
if (!function_exists('woo_discount_rules_show_you_saved_cart_totals_row')) {
add_action('woocommerce_cart_totals_after_order_total', 'woo_discount_rules_show_you_saved_cart_totals_row', 9999);
add_action('woocommerce_review_order_after_order_total', 'woo_discount_rules_show_you_saved_cart_totals_row', 9999);
function woo_discount_rules_show_you_saved_cart_totals_row() {
$discount_total = 0;
// Discount total label in cart and checkout page
$display_text = __('You Saved', 'woo-discount-rules');
// Calculate default discount price total
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();
if(!empty($sale_price)){
$discount = ($regular_price - $sale_price) * $values['quantity'];
$discount_total += $discount;
}
}
}
// Add Woo Discount Rules discount price total
if (class_exists('\Wdr\App\Router')) {
$manage_discount = \Wdr\App\Router::$manage_discount;
if (method_exists($manage_discount, 'getDiscountPerItem')) {
if (!empty($manage_discount::$calculated_cart_item_discount)) {
foreach ($manage_discount::$calculated_cart_item_discount as $discount_details) {
if (!empty($discount_details)) {
$discount_total += $manage_discount->getDiscountPerItem($discount_details);
}
}
}
}
}
// Add Cart discount total
$discount_total += WC()->cart->get_discount_total();
if ($discount_total > 0) {
echo '<tr><th>' . $display_text . '</th><td data-title="' . $display_text . '">' . wc_price($discount_total) . '</td></tr>';
}
}
}