Skip to content
Advertisement

Hide coupon discount raw if no coupon has been applied on WooCommerce cart page

I add coupons and discounts total savings to my cart page. My code works fine but I have an additional question.

This is the code I am currently using:

add_action( 'woocommerce_cart_totals_before_shipping', 'show_total_discount_cart_checkout', 9999 );
 
function show_total_discount_cart_checkout() {
   
   global $woocommerce;
   $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 = ( $regular_price - $sale_price ) * $values['quantity'];
         $regular_total = $regular_price * $values['quantity'];
         $discount_total += $discount;
      }
   }
             
    if ( $discount_total > 0 ) {
      echo '
      
      <tr><th colspan="2">You will save '.     wc_price( $woocommerce->cart->subtotal - $woocommerce->cart->total + $woocommerce->cart->shipping_total*1.23 + $discount_total ) .'</td></tr>
      <tr><th class="smalltl">Price normal</th><td data-title="Poupa" class="smalltot">'.     wc_price( $regular_total ) .'</td></tr>
      <tr><th class="smalltl">Dicsount Pack</th><td data-title="Poupa" class="smalltot">-'.     wc_price( $discount_total ) .'</td></tr>  
      <tr><th class="smalltl">Discount Cupon</th><td data-title="Poupa" class="smalltot">-'.     wc_price( $woocommerce->cart->subtotal - $woocommerce->cart->total + $woocommerce->cart->shipping_total*1.23 ) // need to hide this tr when there i no coupon applied  
.'</td></tr>
      ';
    }
}

Currently: if no coupons are applied, it shows 0.

My question: How can I hide the table output when 0 is displayed?

Advertisement

Answer

You could use WC_Cart::get_applied_coupons() and when NOT empty.

  • I’ve cleaned up your code and rewritten it a little bit

So you get:

function show_total_discount_cart_checkout() {
    // Counter
    $discount_total = 0;
    
    // WC Cart
    if ( WC()->cart ) {
        // Get cart
        $cart = WC()->cart;
        
        // If cart is NOT empty
        if ( ! $cart->is_empty() ) {
            // Loop through cart items and calculate total volume
            foreach( $cart->get_cart() as $cart_item ) {
                // Get product
                $product = $cart_item['data'];
                
                // On sale
                if ( $product->is_on_sale() ) {
                    // Getters
                    $regular_price = $product->get_regular_price();
                    $sale_price = $product->get_sale_price();
                    $quantity = $cart_item['quantity'];
                    
                    // Calculations
                    $discount = ( $regular_price - $sale_price ) * $quantity;
                    $regular_total = $regular_price * $quantity;
                    $discount_total += $discount;
                }
            }
        }
    }
    
    // Greater than
    if ( $discount_total > 0 ) {
        // Getters
        $subtotal = $cart->get_subtotal();
        $total = $cart->total;
        $shipping_total = $cart->shipping_total;
        
        // Output
        echo '<tr><th colspan="2">You will save ' . wc_price( $subtotal - $total + $shipping_total * 1.23 + $discount_total ) . '</td></tr>';
        echo '<tr><th class="smalltl">Price normal</th><td data-title="Poupa" class="smalltot">' . wc_price( $regular_total ) . '</td></tr>';
        echo '<tr><th class="smalltl">Dicsount Pack</th><td data-title="Poupa" class="smalltot">-' . wc_price( $discount_total ) . '</td></tr>';
        
        // Get applied coupons
        $coupon_applieds = $cart->get_applied_coupons();
        
        // NOT Empty coupon applieds
        if ( ! empty ( $coupon_applieds ) ) {
            echo '<tr><th class="smalltl">Discount Cupon</th><td data-title="Poupa" class="smalltot">-' . wc_price( $subtotal - $total + $shipping_total * 1.23 ) . '</td></tr>';
        }
    }
}
add_action( 'woocommerce_cart_totals_before_shipping', 'show_total_discount_cart_checkout', 10, 0 );
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement