Đoạn code ngắn bên dưới đây sẽ giúp bạn chuyển “0đ” thành chữ “liên hệ” trên các sản phẩm trong WooCommerce một cách dễ dàng. Bạn chỉ cần chèn chúng vào sau file functions.php của theme đang kích hoạt là được! (Khuyến nghị nên cài ở file functions trong theme child).
function 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', 'wc_custom_get_price_html', 10, 2 );
Tương tự, nếu bạn muốn sản phẩm khi hết hàng sẽ hiện chữ liên hệ thì có thể thêm vào đoạn code sau:
function oft_custom_get_price_html( $price, $product ) {
if ( !is_admin() && !$product->is_in_stock()) {
$price = '<span class="amount">' . __( 'Liên hệ', 'woocommerce' ) . '</span>';
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'oft_custom_get_price_html', 99, 2 );
Xem thêm:
- Hướng dẫn tạo nút Affiliates cho sản phẩm WooCommerce
- Tạo form lấy SĐT bên dưới Gallery trong trang Sản phẩm
- Chuyển giá “0 đồng” thành chữ “Liên hệ” trong WordPress
- ThemeForest là gì | Cách mua, kinh nghiệm mua theme đẹp giá tốt
- Case Study kiếm tiền với Google Adsense bằng Website tin tức tiếng Việt