Thứ Hai, 19/05/2025
Loading...
  • Login
Hoàng Giang Nam Blog
  • Home
  • WordPress
    • WordPress
    • Woocommerce
  • Windows
  • Office
  • Phần Mềm
  • Thủ thuật
  • Linux
    • Ubuntu
    • CentOS
    • VPS – Server
    • Web Server
No Result
View All Result
Hoàng Giang Nam Blog
  • Home
  • WordPress
    • WordPress
    • Woocommerce
  • Windows
  • Office
  • Phần Mềm
  • Thủ thuật
  • Linux
    • Ubuntu
    • CentOS
    • VPS – Server
    • Web Server
No Result
View All Result
  • Login
Hoàng Giang Nam Blog
No Result
View All Result
Home Wordpress Woocommerce
Hướng dẫn tạo khung khuyến mãi đẹp trong Flatsome

Thay đổi cách hiển thị giảm giá đẹp cho Woocommerce

admin by admin
19/12/2024
Share on FacebookShare on Twitter

Mục lục

  1. Action Hook
  2. Filter Hook

Để sử dụng bạn copy đoạn code sau vào file functions.php trong thư mục theme bạn đang sử dụng nhé!
Ngoài ra kéo xuống để xem thêm CSS.

function discount_price_html($product, $is_variation = false) {
    ob_start();

    if ($product->is_on_sale()): ?>
        <?php
    endif;

    if ($product->is_on_sale() && ($is_variation || $product->is_type('simple') || $product->is_type('external'))) {
        $sale_price = $product->get_sale_price();
        $regular_price = $product->get_regular_price();

        if ($regular_price) {
            $sale = round(((floatval($regular_price) - floatval($sale_price)) / floatval($regular_price)) * 100);
            $sale_amount = $regular_price - $sale_price;
            ?>
            <div class="discount_single_price">
                <div>
                    <span class="label">Giá:</span>
                    <span class="discount_price"><?php echo wc_price($sale_price); ?></span>
                </div>
                <div>
                    <span class="label">Thị trường:</span>
                    <span class="discount_price"><del><?php echo wc_price($regular_price); ?></del></span>
                </div>
                <div>
                    <span class="label">Tiết kiệm:</span>
                    <span class="discount_price sale_amount">
                        <?php echo wc_price($sale_amount); ?> (<?php echo $sale; ?>%)
                    </span>
                </div>
            </div>
            <?php
        }
    } elseif ($product->is_on_sale() && $product->is_type('variable')) {
        $prices = $product->get_variation_prices(true);

        if (empty($prices['price'])) {
            $price = apply_filters('woocommerce_variable_empty_price_html', '', $product);
        } else {
            $min_price = current($prices['price']);
            $max_price = end($prices['price']);
            $min_reg_price = current($prices['regular_price']);
            $max_reg_price = end($prices['regular_price']);

            if ($min_price !== $max_price) {
                $price = wc_format_price_range($min_price, $max_price) . $product->get_price_suffix();
            } elseif ($product->is_on_sale() && $min_reg_price === $max_reg_price) {
                $sale = round(((floatval($max_reg_price) - floatval($min_price)) / floatval($max_reg_price)) * 100);
                $sale_amount = $max_reg_price - $min_price;
                ?>
                <div class="discount_single_price">
                    <div>
                        <span class="label">Giá:</span>
                        <span class="discount_price"><?php echo wc_price($min_price); ?></span>
                    </div>
                    <div>
                        <span class="label">Thị trường:</span>
                        <span class="discount_price"><del><?php echo wc_price($max_reg_price); ?></del></span>
                    </div>
                    <div>
                        <span class="label">Tiết kiệm:</span>
                        <span class="discount_price sale_amount">
                            <?php echo wc_price($sale_amount); ?> (<?php echo $sale; ?>%)
                        </span>
                    </div>
                </div>
                <?php
            } else {
                $price = wc_price($min_price) . $product->get_price_suffix();
            }
        }

        echo $price;
    } else {
        ?>
        <p class="<?php echo esc_attr(apply_filters('woocommerce_product_price_class', 'price')); ?>">
            <?php echo $product->get_price_html(); ?>
        </p>
        <?php
    }

    return ob_get_clean();
}

function woocommerce_template_single_price() {
    global $product;
    echo discount_price_html($product);
}

add_filter('woocommerce_available_variation', 'discount_woocommerce_available_variation', 10, 3);

function discount_woocommerce_available_variation($args, $thisC, $variation) {
    $old_price_html = $args['price_html'];

    if ($old_price_html) {
        $args['price_html'] = discount_price_html($variation, true);
    }

    return $args;
}

 

Đoạn code sau chính là CSS kèm theo

.discount_single_price {
    background-color: #199bc42e;
    border: 1px dashed #199bc4;
    padding: 10px;
    border-radius: 3px;
    margin: 0 0 10px;
    color: #000;

    /* Cross-browser compatibility */
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
}

.discount_single_price span.label {
    color: #333;
    font-weight: 400;
    font-size: 14px;
    padding: 0;
    margin: 0;
    float: left;
    width: 82px;
    text-align: left;
    line-height: 18px;
}

.discount_single_price span.discount_price .amount {
    font-size: 14px;
    font-weight: 700;
    color: #ff3a3a;
}

.discount_single_price span.discount_price del .amount,
.discount_single_price span.discount_price del {
    font-size: 14px;
    color: #333;
    font-weight: 400;
}

 

Hooks trong WordPress là các điểm móc nằm trong hệ thống mã nguồn của WordPress, tại đây có thể bổ sung các đoạn code thông qua hai hàm add_action và add_filter mà WordPress đã cung cấp sẵn.

Hooks được chia ra làm 2 loại chính đó là Action Hook và Filter Hook :

  • Action là các điểm móc liên quan đến xử lý hành động
  • Filter là các điểm móc liên quan đến xử lý nội dung

Dưới đây là các hook thông dụng được cung cấp bởi WordPress.org giúp bạn tùy biến Website theo ý mình

Action Hook

  • has_action()
  • add_action()
  • do_action()
  • do_action_ref_array()
  • did_action()
  • remove_action()
  • remove_all_actions()
  • doing_action()

Filter Hook

  • has_filter()
  • add_filter()
  • apply_filters()
  • apply_filters_ref_array()
  • current_filter()
  • remove_filter()
  • remove_all_filters()
  • doing_filter()

Chúc các bạn thành công!

Tags: Custom themeWoocommerce
admin

admin

Next Post

Cách tính số lần sạc của pin laptop Windows

Please login to join discussion

Recommended.

Hướng dẫn tạo và kích hoạt Swap trên VPS

Hướng dẫn tạo và kích hoạt Swap trên VPS

19/12/2024
Dùng thử VPS Linode trong 60 Ngày với 100$ Credit trải nghiệm dịch vụ

Dùng thử VPS Linode trong 60 Ngày với 100$ Credit trải nghiệm dịch vụ

01/01/2025

Trending.

HuananZhi X79 / X99 Motherboard BIOS POST codes

HuananZhi X79 / X99 Motherboard BIOS POST codes

01/01/2025
Xóa, Sửa, Nâng dung lượng RAM ẢO – Swap trên Ubuntu

Xóa, Sửa, Nâng dung lượng RAM ẢO – Swap trên Ubuntu

01/01/2025
Tạo file IMG Windows.GZ bằng VM VirtualBox để cài đặt trên VPS Digitalocean, Linode

Tạo file IMG Windows.GZ bằng VM VirtualBox để cài đặt trên VPS Digitalocean, Linode

31/12/2024
Tăng / Giảm giá SALE toàn bộ cửa hàng, Xóa Giá Sale

Tăng / Giảm giá SALE toàn bộ cửa hàng, Xóa Giá Sale

01/01/2025

GG One 1năm

18/12/2024
Hoàng Giang Nam

Blog chia sẽ kiến thức lập trình, source code, themes, plugin, phần mềm & thủ thuật máy tính...

Follow Us

Danh mục

  • CentOS
  • Chung
  • Server Email
  • Thủ thuật máy tính
  • Ubuntu
  • VPS – Server
  • Web Server
  • Windows
  • Woocommerce
  • Wordpress

Recent News

Disable Web Search Results in Windows 10/11 Start Menu

16/01/2025
Hướng dẫn lấy Client ID + Client Secret + Refresh Token Google Drive API

Hướng dẫn lấy Client ID + Client Secret + Refresh Token Google Drive API

19/12/2024
  • About
  • Advertise
  • Privacy & Policy
  • Contact

© 2024 Hoanggiangnam.com

No Result
View All Result
  • Home
  • Phần Mềm
  • Thủ thuật máy tính
  • WordPress
  • Ubuntu
  • Office
  • Photography
  • Windows

© 2024 Hoanggiangnam.com

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Click to Copy