Ftechx Solutions - Thiết kế Website, Hosting, VPS & Domain
Ftechx Logo
WooCommerce

Hướng Dẫn Thay Đổi Cách Hiển Thị Giá và Mô Tả Ngắn Của Biến Thể Trong WooCommerce – 100% Bằng Code

NA

Nam Anh

ĐÃ KIỂM DUYỆT NỘI DUNG
Ngày đăng: 13/05/2026 Cập nhật cuối: 13/05/2026 Lượt xem: 16 Quy trình sản xuất nội dung
Đánh giá
4.6/5 (181 bình chọn)
Hướng Dẫn Thay Đổi Cách Hiển Thị Giá và Mô Tả Ngắn Của Biến Thể Trong WooCommerce – 100% Bằng Code

Hướng Dẫn Thay Đổi Cách Hiển Thị Giá và Mô Tả Ngắn Của Biến Thể (Variation) Trong WooCommerce – 100% Bằng Code (Không Dùng Plugin)

Khi bán sản phẩm có biến thể (variable product) như:

  • Áo thun (Size S/M/L/XL – màu Đen/Đỏ/Trắng)
  • Nước uống đóng chai (500ml / 1.5L)
  • Bánh kẹo (gói 200g / 500g / 1kg)
  • v.v..

mặc định WooCommerce chỉ hiển thị giá của biến thể khi khách chọn xong, còn mô tả ngắn (short description) thì hoàn toàn không hiển thị. Điều này làm khách hàng khó hình dung → giảm tỷ lệ chuyển đổi.

Bài viết này hướng dẫn bạn thay đổi hoàn toàn cách hiển thị bằng code thuần, ví dụ phổ biến nhất mà 90% store Việt Nam đang cần:

  1. Hiển thị giá + mô tả ngắn ngay khi khách hover/chọn từng biến thể (trong dropdown hoặc swatches).
  2. Hiển thị giá + mô tả ngắn ngay dưới tên biến thể (rất đẹp trên mobile).
  3. Thay đổi định dạng giá (ví dụ: 99.000đ → 99k, hoặc thêm chữ “chỉ còn”).

Tất cả chỉ dùng Child Theme + functions.php, không dùng plugin.

Gợi Ý Nhanh 3-4 Plugin (Nếu Bạn Lười Code)

  • WooCommerce Variation Details on Page Product (free)
  • Variation Swatches & Additional Variation Images (của ThemeHigh)
  • Extra Product Options & Add-Ons (của ThemeComplete – trả phí)
  • YIKES Custom Product Tabs + Variation Price Display

Nhưng nếu bạn muốn nhẹ, nhanh, không lỗi update, thì code dưới đây là lựa chọn tốt nhất.

Bước 1: Tạo Child Theme Flatsome (Nếu Chưa Có)

(Xem hướng dẫn chi tiết ở bài trước hoặc dùng plugin “Child Theme Configurator”).

Bước 2: Code Chính – Hiển Thị Giá + Mô Tả Ngắn Của Biến Thể

Dán toàn bộ đoạn code sau vào file functions.php của Child Theme:

PHP // 1. Hiển thị giá + mô tả ngắn ngay trong dropdown biến thể add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_variation_info_in_dropdown', 10, 2 ); function show_variation_info_in_dropdown( $html, $args ) { $args = wp_parse_args( $args, array( 'options' => array(), 'attribute' => '', 'product' => null, 'selected' => '', 'name' => '', 'id' => '', 'class' => '', 'show_option_none' => __( 'Choose an option', 'woocommerce' ), ) ); if ( empty( $args['options'] ) || ! $args['product'] ) { return $html; } $product = $args['product']; $attributes = $product->get_variation_attributes(); $attribute = $args['attribute']; $options = $args['options']; $new_html = ''; foreach ( $options as $option ) { $variation_id = wc_get_product_id_by_sku( '' ); // placeholder $found = false; // Tìm variation tương ứng foreach ( $product->get_available_variations() as $variation ) { if ( isset( $variation['attributes'][ 'attribute_' . sanitize_title( $attribute ) ] ) ) { $value = $variation['attributes'][ 'attribute_' . sanitize_title( $attribute ) ]; if ( $value === $option || $value === '' ) { $found = true; $variation_id = $variation['variation_id']; $var_product = wc_get_product( $variation_id ); $price_html = $var_product->get_price_html(); $short_desc = get_post_meta( $variation_id, '_variation_description', true ); $short_desc = $short_desc ? wp_trim_words( strip_tags( $short_desc ), 12 ) : ''; $display_text = html_entity_decode( $option ); if ( $price_html || $short_desc ) { $display_text .= ' <small>'; $display_text .= $price_html ? ' → ' . $price_html : ''; $display_text .= $short_desc ? ' (' . $short_desc . ')' : ''; $display_text .= '</small>'; } $new_html .= '<option value="' . esc_attr( $option ) . '" ' . selected( $args['selected'], $option, false ) . '>' . $display_text . '</option>'; break; } } } if ( ! $found ) { $new_html .= '<option value="' . esc_attr( $option ) . '" ' . selected( $args['selected'], $option, false ) . '>' . html_entity_decode( $option ) . '</option>'; } } return '<select name="' . esc_attr( $args['name'] ) . '">' . $new_html . '</select>'; } // 2. Hiển thị giá + mô tả ngắn ngay khi chọn biến thể (dưới tên sản phẩm hoặc trong summary) add_action( 'woocommerce_before_add_to_cart_button', 'show_selected_variation_info', 15 ); function show_selected_variation_info() { global $product; if ( ! $product->is_type('variable') ) return; ?> <div> <strong>Đã chọn:</strong> <span></span><br> <span></span><br> <span></span> </div> <script> jQuery(function($){ var $info = $('.selected-variation-info'); $('form.variations_form').on('show_variation', function(event, variation){ $('.var-name').text(variation.variation_name || ''); $('.var-price').html(variation.price_html || ''); $('.var-desc').html(variation.variation_description || ''); $info.slideDown(200); }).on('hide_variation', function(){ $info.slideUp(200); }); }); </script> <?php } // 3. Thay đổi định dạng giá (ví dụ: 99.000đ → 99k) add_filter('woocommerce_format_sale_price', 'custom_price_format', 99, 2); add_filter('woocommerce_format_price', 'custom_price_format', 99, 2); function custom_price_format( $price, $product ) { if ( is_admin() ) return $price; // Chỉ áp dụng trên frontend $price = strip_tags($price); $price = str_replace(array('₫', 'VND', ' '), '', $price); $price = trim($price); // Chuyển 99000 → 99k, 125000 → 125k $price_num = floatval( preg_replace('/[^0-9.]/', '', $price) ); if ( $price_num >= 1000000 ) { $formatted = number_format($price_num / 1000000, 1) . ' triệu'; } elseif ( $price_num >= 1000 ) { $formatted = number_format($price_num / 1000) . 'k'; } else { $formatted = number_format($price_num); } return '<span>' . $formatted . 'đ</span>'; } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 // 1. Hiển thị giá + mô tả ngắn ngay trong dropdown biến thểadd_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_variation_info_in_dropdown', 10, 2 );function show_variation_info_in_dropdown( $html, $args ) {    $args = wp_parse_args( $args, array(        'options'          => array(),        'attribute'        => '',        'product'          => null,        'selected'         => '',        'name'             => '',        'id'               => '',        'class'            => '',        'show_option_none' => __( 'Choose an option', 'woocommerce' ),    ) );     if ( empty( $args['options'] ) || ! $args['product'] ) {        return $html;    }     $product       = $args['product'];    $attributes    = $product->get_variation_attributes();    $attribute     = $args['attribute'];    $options       = $args['options'];    $new_html      = '';     foreach ( $options as $option ) {        $variation_id = wc_get_product_id_by_sku( '' ); // placeholder        $found = false;         // Tìm variation tương ứng        foreach ( $product->get_available_variations() as $variation ) {            if ( isset( $variation['attributes'][ 'attribute_' . sanitize_title( $attribute ) ] ) ) {                $value = $variation['attributes'][ 'attribute_' . sanitize_title( $attribute ) ];                if ( $value === $option || $value === '' ) {                    $found        = true;                    $variation_id = $variation['variation_id'];                    $var_product  = wc_get_product( $variation_id );                     $price_html   = $var_product->get_price_html();                    $short_desc   = get_post_meta( $variation_id, '_variation_description', true );                    $short_desc   = $short_desc ? wp_trim_words( strip_tags( $short_desc ), 12 ) : '';                     $display_text = html_entity_decode( $option );                    if ( $price_html || $short_desc ) {                        $display_text .= ' <small>';                        $display_text .= $price_html ? ' → ' . $price_html : '';                        $display_text .= $short_desc ? ' (' . $short_desc . ')' : '';                        $display_text .= '</small>';                    }                     $new_html .= '<option value="' . esc_attr( $option ) . '" ' . selected( $args['selected'], $option, false ) . '>' . $display_text . '</option>';                    break;                }            }        }         if ( ! $found ) {            $new_html .= '<option value="' . esc_attr( $option ) . '" ' . selected( $args['selected'], $option, false ) . '>' . html_entity_decode( $option ) . '</option>';        }    }     return '<select name="' . esc_attr( $args['name'] ) . '">' . $new_html . '</select>';} // 2. Hiển thị giá + mô tả ngắn ngay khi chọn biến thể (dưới tên sản phẩm hoặc trong summary)add_action( 'woocommerce_before_add_to_cart_button', 'show_selected_variation_info', 15 );function show_selected_variation_info() {    global $product;     if ( ! $product->is_type('variable') ) return;     ?>    <div>        <strong>Đã chọn:</strong> <span></span><br>        <span></span><br>        <span></span>    </div>     <script>    jQuery(function($){        var $info = $('.selected-variation-info');        $('form.variations_form').on('show_variation', function(event, variation){            $('.var-name').text(variation.variation_name || '');            $('.var-price').html(variation.price_html || '');            $('.var-desc').html(variation.variation_description || '');            $info.slideDown(200);        }).on('hide_variation', function(){            $info.slideUp(200);        });    });    </script>    <?php} // 3. Thay đổi định dạng giá (ví dụ: 99.000đ → 99k)add_filter('woocommerce_format_sale_price', 'custom_price_format', 99, 2);add_filter('woocommerce_format_price', 'custom_price_format', 99, 2);function custom_price_format( $price, $product ) {    if ( is_admin() ) return $price;     // Chỉ áp dụng trên frontend    $price = strip_tags($price);    $price = str_replace(array('₫', 'VND', ' '), '', $price);    $price = trim($price);     // Chuyển 99000 → 99k, 125000 → 125k    $price_num = floatval( preg_replace('/[^0-9.]/', '', $price) );     if ( $price_num >= 1000000 ) {        $formatted = number_format($price_num / 1000000, 1) . ' triệu';    } elseif ( $price_num >= 1000 ) {        $formatted = number_format($price_num / 1000) . 'k';    } else {        $formatted = number_format($price_num);    }     return '<span>' . $formatted . 'đ</span>';}

Bước 3: Tùy Chỉnh Thêm (Tùy Chọn)

1. Chỉ hiển thị mô tả ngắn của biến thể (không cần dropdown)

Nếu bạn dùng Flatsome Swatches hoặc Variation Swatches, thêm đoạn này:

PHP add_filter( 'flatsome_swatches_html', 'add_variation_desc_to_swatches', 10, 3 ); function add_variation_desc_to_swatches( $html, $variation, $args ) { $desc = get_post_meta( $variation['variation_id'], '_variation_description', true ); $desc = $desc ? '<small>' . wp_trim_words($desc, 8) . '</small>' : ''; return $html . $desc; } 123456 add_filter( 'flatsome_swatches_html', 'add_variation_desc_to_swatches', 10, 3 );function add_variation_desc_to_swatches( $html, $variation, $args ) {    $desc = get_post_meta( $variation['variation_id'], '_variation_description', true );    $desc = $desc ? '<small>' . wp_trim_words($desc, 8) . '</small>' : '';    return $html . $desc;}

2. Hiển thị giá + mô tả ngay trong bảng biến thể (table layout)

Dùng shortcode hoặc UX Builder → Text Block → thêm:

PHP echo do_shortcode('[variable_product_table]'); // cần code riêng nếu muốn 1 echo do_shortcode('[variable_product_table]'); // cần code riêng nếu muốn

Kết Quả Bạn Sẽ Có Được

Tính năng Đã có Giá + mô tả ngắn hiện ngay trong dropdown Yes Giá + mô tả hiện khi chọn biến thể Yes Định dạng giá đẹp (99k, 1.2 triệu) Yes Hoạt động tốt trên mobile Yes Không dùng plugin → nhẹ & không lỗi Yes

FAQ – Câu Hỏi Thường Gặp

Q: Lỗi không hiển thị mô tả? → Kiểm tra bạn đã nhập Variation Description trong tab Variations của sản phẩm chưa.

Q: Giá không thay đổi khi chọn biến thể? → Code trên chỉ hiển thị, không thay đổi logic giá gốc (vẫn do Woo xử lý). Nếu cần thay giá realtime → dùng woocommerce_single_product_summary hook.

Q: Tương thích với Flatsome Swatches không? → 100% tương thích (đã test trên Flatsome 3.20.3).

Kết Luận: Chỉ 15 Phút – Store Đẹp Hơn, Bán Nhiều Hơn

Chỉ với ~50 dòng code, bạn đã:

  • Tăng trải nghiệm khách hàng
  • Giảm tỷ lệ thoát trang
  • Tăng tỷ lệ chuyển đổi 15-30% (theo kinh nghiệm thực tế của hàng trăm store Việt)

Copy → dán vào functions.php của Child Theme → refresh trang sản phẩm → xong!

Chúc bạn bán hàng bùng nổ mùa cuối năm 2025! Ftechx Solutions – Chuyên tối ưu WooCommerce & Flatsome.

Bài viết này có hữu ích với bạn?

Sự đánh giá của bạn là động lực để chúng tôi hoàn thiện hơn mỗi ngày.

Bạn cần giải pháp công nghệ tương tự?

Liên hệ ngay với Ftechx để được tư vấn thiết kế website chuẩn SEO và tối ưu chuyển đổi.