Tuỳ biến sản phẩm giá 0đ/hết hàng trong Woocommerce thành Liên hệ, số điện thoại

Tuỳ biến sản phẩm giá 0đ/hết hàng trong Woocommerce thành Liên hệ, số điện thoại

Một đoạn code tuỳ biến sản phẩm trong Woocommerce, giúp bạn chuyển những sản phẩm có giá 0đ hoặc hết hàng thành chữ, nút, số điện thoại tuỳ ý.

Đoạn code Woocommerce chuyển giá hoặc để trống thành Liên hệ, số điện thoại tùy ý bạn, bạn muốn thay thành gì cũng được, code sẽ được thêm vào function.php

Nếu bạn không xài child theme thì paste code vào function.php của theme đó luôn.

Nếu bạn xài flatsome-child thì vào đó và thêm code vào function.php:

function quannguyenphat_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, __( ‘Liên hệ’, ‘woocommerce’ ) );
} else {
$price = ‘‘ . __( ‘Liên hệ’, ‘woocommerce’ ) . ‘‘;
}
}
return $price;
}
add_filter( ‘woocommerce_get_price_html’, ‘quannguyenphat_wc_custom_get_price_html’, 10, 2 );

Chuyển giá thành Liên hệ khi hết hàng trong woocommerce

Đoạn code này sẽ giúp bạn hiển thị giá thành chữ Liên hệ khi set sản phẩm đó là hết hàng. Bạn chỉ cần thêm đoạn code sau vào functions.php của theme đang active hoặc child theme

function quannguyenphat_oft_custom_get_price_html( $price, $product ) {
if ( !is_admin() && !$product->is_in_stock()) {
$price = '' . __( 'Liên hệ', 'woocommerce' ) . '';
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'quannguyenphat_oft_custom_get_price_html', 99, 2 );