• Skip to main content
  • Bỏ qua primary sidebar
  • Trang chủ
  • Tút
    • Directadmin
    • WordPress
  • Code
    • Account
    • License
  • Service
  • Template
    • Open Cart
    • WordPress
      • Website doanh nghiệp
      • Website bán mỹ phẩm
      • Website bất động sản
    • Miễn phí
  • VPS
  • Check IP Location

Việt Beginner

Chia sẻ cho những ai cần đọc

Bạn đang ở:Trang chủ / Tút / Wordpress / Display the discount percentage on the sale badge

Display the discount percentage on the sale badge

04/07/2019 by Mcode Để lại bình luận

Hiển thị phần trăm giảm giá trên sale badge woocommerce

Display the discount percentage on the sale badge 1

Chỉ cần add code sau vào functions.php của theme đang dùng.

add_filter( 'woocommerce_sale_flash', 'add_percentage_to_sale_badge', 20, 3 );
function add_percentage_to_sale_badge( $html, $post, $product ) {
    if( $product->is_type('variable')){
        $percentages = array();

        // Get all variation prices
        $prices = $product->get_variation_prices();

        // Loop through variation prices
        foreach( $prices['price'] as $key => $price ){
            // Only on sale variations
            if( $prices['regular_price'][$key] !== $price ){
                // Calculate and set in the array the percentage for each variation on sale
                $percentages[] = round(100 - ($prices['sale_price'][$key] / $prices['regular_price'][$key] * 100));
            }
        }
        // We keep the highest value
        $percentage = max($percentages) . '%';
    } else {
        $regular_price = (float) $product->get_regular_price();
        $sale_price    = (float) $product->get_sale_price();

        $percentage    = round(100 - ($sale_price / $regular_price * 100)) . '%';
    }
    return '<span class="onsale">' . esc_html__( 'SALE', 'woocommerce' ) . ' ' . $percentage . '</span>';
}

Thuộc chủ đề:Wordpress Tag với:sale badge, woocommerce

Reader Interactions

Trả lời Hủy

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Sidebar chính

THIẾT KẾ WEBSITE UY TÍN

Thẻ

bài viết liên quan cloudlinux cloud storage cpanel csf firewall cài đặt cloudlinux cài đặt directadmin directadmin directadmin crack directadmin openlitespeed diệt malware diệt virus DKIM google clou google cloud google drive unlimited imunify imunify 360 LEMP let's encrypt lưu trữ hình ảnh ngoài hosting mariadb myportal nginx nâng cấp mariadb office 365 openlitespeed popup flatsome port login sale badge service is currently down ssl ssl giá rẻ swap system load Theme blog theme miễn phí theme tin tức theme tạp chí tường lửa warning wildcard ssl woocommerce wpml xử lý mã độc

Copyright © 2021 · MH Solutions · Đăng nhập