How to add text before or after price in WooCommerce

  • Home
  • How to add text before or after price in WooCommerce

You can use the WooCommerce filter woocommerce_get_price_html and woocommerce_cart_item_price to add a string before or after the price in WooCommerce product and cart pages.

Here is the code that you can copy and paste in your active theme’s functions.php file:

function wptips_custom_html_addon_to_price( $price, $product ) {
 $html_price_prefix = '<span class="price-prefix">prefix</span>'; // custom prefix html that you want to add before price
 $html_price_suffix = '<span class="price-suffix"> suffix</span>'; // custom suffix html that you want to add after price

 if ( !empty( $price ) ) {
	 $price = $html_price_prefix . ' ' . $price . ' ' . $html_price_suffix;
	  return $price;
	} else {
	  return $price;
	}
}
add_filter( 'woocommerce_get_price_html', 'wptips_custom_html_addon_to_price', 999, 2 );
add_filter( 'woocommerce_cart_item_price', 'wptips_custom_html_addon_to_price', 999, 2 );

You can use the above simple code snippet in your active theme, or use the code in a custom plugin to add text next to the price in WooCommerce. You can also add a prefix or suffix HTML or TEXT to the price on the product page and cart page as well.

3 Comments

  1. Stone

    Some products on the website have prices, while others do not have prices marked. I want to add text after the products have prices. If the products do not have prices, the text will not be displayed. What should I do?
    thanks

    Reply
    • Murali Kumar

      Did you try using the same code? It should work.

      Reply
  2. Laplace

    If variation is not selected i have a price span like ^^from 2$^^. Under this price span I would like to create a custom text A.
    But when I select a variation( doesnt matter which one) I would like to change this text to a text B.
    Is that possible? Thank you.

    Reply

Leave a comment