Posted on

WooCommerce: Add Prefix / Suffix to Product Prices

Sometimes you may want to add a prefix or a suffix to your prices. It could be something like “From…”, “Only…”, “…tax free” and so on. The first good news is this is very easy to do with a WooCommerce filter (remember, filters change the value of an existing variable, while actions add content). The second good news is that you don’t need to know PHP, just copy/paste my snippet!

woocommerce-add-prefix-suffix-price
WooCommerce: add prefix & suffix to product prices

PHP Snippet: Add Suffix to WooCommerce Prices


/**
 * @snippet       Adds translatable suffix WooCommerce Prices with price ex tax
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @source        https://businessbloomer.com/?p=472
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.1
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_filter( 'woocommerce_get_price_suffix', 'bbloomer_price_translatable_suffix', 99, 4 );

function bbloomer_price_translatable_suffix( $html, $product, $price, $qty ){
    $html .= ' ' . __( 'incl. VAT', 'bbloomer' ) . ' (' . wc_price( wc_get_price_excluding_tax( $product, array( 'qty' => $qty, 'price' => $price ) ) ) . ' ' . __( 'excl. VAT', 'bbloomer' ) . ')';
    return $html;
}

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares :) Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

82 thoughts on “WooCommerce: Add Prefix / Suffix to Product Prices

  1. Did anyone no a solution to show a prefix for for a specific attribute value in an product variation?

    1. Hey Thomas, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  2. Hi,
    is it possible to display the suffix only in Single Product Page? After I apply the snippet, the suffix also appear in the product thumbnail in Shop page and any page that display products.

    Thanks.

  3. Hello, I want to add a star(*) symbol as prefix before the price of products. Would you please tell me how could do that..? Advanced Thanks.

    1. Hello Rana, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  4. Hi Rodolfo,

    could you let me know how to add a link to the suffix?

    Thanks!
    Kris

    1. Hey Kris, try with this:

      $html .= '<a href="yourlink.com">Link</a>';
      
  5. Hallo Rodolfo,

    I applied your snippet and that works fine for all the shop and also per category. But I need the prefix to be applied only to the certain products.
    So, I used this:

    add_filter( 'woocommerce_get_price_html', 'bbloomer_price_prefix_suffix', 100, 2 );
     
    function bbloomer_price_prefix_suffix( $price, $product ){
     
    // example product ID = 555
         
        if(is_single(555)) {
        $price = 'different prefix here ' . $price . ' different suffix here';
        } else {
        $price = 'prefix here ' . $price . ' suffix here';
        }
     
        return apply_filters( 'woocommerce_get_price', $price );
    }
    

    It still applies the same prefix to all the products in all the shop. Can you suggest me anything, please? Would be very appreciated, thanx!

    1. Hey Diana – this should work:

      
      // wrong
      // if(is_single(555)) {
      
      // right
      if( $product->get_id() == 555 ) {
      
      
  6. PHP Snippet 1 works well, but the others give me whiteout pages! Not sure why.

    1. You’re right John, they were snippets for older versions of Woo. I’ve now removed them. Thank you!

  7. Really great! This hack was wonderful!

  8. Hi,

    I will use your snippet but it add it only to the price with TAX and I will also add it to the price without TAX.
    Please is possible to help me how to add it to the second price too? And is also possible top tell me if is possible to change the order the prices? Now I have without TAX and then with TAX and I will have it in other order.

    Thanks.

    1. Hi Peter, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here on the blog. Thanks a lot for your understanding! ~R

  9. Hi Rodolfo,

    Thanks for this code – it works brilliantly:

    But is it possible to apply it if a certain tax class is used (I.e. I’ve created one called ‘VAT included’) rather than a category?

    1. Hey Dean, thanks for your comment! Yes, of course that is possible, but unfortunately I can’t help directly. I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know 🙂

  10.  // Add prefix and suffix to prices //
    add_filter('woocommerce_price_html', 'custom_single_price');
    function custom_single_price() {
    $product = new WC_Product( get_the_ID() );
    $price = '';
    $price .= '<span class="price-prefix" style="color:#385d85";>Efectivo </span>';
    $price .= woocommerce_price($product->price);
    $price .= '<span class="price-suffix" style="color:#4b7eb3;font-size: 15pt"><br/> 12 Cuotas de </span>';
    $price .= woocommerce_price($product->price*1.30/12);
    return $price;
    }

    i need help for fix this problem in wc 3.0.4, In the previous version to 3.0 of wc It worked perfect this code , desde ya le agradezco la ayuda saludos desde argentina

    1. Hola Marcelo! I’m afraid you’re not using my code so I can’t help 🙂 Thank you!

    2. Yes, it is the fourth code of this page, PHP Snippet 4 (WC <2.4): add prefix & suffix to WooCommerce prices

    3. You’re right 🙂 Sorry, that won’t work with WooCommerce 3.0.4! Try using version 1 and let me know

    4. Yes, I work! thank you very much! 🙂

  11. There is also a filter used by WooCommerce called

    woocommerce_get_price_suffix

    Much simpler to use unless you need a prefix.

    1. Awesome Johannes, thanks a million for your useful comment!

  12. Works perfectly.

    1. Awesome, thanks Lasse!

  13. Hi there! Thank you so much for this code. It works beautifully. I’d like to apply it to the cart and checkout pages. I’ve tried :

    add_filter( 'woocommerce_cart_item_price', 'bj_woocommerce_get_price_html', 100, 2 );
    add_filter( 'woocommerce_get_price_html', 'bj_woocommerce_get_price_html', 100, 2 );
      
    function bj_woocommerce_get_price_html( $price, $product ){
      
    // change 'audio' with your the category slug
    if ( has_term( 'cookout-bundle', 'product_cat' ) ) {
        $price = $price . ' per bundle';
    }
    else $price = $price . ' per lb';  
    // no need to put the else! $price will stay the same
      
        return apply_filters( 'woocommerce_get_price', $price );
    }
    

    But, this returns $price per lb for everything in the cart regardless of it’s category. Why is the cart not able to see the category?
    Thanks for you help!

    1. Hey BJ, thanks for your comment 🙂 There are a few mistakes in your snippet, however I’m afraid this is custom work and I cannot provide a complementary solution here on the blog. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  14. Thanks for the helpfull post.
    I would like to have the date that the product was created in the suffix next to the price.
    Is this possible. Your assistance is appreciated.

    1. Thank you Wesley! I actually have a similar post here: https://businessbloomer.com/woocommerce-show-product-published-date/. You could try combine the two snippets and that would work 🙂 Let me know

    2. Thanks a stack, love your stuff!

    3. Awesome stuff! Cheers 🙂

  15. Hi is it possible to add a prefix before the normal price and before the sales prices?

    Like so:

    Normal price: € 99,- | Sale price: € 88,-

    With the above code it will be: Normal price: Sale price: €99,- | €88,-

    1. Hey Ronald, thanks for your comment! Yes, I’m sure there is a way 🙂 The filter “woocommerce_get_price_html” is probably not ideal in this case, as it affects the whole price. You’d need to look for the specific filters for the “regular” and “sale” price respectively. Hope this helps 🙂

  16. Hi Rodolfo,

    I used PHP Snippet 1, to set a HK$ prefix for all woocommerce prices. But how do add this prefix to the Cart and Checkout pages too, I thought this code would do the job too?

    I used the code like this, in my child-theme functions.php

    
    add_filter( &#039;woocommerce_get_price_html&#039;, &#039;bbloomer_price_prefix_suffix&#039;, 100, 2 );
     
    function bbloomer_price_prefix_suffix( $price, $product ){
        $price = &#039;HK&#039; . $price . &#039;&#039;;
        return apply_filters( &#039;woocommerce_get_price&#039;, $price );
    }
    
    

    Please kindly advise.

    Regards

    1. Hey MArc, thanks for your comment! If you just want to change the “currency symbol”, there is a much better way: https://docs.woocommerce.com/document/change-a-currency-symbol. Hope this helps!

    2. Hi Rodolfo, that custom code does not work on cart* or checkout* pages it still just only has the dollar sign $ ?

      Please kindly advise,

      Marc

    3. Marc, thank you for your comment! Could you kindly attach a screenshot so that I have something to start working on? Thanks!

  17. Hi Rodolfo,

    Which code should i use to have all woocommerce prices with a USD at the end or the amount? Example $100.00 USD

    And where should I paste the code in

    Please kindly advise.

    Thanks and Regards

    1. Hey Arthur, thanks so much for your comment! So, you can just Snippet 1 and remove the part with the prefix (you only need the suffix). You can place this in your child theme’s functions.php file – if you need more guidance, please take a look at this video tutorial: http://businessbloomer.com/woocommerce-customization-hangout/. Thanks!

  18. Hi Rudolfo,

    Thanks for this precious help !
    Did you now how to add suffix on single page product with discount ?

    Thanks !
    Simon

    1. Hey Simon, thanks for your comment! I guess you could add a “conditional” check before running the function, something along the lines of:

      global $product;
      if ( $product->is_on_sale() ) {
       // add suffix
      }
      

      Let me know 🙂

  19. Hi Rudolfo,
    could you let me know how to make the additional text/suffix into a clickable link?
    Thank you!

    1. Hey Andrea, thanks for your comment! This is not difficult, you simply need to add the HTML of the link like in this example:

      Hope this helps 🙂

  20. Hello, thanks for article !
    One question, some of my products are in 2 or 3 different product category… and my suffix appear 2 or 3 times 🙁
    How can I fix it?

    1. SG, thanks for your feedback! I guess you’re referring to Snippet #2? And I guess you’ve edited that snippet – can you copy/paste it here so that I can send you a fix? Thanks 🙂

    2. Hello, yes indeed, I refer to Snippet #2,
      here my code :

      
      add_filter( 'woocommerce_get_price_html', 'bbloomer_price_prefix_suffix', 100, 2 );
        
      function bbloomer_price_prefix_suffix( $price, $product ){
      
      if ( has_term( 'category_1','product_cat' ) ) {
          $price = . $price . ' / jour - TTC';
      } 
      if ( has_term( 'category_2', 'product_cat' ) ) {
          $price = . $price . ' / jour - TTC';
      }
      if ( has_term( 'category_3', 'product_cat' ) ) {
          $price = . $price . ' TTC';
      }
      if ( has_term( 'category_4', 'product_cat' ) ) {
          $price = . $price . ' / week-end - TTC';
      } 
      return apply_filters( 'woocommerce_get_price', $price );
      }
      
      
    3. Yes, it’s what I was afraid you had done 🙂

      Well, what should display if a product belongs to category 1 AND category 2? In your case, you’re basically applying the code twice… in this case, you will only apply the code once:

      
      if ( has_term( 'category_1','product_cat' ) && has_term( 'category_2', 'product_cat' )) {
          $price = $price . ' / cat 1 & cat 2';
      } 
      
      
    4. sorry, check this code :

      
      add_filter( ‘woocommerce_get_price_html’, ‘bbloomer_price_prefix_suffix’, 100, 2 );
      
      function bbloomer_price_prefix_suffix( $price, $product ){
      
      if ( has_term( ‘indoor’,’product_cat’ ) ) {
      $price = ” . $price . ‘ / jour – TTC’;
      }
      
      if ( has_term( ‘outdoor’, ‘product_cat’ ) ) {
      $price = ” . $price . ‘ / jour – TTC’;
      }
      
      if ( has_term( ‘transport’, ‘product_cat’ ) ) {
      $price = ” . $price . ‘ / jour – TTC’;
      }
      
      if ( has_term( ‘events’, ‘product_cat’ ) ) {
      $price = ” . $price . ‘ / week-end – TTC’;
      }
      
      if ( has_term( ‘others’, ‘product_cat’ ) ) {
      $price = ” . $price . ‘ – TTC’;
      }
      
      if ( has_term( ‘indoor’,’product_cat’ ) && has_term( ‘outdoor’, ‘product_cat’ ) && has_term( ‘transport’, ‘product_cat’ )) {
      $price = $price . ‘ / cat indoor & cat outdoor & cat transport’; // HERE I NEED “/ jour – TTC”
      }
      
      if ( has_term( ‘indoor’,’product_cat’ ) && has_term( ‘outdoor’, ‘product_cat’ ) && has_term( ‘transport’, ‘product_cat’ ) && has_term( ‘events’, ‘product_cat’ )) {
      $price = $price . ‘ / cat indoor & cat outdoor & cat transport & cat events’; // HERE I NEED “week-end – TTC”
      }
      
      // no need to put the else! $price will stay the same
      return apply_filters( ‘woocommerce_get_price’, $price );
      }
      
      
    5. Hey SG 🙂 I guess you can’t have both checks:

      
      if ( has_term( ‘indoor’,’product_cat’ ) ) {
      
      

      AND

      
      if ( has_term( ‘indoor’,’product_cat’ ) && has_term( ‘outdoor’, ‘product_cat’ ) && has_term( ‘transport’, ‘product_cat’ )) {
      
      

      So, you should do something like this:

      
      if ( has_term( ‘indoor’,’product_cat’ ) && has_term( ‘outdoor’, ‘product_cat’ ) && has_term( ‘transport’, ‘product_cat’ )) {
      ....
      } elseif ( has_term( ‘indoor’,’product_cat’ ) ) {
      ....
      } elseif ( has_term( ‘outdoor’,’product_cat’ ) ) {
      ....
      }
      
      

      and so on… Basically you check first if the product has multiple categories, otherwise (elseif) you check each single category. Hope this helps!

  21. I tried this but I want to exclude one category from it. I tried this code and it doesn’t change anything. The suffix is still being used on all categories:

    
    add_filter( 'woocommerce_variable_price_html', 'bbloomer_price_prefix_suffix', 100, 2 );
     
    function bbloomer_price_prefix_suffix( $price, $product ){
     
        if(has_term('baby-headbands','product-cat')) {
        $price = ' ' . $price . ' ';
        } else {
        $price = ' ' . $price . ' +';
        }
     
        return apply_filters( 'woocommerce_variable_price', $price );
    }
    
    
    1. Hey, thanks for your comment 🙂 I just tested this on WooCommerce 2.5.3 and it works, so a couple of possible ideas:

      1) This snippet only works for variable products. Are you trying to apply this to simple products as well?
      2) Is “baby-headbands” the exact category slug (not category name)?

      Let me know 🙂

  22. Hi Rodolfo thanks for your post. I’ve implemented your code into my functions.php file now i have the currency showed by woocommerce and my customized suffix. How can i only show my suffix and not the default one as well?
    Many thanks

    1. Hey Vicio, thanks for your comment 🙂 You probably need to edit the relevant box under WooCommerce Settings > Tax > Tax Options: http://screencast.com/t/QmZmu8eC1c29

  23. Hello Rodolfo,
    Great Article! I was wondering if you could help me. I would like to add tax rate as a suffix, for example: Price 350$ (VAT – 20%).
    I have two different tax rates – Standard (20%) and reduced (10%).

    Thanks!

    1. Milan, thanks so much for your feedback! Yes, this is possible – but unfortunately this is custom work and I cannot provide this solution on the blog right now. If you would like to get a quote, feel free to go here. Thank you! R

  24. Hello Rodolfo,
    thank you for this tip. How to make 2 different prefix? First for price with tax Second for price without tax ? And I would like make if price is empy “call me for price” and don’t show prefix. Thank you for answer.

    1. Hey Pawel thanks for your comment! In regard to tax, could you explain that a little better – maybe with a screenshot?

      For the empty price, you can put an IF statement: if ( $price = ” ) { echo ‘Call me for price’; } or something like that 🙂

    2. Hi Rodolfo thank for reply. Ye It will be better when I put screenshots and descpribe it one more 🙂
      1. I would like 2 suffix:
      First – “BRUTTO” – this is price with tax
      Second – “NETTO” – this is price without tax
      2. If price is empty i would like text “call for price”
      3. In woocommerce panel I entered the price with tax (BRUTTO suffix)

      1.
      http://www.gabski.pl/ss/category-page.png
      http://www.gabski.pl/ss/single-product-page.png
      2.
      http://www.gabski.pl/ss/category-page-empty-price.png
      http://www.gabski.pl/ss/single-product-page-empty-price.png

      Thank for help

    3. Ah ok 🙂 Now it’s clear! In regard to the tax and non-tax price, you need to duplicate the price, and then add a prefix/suffix to it 🙂 You can echo the non taxed price in this way: echo $product->get_price_excluding_tax(). Let me know 🙂

    4. Thank for reply.
      Now I have problem, I don’t know how to hide price without tax if price is empty.
      http://www.gabski.pl/ss/no-price.png

      My files:
      http://gabski.pl/ss/function-call-for-price.txt
      http://gabski.pl/ss/price-from-loop.txt
      price-from-single-product.txt

      thank for help

    5. I’d suggest where you say:

      # echo woocommerce_price($product->get_price_excluding_tax()) . ‘ netto’ #

      to use this instead:

      # if ($product->get_price_excluding_tax() !== 0) { echo woocommerce_price($product->get_price_excluding_tax()) . ‘ netto’ } #

  25. Hello,
    how to add prefix or suffix to variations?
    Any help would be really appreciated.

    1. Hello, thanks so much for your feedback! Yes, this is possible – but unfortunately this is custom work and I cannot provide this solution on the blog right now. If you would like to get a quote, feel free to go here. Thank you! R

  26. Hi, I am really thankful you share this snippet. On the conditional snippet you shared for single product, may I know how do I do likewise for a product category?
    For a product category that I do not wish to apply prefix or suffix, what should I do?
    use the empty prefix or suffix string like below?
    $price = ‘ ‘ . $price . ”;

    Thanks!

    1. Hi Lee 🙂 You could just check if the product is in a certain category – and if not do not apply the filter. Example:

      
      /**
       * @snippet       Adds prefix and/or suffix to WooCommerce Prices (conditionally per category)
       * @how-to        Watch tutorial @ http://businessbloomer.com/?p=19055
       * @source        http://businessbloomer.com/?p=472
       * @author        Rodolfo Melogli
       * @compatible    WooCommerce 2.4.7
       */
      
      add_filter( 'woocommerce_get_price_html', 'bbloomer_price_prefix_suffix', 100, 2 );
      
      function bbloomer_price_prefix_suffix( $price, $product ){
      
      // change 'audio' with your the category slug
      if ( has_term( 'audio', 'product_cat' ) ) {
          $price = 'prefix here ' . $price . ' suffix here';
      } 
      
      // no need to put the else! $price will stay the same
      
          return apply_filters( 'woocommerce_get_price', $price );
      }
      
      
    2. Thanks, I replace the category slug, id or category name into the snippets ( has_term(…, I don’t see the suffix change.

      Does this snippets checks individual products on the page (i.e. Homepage, Category page, product page) which product category it belongs to and renders the prefix/suffix?

    3. Lee, I just tested this and it works perfectly on every page (single product, category, homepage). You have to use the category slug e.g. has_term( ‘category-slug-here’, ‘product_cat’ ). If it does not work on your website, maybe you have another plugin that is filtering that function?

    4. Hi Rodolfo, I made a silly mistake of including multiple categories slug in the has_term. I changed it and it works perfect. Thanks for this fantastic code! Appreciate your help!

    5. Hi Rodolfo, I did used this code here and it worked better than I expected and I had a question. So I used this code but i don’t need a Prefix, just a suffix and only on products that belong to a specific category.

      I notice that this code adds my suffix after the price, on both the Product Page, and the Category page. I wanted to ask you how it is that its being displayed on both? Does WP assume it should go on both unless its specified somewhere?

      Just curious 🙂

      Thank you

    6. Hello Chris, thanks for your comment! I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know 🙂

  27. Doesn’t work for me, I get error:

    Warning: Missing argument 2 for RP_WCCF_Product::maybe_change_price() in /home/quarrysc/public_html/test/wp-content/plugins/woocommerce-custom-fields/includes/classes/rp-wccf-product.class.php on line 594

    1. Hey Alan, thanks for your feedback. That to me looks like a problem with your plugin, not the snippet. Can you check if your plugin is updated?

  28. Hi Rodolpho i think it’s just what i was looking for but i need to adapt to my own purpose. On my website all products are VAT free (HT) and this is set as i want it to be, except for one product which i need to be with VAT include (TTC). it has of course a specific Id but i don’t know how to set it with your code. can you help ? thanks a lot

    1. If I understand well you want this snippet to be conditional. Take a look at this:

      
      /**
       * @snippet       Adds prefix and/or suffix to WooCommerce Prices [conditionally]
       * @how-to        Watch tutorial @ http://businessbloomer.com/?p=19055
       * @source        http://businessbloomer.com/?p=472
       * @author        Rodolfo Melogli
       * @compatible    WooCommerce 2.4.7
       */
      
      add_filter( 'woocommerce_get_price_html', 'bbloomer_price_prefix_suffix', 100, 2 );
      
      function bbloomer_price_prefix_suffix( $price, $product ){
      
      // example product ID = 555
          
          if(is_single(555)) {
          $price = 'different prefix here ' . $price . ' different suffix here';
          } else {
          $price = 'prefix here ' . $price . ' suffix here';
          }
      
          return apply_filters( 'woocommerce_get_price', $price );
      }
      
      
  29. I tried this in Woo 2.4.8, but it doesn’t work, it adds the prefix but returns a price of 0.00. It looks like the woocommerce_price function has been deprecated, but I can’t figure out what to replace it with. Do you have any suggestions? Thank you!

    1. Danielle, I’ve checked that and updated the snippet (see snippet #1). Hope this helps!

  30. This is just what I need but where do I place this code?

    Thanks in advance!

    1. Awesome Matt, thanks for the feedback! You can place the code in your theme’s functions.php – or otherwise if you don;t want to edit that, install a plugin called Code Snippets and copy/paste the function there. Hope this helps!

Questions? Feedback? Support? Leave your Comment Now!
If you're writing code, please wrap it between: [php] code_here [/php]

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.