Code chỉ hiển thị Giá thấp nhất hoặc Giá cao nhất của Sản phẩm có biến thể Variable Products

Nếu bạn đang sử dụng Woocommerce, bạn có thể sẽ không thích cách hiển thị khoảng giá mặc định của Sản phẩm có biến thể Variable Products. Thay vào đó, bạn có thể tùy biến giá theo cách của riêng mình. Trong bài viết này, mình sẽ hướng dẫn đoạn code chỉ hiển thị Giá thấp nhất hoặc Giá cao nhất của loại sản phẩm này.

Ủng hộ mình bằng cách click vào 1-2 quảng cáo bất kỳ trên website nhé. Cảm ơn bạn nhiều nhiều.

No Code Building – Admin

Hướng dẫn cách chèn code PHP vào website

Nếu bạn nào chưa biết cách chèn code PHP vào website. Mời bạn tham khảo bài viết “Cách chèn code PHP vào website” của mình nhé.

Chỉ hiển thị Giá thấp nhất của Sản phẩm có biến thể (Show only the lowest price on variable products)

Phạm vi giá sản phẩm biến thể thường hiển thị dao động theo khoảng, ví dụ 3.000.000đ – 5.000.000đ.

Với sự trợ giúp của đoạn mã dưới đây, bạn sẽ có thể ẩn giá cao nhất, đồng thời thêm chữ “Giá từ:” vào trước giá tối thiểu. Trong đoạn code, mình giữ nguyên bản tiếng Anh là “From:”, bạn có thể tùy chỉnh nhé.

// Show only lowest prices in WooCommerce variable products

add_filter( 'woocommerce_variable_sale_price_html', 'wpglorify_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wpglorify_variation_price_format', 10, 2 );
 
function wpglorify_variation_price_format( $price, $product ) {
 
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
 
// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
 
if ( $price !== $saleprice ) {
$price = '<del>' . $saleprice . $product->get_price_suffix() . '</del> <ins>' . $price . $product->get_price_suffix() . '</ins>';
}
return $price;
}

Chỉ hiển thị Giá cao nhất của Sản phẩm có biến thể (Show only the highest price on variable products)

Nếu vì lý do nào đó bạn muốn hiển thị giá tối đa thay vì giá tối thiểu, hãy sử dụng đoạn mã sau.

add_filter( 'woocommerce_variable_sale_price_html', 'wpglorify_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wpglorify_variation_price_format', 10, 2 );
 
function wpglorify_variation_price_format( $price, $product ) {
 
// Main Price
$prices = array( $product->get_variation_price( 'max', true ), $product->get_variation_price( 'min', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'Up To: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
 
// Sale Price
$prices = array( $product->get_variation_regular_price( 'max', true ), $product->get_variation_regular_price( 'min', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'Up To: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
 
if ( $price !== $saleprice ) {
$price = '<del>' . $saleprice . $product->get_price_suffix() . '</del> <ins>' . $price . $product->get_price_suffix() . '</ins>';
}
return $price;
}

Bạn muốn ẩn giá hoàn toàn cho các sản phẩm có biến thể cho đến khi khách hàng chọn một biến thể của sản phẩm?

Tất nhiên là được! Mình giới thiệu cho bạn đoạn code ngay dưới đây.

Ẩn giá đến khi khách hàng chọn 1 biến thể của sản phẩm (Hide Price until user select variations of a product)

Thay vì đoạn mã trên, hãy sử dụng đoạn mã bên dưới (đảm bảo không sử dụng cả hai đoạn mã cùng nhau để tránh bất kỳ xung đột nào).

add_filter( 'woocommerce_variable_sale_price_html', 'wpglorify_remove_variation_price', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wpglorify_remove_variation_price', 10, 2 );
 
function wpglorify_remove_variation_price( $price ) {
$price = '';
return $price;
}

Cảm ơn các bạn đã theo dõi bài viết Hướng dẫn chỉ hiển thị Giá thấp nhất hoặc Giá cao nhất của Sản phẩm có biến thể. Chúc các bạn thành công.

0989 333 069
Chat Zalo