如果 WooCommerce 购物车页面上没有应用优惠券,则隐藏原始优惠券折扣

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

我将优惠券和折扣总储蓄添加到我的购物车页面。我的代码工作正常,但我还有一个问题。

这是我目前使用的代码:

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>
      ';
    }
}

目前:如果没有使用优惠券,则显示0。

我的问题:如何在显示 0 时隐藏 table 输出?

您可以使用 WC_Cart::get_applied_coupons() 并且当不为空时。

  • 我已经清理了你的代码并稍微重写了它

所以你得到:

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 );