
Hiện thị giá thấp cho sản phẩm biến thể WooCommerce
Nếu sản phẩm bạn là biến thể thì khi xem ở các trang lưu trữ sản phẩm (archive/shop) giá sẽ hiển thị từ giá thấp đến cao ví dụ 150,000₫ – 479,000₫, nhiều khách hàng của tôi yêu cầu hiện giá thấp thay cho giá dao động từ thấp đến cao nên có 1 đoạn code tôi hay áp dụng và sẽ share trong bài viết này để nhiều người cần có thể áp dụng.
Code hiện thị giá thấp cho sản phẩm biến thể WooCommerce
Bạn có thể thêm code này vào file functions.php và lưu lại là xong.
/** * Display only the lowest price for variable products on archive pages */ add_filter( 'woocommerce_variable_price_html', 'vutruso_custom_variable_price_html', 10, 2 ); function vutruso_custom_variable_price_html( $price, $product ) { // Only modify on archive/shop pages if ( ! is_singular( 'product' ) ) { // Get the minimum price $prices = $product->get_variation_prices( true ); $min_price = current( $prices['price'] ); // Format the price with the currency symbol $price = wc_price( $min_price ); } return $price; }
Ví dụ ban đầu sẽ hiện giá như sau 150,000₫ – 479,000₫ thì sau khi áp dụng code sẽ là 150,000₫
Hoặc cũng có 1 số khách lại cần ghi thêm “từ” trước giá để khách hàng của họ biết là giá sẽ từ thấp nhất là…
/** * Display only the lowest price for variable products on archive pages */ add_filter( 'woocommerce_variable_price_html', 'vutruso_custom_variable_price_html', 10, 2 ); function vutruso_custom_variable_price_html( $price, $product ) { // Only modify on archive/shop pages if ( ! is_singular( 'product' ) ) { // Get the minimum price $prices = $product->get_variation_prices( true ); $min_price = current( $prices['price'] ); // Format the price with the currency symbol and add "From" text $price = sprintf( __( 'Từ %s', 'woocommerce' ), wc_price( $min_price ) ); } return $price; }
Kết quả sau khi áp dụng code
Code share nhanh ở trên hy vọng sẽ giúp ích được cho nhiều người.