Kiến Thức Lập TrìnhWordpress
Thay thế giá thành “Liên Hệ” trong woocommerce

Bạn là người sử dụng wordpress để tạo các trang bán hàng, trang thương mại điện tử nhưng có những sản phẩm bạn không thể nhập giá hay bạn để trống giá cần khách hàng liên hệ. Hôm nay mình sẽ hướng dẫn các bạn thay thế giá thành liên hệ hay để giá rỗng sẽ tự chuyển thành “liên hệ”.
Các bạn dán đoạn code bên dưới vào file function.php
function quynhon30s_custom_price_woocommer( $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', 'quynhon30s_custom_price_woocommer', 10, 2 );
Vậy những khi hết hàng thì sao. Bạn cũng có thể chuyển thành “Liên hệ” với đoạn code sau đây
function quynhon30s_custom_price_product( $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', 'quynhon30s_custom_price_product', 99, 2 );
Bạn có thể xem thêm các tip liên quan đến wordpress tại đây