WooCommerce sẽ không hiển thị giá hoặc hiển thị giá 0đ khi sản phẩm của bạn chưa cập nhật giá hoặc có giá 0đ, tuy nhiên hiển thị Miễn phí trông sẽ đẹp hơn không hiển thị hoặc hiển thị “0đ”.

Dưới đây là đoạn code giúp bạn thực hiện điều đó!
Hiển thị Miễn phí khi sản phẩm 0đ hoặc không có giá
/**
* Display FREE if Price Zero or Empty - WooCommerce Single Product
*/
add_filter( 'woocommerce_get_price_html', 'nhut_me_price_free_zero', 9999, 2 );
function nhut_me_price_free_zero( $price, $product ) {
if ( $product->is_type( 'variable' ) ) {
$prices = $product->get_variation_prices( true );
$min_price = current( $prices['price'] );
if ( 0 == $min_price ) {
$max_price = end( $prices['price'] );
$min_reg_price = current( $prices['regular_price'] );
$max_reg_price = end( $prices['regular_price'] );
if ( $min_price !== $max_price ) {
$price = wc_format_price_range( 'FREE', $max_price );
$price .= $product->get_price_suffix();
} elseif ( $product->is_on_sale() && $min_reg_price === $max_reg_price ) {
$price = wc_format_sale_price( wc_price( $max_reg_price ), 'Miễn phí' );
$price .= $product->get_price_suffix();
} else {
$price = 'Miễn phí';
}
}
} elseif ( 0 == $product->get_price() ) {
$price = '<span class="woocommerce-Price-amount amount">Miễn phí</span>';
}
return $price;
}
Xem thêm nhiều bài viết về thủ thuật WooCommerce tại đây nhé!