• Bỏ qua primary navigation
  • Skip to main content
  • Bỏ qua primary sidebar
  • Bỏ qua footer

Thủ thuật thiết kế website

Chia sẻ kinh nghiệm thiết kế website

  • Trang chủ
  • Joomla
    • Thủ thuật Joomla
    • Joomla Extensions
    • Joomla Themes
  • WordPress
    • Thủ thuật WordPress
    • WordPress Plugins
    • WordPress Themes
  • Web Development
    • HTML/CSS
    • Javascript
    • PHP
  • PHP Framework
    • Codeigniter
  • App Developer
    • React Native
  • SEO
  • Chia sẻ
  • Phần mềm
Bạn đang ở:Trang chủ / Wordpress / Thủ thuật WordPress / [Woocommerce] Thay chữ Sale bằng % giảm giá trong Woocommerce

[Woocommerce] Thay chữ Sale bằng % giảm giá trong Woocommerce

24/02/2021 - admin Để lại bình luận

Nhiều khi chúng ta không muốn hiển thị chữ SALE khi sản phẩm đó có chương trình giảm giá. Mà thay vào đó sẽ hiển thị phần trăm (%) giảm giá của sản phẩm đó. vậy chúng ta sẽ làm gì?

Thay chữ sale bằng phần trăm giảm giá trong woocommerce

Bạn chỉ cần thêm đoạn code sau vào file functions.php của theme trang sử dụng là được nhé

/*
 * Thay chữ Sale thành phần trăm (%) giảm giá
 * Author: levantoan.com
 */
add_filter('woocommerce_sale_flash', 'devvn_woocommerce_sale_flash', 10, 3);
function devvn_woocommerce_sale_flash($html, $post, $product){
    return '<span class="onsale"><span>' . devvn_presentage_bubble($product) . '</span></span>';
}
 
function devvn_presentage_bubble( $product ) {
    $post_id = $product->get_id();
 
    if ( $product->is_type( 'simple' ) || $product->is_type( 'external' ) ) {
        $regular_price  = $product->get_regular_price();
        $sale_price     = $product->get_sale_price();
        $bubble_content = round( ( ( floatval( $regular_price ) - floatval( $sale_price ) ) / floatval( $regular_price ) ) * 100 );
    } elseif ( $product->is_type( 'variable' ) ) {
        if ( $bubble_content = devvn_percentage_get_cache( $post_id ) ) {
            return devvn_percentage_format( $bubble_content );
        }
 
        $available_variations = $product->get_available_variations();
        $maximumper           = 0;
 
        for ( $i = 0; $i < count( $available_variations ); ++ $i ) {
            $variation_id     = $available_variations[ $i ]['variation_id'];
            $variable_product = new WC_Product_Variation( $variation_id );
            if ( ! $variable_product->is_on_sale() ) {
                continue;
            }
            $regular_price = $variable_product->get_regular_price();
            $sale_price    = $variable_product->get_sale_price();
            $percentage    = round( ( ( floatval( $regular_price ) - floatval( $sale_price ) ) / floatval( $regular_price ) ) * 100 );
            if ( $percentage > $maximumper ) {
                $maximumper = $percentage;
            }
        }
 
        $bubble_content = sprintf( __( '%s', 'woocommerce' ), $maximumper );
 
        devvn_percentage_set_cache( $post_id, $bubble_content );
    } else {
        $bubble_content = __( 'Sale!', 'woocommerce' );
 
        return $bubble_content;
    }
 
    return devvn_percentage_format( $bubble_content );
}
 
function devvn_percentage_get_cache( $post_id ) {
    return get_post_meta( $post_id, '_devvn_product_percentage', true );
}
 
function devvn_percentage_set_cache( $post_id, $bubble_content ) {
    update_post_meta( $post_id, '_devvn_product_percentage', $bubble_content );
}
 
//Định dạng kết quả dạng -{value}%. Ví dụ -20%
function devvn_percentage_format( $value ) {
    return str_replace( '{value}', $value, '-{value}%' );
}
 
// Xóa cache khi sản phẩm hoặc biến thể thay đổi
function devvn_percentage_clear( $object ) {
    $post_id = 'variation' === $object->get_type()
        ? $object->get_parent_id()
        : $object->get_id();
 
    delete_post_meta( $post_id, '_devvn_product_percentage' );
}
add_action( 'woocommerce_before_product_object_save', 'devvn_percentage_clear' );

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

Nguồn : https://levantoan.com

0 0 vote
Article Rating
Subscribe
Login
Notify of
guest
guest
0 Comments
Inline Feedbacks
View all comments

Sidebar chính

Từ khóa

Air Explorer Pro chia se plugin WP Rocket chia se WP Rocket chuyen excel thanh file csv Codeigniter password csv Excel excel thành csv flatsome giam gia woocommerce git git cheat sheet github huong dan seo website loai bo tai nguyen chan hien thi Loại bỏ các tài nguyên chặn hiển thị menu flatsome menu Woocommerce menu xo flatsome nut mua ngay password generator Codeigniter plugin WP Rocket polylang polylang fix lang default polylang loi ngon ngu mac dinh polylang set default language quản lý nhiều tài khoản random password generator React Native seo website splash React Native splash screen splash screen React Native Test Speed Google thu thuat wordpress toi uu seo website tài nguyên chặn hiển thị Woocommerce Woocommerce Product Filter Woo Product Filter wordpress rocket WP Rocket xoá menu quản trị Woocommerce xoá menu Woocommerce xoá thanh menu Woocommerce

Dịch vụ Thiết Kế Website

Footer

GIỚI THIỆU

Đây là blog cá nhân, cung cấp các thông tin, kiến thức và kinh nghiệm về lập trình và cuộc sống. Những bài viết được mình sưu tập từ nhiều nguồn, mọi chi tiết liên quan đến bản quyền xin vui lòng liên hệ qua email kairu2607@gmail.com ! Cám ơn

BÀI VIẾT MỚI NHẤT

  • [Woocommerce] Hướng dẫn thay đổi cách hiển thị giảm giá bắt mắt người xem trong
  • [Woocommerce] Thay chữ Sale bằng % giảm giá trong Woocommerce
  • Thêm nút Mua ngay vào Woocommerce một cách đơn giản

Copyright © 2021 · Metro Pro on Genesis Framework · WordPress · Đăng nhập

wpDiscuz