Add characters before and after product price in WooCommerce.

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

The code provided allows users to add stimulating text before or after product prices to attract customers. By adding characters through the code, users can customize the text to promote sales or run promotional programs. The code adds two input fields, Prefix and Suffix, to product data, allowing users to customize the text for each product individually. This customization feature enables users to choose which products receive the special text and which do not, providing flexibility in marketing strategies. Overall, adding characters before and after product prices can enhance the attractiveness of products to potential buyers.

There are times when you want to add special text before or after the price of a product. These are usually stimulating words that can attract customers to buy immediately. Or sometimes it serves a promotional program.

Add characters before and after product price

To add characters to the product price, you can use the following code to add to the file function.php.

/*Add metabox to product*/
add_action( 'woocommerce_product_options_general_product_data', 'hocwp_presuffix_products' );
function hocwp_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', 'hocwp_presuffix_products_save' );
function hocwp_presuffix_products_save( $post_id ) {
if ( ! empty( $_POST['_product_prefix'] ) ) {
update_post_meta( $post_id, '_product_prefix', esc_attr( $_POST['_product_prefix'] ) );
}
if ( ! empty( $_POST['_product_suffix'] ) ) {
update_post_meta( $post_id, '_product_suffix', esc_attr( $_POST['_product_suffix'] ) );
}
}

/*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( 'hocwp_woocommerce_price_prefix');
$suffix = get_option( 'hocwp_woocommerce_price_suffix');

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

if($prefix_product) $prefix = $prefix_product;
if($suffix_product) $suffix = $suffix_product;

$prefix = ($prefix)?'<span class="hocwp_woocommerce_price_prefix">'.$prefix.'</span>':'';
$suffix = ($suffix)?'<span class="hocwp_woocommerce_price_suffix">'.$suffix.'</span>':';

$price = $prefix.$price.$suffix;
return apply_filters( 'woocommerce_get_price', $price );
}

The special thing about this code is that it will add two input fields. Prefix and Suffix into your product data. So you can choose which products get content and which don’t, and you can also use different content.

See also  Principles and Tips to Speed Up a WordPress Website Using Litespeed Cache

image1

And this is the result of the above code when displayed on the website.

image2

Summary

Adding characters before and after the price of a product is one of the ways to stimulate customers to buy the product. We can do this very easily with php code. Good luck.

Rate this post

Related posts