Skip to main content

[Thủ Thuật WP] Hiển thị tổng số tiền được giảm giá ở trang thanh toán WordPress

Hiển thị tổng số tiền được giảm giá ở trang thanh toán WordPress là một cách để kích thích nhu cầu mua hàng cho khách hàng cũng như giúp hạn chế tỉ lệ huỷ bỏ đơn hàng cho chủ cửa hàng online.

Dưới đây là code snippet hướng dẫn giúp bạn làm điều đó

Code hiển thị tổng số tiền được giảm giá ở trang thanh toán WordPress

Copy và paste đoạn code sau vào cuối file functions.php của child theme

add_action('woocommerce_cart_totals_after_order_total', 'bbloomer_show_total_discount_cart_checkout', 9999);
add_action('woocommerce_review_order_after_order_total', 'bbloomer_show_total_discount_cart_checkout', 9999);

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

Kêt quả 

Image
Hiển thị tổng số tiền được giảm giá ở trang thanh toán WordPress