Đoạn code dưới đây sẽ giúp các bạn chuyển 0đ thành chữ “Liên hệ”. Bạn chỉ cần chèn đoạn code sau vào file functions.php của theme bạn đang sử dụng là được

Code chuyển 0đ thành chữ “Liên hệ”
Dán code dưới vào file functions.php của theme
function devvn_wc_custom_get_price_html( $price, $product ) {
if ( $product->get_price() == 0 ) {
if ( $product->is_on_sale() && $product->get_regular_price() ) {
$regular_price = wc_get_price_to_display( $product, array( 'qty' => 1, 'price' => $product->get_regular_price() ) );
$price = wc_format_price_range( $regular_price, __( 'Free!', 'woocommerce' ) );
} else {
$price = '<span class="amount">' . __( 'Liên hệ', 'woocommerce' ) . '</span>';
}
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'devvn_wc_custom_get_price_html', 10, 2 );
Cập nhật thêm phiên bản mới :
// Shortcode: [wc_price_or_contact id="123" text="Liên hệ"]
add_shortcode('wc_price_or_contact', function($atts){
$atts = shortcode_atts([
'id' => 0,
'text' => 'Liên hệ'
], $atts, 'wc_price_or_contact');
$product_id = intval($atts['id']);
if (!$product_id) return '';
$product = wc_get_product($product_id);
if (!$product) return '';
// Lấy giá thô
$price = $product->get_price();
// Xử lý: nếu không có giá (empty, null, '') hoặc giá = 0 -> hiển thị text
if ($price === '' || $price === null || floatval($price) == 0) {
// Dùng class giống Flatsome để kế thừa style
return '<span class="price wc-contact-text">' . esc_html($atts['text']) . '</span>';
}
// Nếu có giá -> hiển thị giá HTML chuẩn WooCommerce
return '<span class="price">' . $product->get_price_html() . '</span>';
});
// Optional: thay đổi hiển thị giá toàn site nếu sản phẩm không nhập giá
add_filter('woocommerce_get_price_html', function($price_html, $product){
// $product có thể là ID hoặc object — đảm bảo là object
if (!is_object($product)) {
$product = wc_get_product($product);
if (!$product) return $price_html;
}
$price = $product->get_price();
// Nếu không có giá hoặc = 0 => trả về "Liên hệ"
if ($price === '' || $price === null || floatval($price) == 0) {
// Nếu muốn thay đổi chữ mặc định, sửa 'Liên hệ' ở đây
return '<span class="price wc-contact-text">Liên hệ</span>';
}
return $price_html;
}, 10, 2);
Nguồn : http://levantoan.com + Sưu tập