Chuyển % giảm giá thành số tiền được giảm cho Woocommerce: Trong các mã nguồn để sử dụng làm các website bán hàng thì WordPress có thể là mã nguồn được dùng nhiều nhất. Plugin Woocommerce sẽ giúp bạn tạo cửa hàng online rất dễ dàng. Khi bạn có sản phẩm giảm giá mà bạn không muốn nó hiển thị % được giảm mà muốn hiển thị luôn số tiền được giảm thì bài viết này là dành cho bạn.

Hướng dẫn chuyển % giảm giá thành số tiền được giảm cho Woocommerce
Sẽ có 3 cách hiển thị mà bạn có thể muốn hiển thị ra ngoài là: chỉ hiện thị số tiền được giảm, chỉ hiển thị % được giảm và hiển thì cả số tiền và % được giảm. Bài viết này sẽ hướng dẫn cả 3 cách các bạn có thể tùy ý lựa chọn cách phù hợp với mình
Có thể bạn quan tâm: Hướng dẫn wordpress
1] Chuyển % giảm giá thành số tiền được giảm
Bạn copy đoạn code bên dưới vào functions của theme bạn đang dùng.
add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );
function change_displayed_sale_price_html( $price, $product ) {
// Only on sale products on frontend and excluding min/max price on variable products
if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){
// Get product prices
$regular_price = (float) $product->get_regular_price(); // Regular price
$sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)
// "Saving price" calculation and formatting
$saving_price = wc_price( $regular_price - $sale_price );
// Append to the formated html price
$price .= sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $saving_price );
}
return $price;
}
2] Chuyển % giảm giá thành % được giảm
Bạn copy đoạn code bên dưới vào vẫn cho vào file functions của theme bạn đang dùng.
add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );
function change_displayed_sale_price_html( $price, $product ) {
// Only on sale products on frontend and excluding min/max price on variable products
if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){
// Get product prices
$regular_price = (float) $product->get_regular_price(); // Regular price
$sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)
// "Saving Percentage" calculation and formatting
$precision = 1; // Max number of decimals
$saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';
// Append to the formated html price
$price .= sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $saving_percentage );
}
return $price;
}
3] Chuyển % giảm giá thành số tiền và % được giảm
add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );
function change_displayed_sale_price_html( $price, $product ) {
// Only on sale products on frontend and excluding min/max price on variable products
if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){
// Get product prices
$regular_price = (float) $product->get_regular_price(); // Regular price
$sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)
// "Saving price" calculation and formatting
$saving_price = wc_price( $regular_price - $sale_price );
// "Saving Percentage" calculation and formatting
$precision = 1; // Max number of decimals
$saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';
// Append to the formated html price
$price .= sprintf( __('<p class="saved-sale">Save: %s <em>(%s)</em></p>', 'woocommerce' ), $saving_price, $saving_percentage );
}
return $price;
}
Chỉ có thế thôi các bạn lựa chọn theo nhu cầu của mình nhé, nếu bạn muốn nó đẹp hơn thì thêm cho nó chút CSS vào là đc nhé.
Demo: Bấm vào đây
Kết luận
Trên đây là hướng dẫn chuyển % giảm giá thành số tiền được giảm cho Woocommerce mà bạn có thể tham khảo. Hy vọng với hướng dẫn mà Blog thủ thuật máy tính f4vnn đã chia sẻ sẽ có ích với bạn, chúc bạn thành công.
