113
Hiển thị giá cho các Variable Product trong Woocommerce bằng cách hiển thị giá nhỏ nhất hoặc giá lớn nhất hoặc thêm hiển thị trước giá vài chữ: :giá chỉ từ…”.
Sau đây là đoạn mã tùy biến, bạn có thể copy để sử dụng một cách hiệu quả nhất
function wc_wc20_variation_price_format( $price, $product ) {
// Giá chính thức
$min_price = $product->get_variation_price( 'min', true );
$max_price = $product->get_variation_price( 'max', true );
$price = ($min_price !== $max_price) ? sprintf( __( 'Giá chỉ từ: %1$s', 'woocommerce' ), wc_price( $min_price ) ) : wc_price( $min_price );
// Giá Sales
$min_sale = $product->get_variation_regular_price( 'min', true );
$max_sale = $product->get_variation_regular_price( 'max', true );
$saleprice = ($min_sale !== $max_sale) ? wc_price( $min_sale ) : wc_price( $min_sale );
if ( $price !== $saleprice ) {
$price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
}
return $price;
}
add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );