Thêm ký tự vào trước và sau giá sản phẩm

Trong quá trình thiết lập sản phẩm, thỉnh thoảng bạn sẽ cần thêm ký tự vào trước và sau giá sản phẩm. Chẳng hạn một sản phẩm ví nam có giá 500.000đ, bạn có thể thêm vào ký tự trước và sau như sau: Chỉ có 500.000đ / cái.

Ký tự phía trước (gọi là Prefix) có thể giúp thúc đẩy người dùng mua hàng. Bạn có thể dùng các từ như: Chỉ có, Chỉ với, Giá từ, Giá chỉ, Mua ngay với giá, …

Ký tự phía sau (gọi là Suffix) để phân loại đơn vị tính cho sản phẩm. Có nhiều loại đơn vị tính tuỳ vào loại sản phẩm, như là: hộp, cái, thùng, chiếc, kg, …

Hướng dẫn thêm ký tự vào trước và sau giá sản phẩm

Để thêm Prefix và Suffix vào giá, bạn phải chỉnh sửa trong file Function.php của theme.

>> Có thể bạn cần xem lại bài viết: Functions.php trong WordPress là gì?

Đầu tiên, bạn có thể thêm Prefix và Suffix vào toàn bộ sản phẩm mặc định. Phương pháp này sẽ thêm vào Setting của Woocommerce và chỉ dùng cho các loại sản phẩm chỉ có một đơn vị tính duy nhất. Bạn thêm đoạn code sau vào Function.php và lưu lại nhé!

/*
* Prefix and sufix to price
* Author: https://wpviet.org
*/
/*Add default setting*/
function wpviet_woocommerce_general_settings( $array ) {

$array[] = array( ‘name’ => __( ‘Prefix and suffix to price’, ‘woocommerce’ ), ‘type’ => ‘title’, ‘desc’ => ”, ‘id’ => ‘woocommerce_presuffix_settings’ );

$array[] = array(
‘title’ => __( ‘Prefix’, ‘woocommerce’ ),
‘desc’ => __( ‘Add prefix to price. Leave blank to disable.’, ‘woocommerce’ ),
‘id’ => ‘wpviet_woocommerce_price_prefix’,
‘desc_tip’ => true,
‘type’ => ‘text’,
);

$array[] = array(
‘title’ => __( ‘Suffix’, ‘woocommerce’ ),
‘desc’ => __( ‘Add suffix to price. Leave blank to disable.’, ‘woocommerce’ ),
‘id’ => ‘wpviet_woocommerce_price_suffix’,
‘desc_tip’ => true,
‘type’ => ‘text’,
);

$array[] = array( ‘type’ => ‘sectionend’, ‘id’ => ‘woocommerce_presuffix_settings’);
return $array;
};
add_filter( ‘woocommerce_general_settings’, ‘wpviet_woocommerce_general_settings’, 10, 1 );

Sau đó, khi vào phần Cài đặt của Woocommerce, bạn sẽ thấy có thêm phần Prefix and suffix to price. Bạn có thể thêm 2 ký tự vào trước và sau giá, chúng sẽ được áp dụng cho tất cả các sản phẩm.

Sau đó, để chúng hiển thị ngoài trang sản phẩm, bạn cần thêm đoạn code sau vào file Function:

/*Add to price html*/
add_filter( ‘woocommerce_get_price_html’, ‘bbloomer_price_prefix_suffix’, 100, 2 );
function bbloomer_price_prefix_suffix( $price, $product ){
$prefix = get_option( ‘wpviet_woocommerce_price_prefix’);
$suffix = get_option( ‘wpviet_woocommerce_price_suffix’);

$prefix_product = sanitize_text_field(get_post_meta($product->get_ID(), ‘_product_prefix’, true));
$suffix_product = sanitize_text_field(get_post_meta($product->get_ID(), ‘_product_suffix’, true));

if($prefix_product || (is_numeric($prefix_product) && $prefix_product == 0)) $prefix = $prefix_product;
if($suffix_product || (is_numeric($suffix_product) && $suffix_product == 0)) $suffix = $suffix_product;

$prefix = ($prefix && $prefix !== 0)?'<span class=”wpviet_woocommerce_price_prefix”>’.$prefix.'</span> ‘:”;
$suffix = ($suffix && $suffix !== 0)?’ <span class=”wpviet_woocommerce_price_suffix”>’.$suffix.'</span>’:”;

$price = $prefix.$price.$suffix;

return apply_filters( ‘wpviet_woocommerce_get_price’, $price );
}

Và đây là kết quả thay đổi của giá sản phẩm.

Tuy nhiên, cách trên có nhược điểm là thiếu linh hoạt. Chẳng hạn bạn muốn dùng các từ khác nhau cho mỗi loại sản phẩm, đơn vị tính các sản phẩm cũng khác nhau. Vi vậy, ta có thể thêm Prefix và Suffix vào từng loại sản phẩm bằng đoạn code sau:

/*Add metabox to product*/
add_action( ‘woocommerce_product_options_general_product_data’, ‘wpviet_presuffix_products’ );
function wpviet_presuffix_products() {
//Add metabox prefix to product
woocommerce_wp_text_input( array(
‘id’ => ‘_product_prefix’,
‘label’ => ‘Prefix’,
‘description’ => ‘Add prefix to price. Leave blank to default.’,
‘desc_tip’ => ‘true’,
) );
//Add metabox suffix to product
woocommerce_wp_text_input( array(
‘id’ => ‘_product_suffix’,
‘label’ => ‘Suffix’,
‘description’ => ‘Add suffix to price. Leave blank to default.’,
‘desc_tip’ => ‘true’,
) );
}

/*Save metabox prefix and suffix*/
add_action( ‘woocommerce_process_product_meta’, ‘wpviet_presuffix_products_save’ );
function wpviet_presuffix_products_save( $post_id ) {
if(get_post_type($post_id) == ‘product’){
if ( isset($_POST[‘_product_prefix’]) ) {
if ($_POST[‘_product_prefix’] != “”) {
update_post_meta($post_id, ‘_product_prefix’, sanitize_text_field($_POST[‘_product_prefix’]));
} else {
delete_post_meta($post_id, ‘_product_prefix’);
}
}
if ( isset($_POST[‘_product_suffix’]) ) {
if ($_POST[‘_product_suffix’] != “”) {
update_post_meta($post_id, ‘_product_suffix’, sanitize_text_field($_POST[‘_product_suffix’]));
} else {
delete_post_meta($post_id, ‘_product_suffix’);
}
}
}
}

Sau khi chèn đoạn code trên vào Function.php và lưu lại, bạn vào thêm sản phẩm sẽ có phần Prefix và Suffix ở trong mục Dữ liệu sản phẩm.

Và đây là kết quả, nó sẽ lấy dữ liệu ký tự trước và sau từ sản phẩm, thay vì từ cài đặt trong Woocommerce mà ta đã làm trước đó.

Kết luận

Thêm Prefix và Suffix vào giá có rất nhiều lợi ích cho trang web của bạn. Việc thêm 2 dữ liệu này vào giá sản phẩm cũng khá đơn giản với các đoạn code ở trên. Tuy nhiên, bạn cần đảm bảo mình đã đọc và hiểu về Function.php trước khi thực hiện thay đổi này nhé. Chúc bạn thành công!

Để lại bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *