Skip to main content

[Thủ Thuật WordPress] Hiển thị tổng số tiền tiết kiệm được trên Cart và Checkout của WooCommerce

Hiển thị tổng số tiền tiết kiệm được trên Cart và Checkout của WooCommerce là để giúp khách hàng thuận tiện hơn bằng cách tính tổng số tiền khách tiết kiệm được từ đợt Sale, giảm giá, discount ... từ đơn hàng của khách thì đoạn snippet này sẽ giúp bạn làm điều đó một cách dễ dàng.

Thêm code bên dưới file functions.php của child theme.

add_action( 'woocommerce_cart_totals_after_order_total', 'devwk_show_total_discount_cart_checkout', 9999 );
add_action( 'woocommerce_review_order_after_order_total', 'devwk_show_total_discount_cart_checkout', 9999 );
 
function devwk_show_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 = ( $regular_price - $sale_price ) * $values['quantity'];
         $discount_total += $discount;
      }
   }
             
    if ( $discount_total > 0 ) {
      echo '<tr><th>You Saved</th><td data-title="You Saved">' . wc_price( $discount_total + WC()->cart->get_discount_total() ) .'</td></tr>';
    }
  
}