Add Text After the Price in a WooCommerce Product

You can add a bit of static text after the price in a WooCommerce Product by using a woocommerce filter woocommerce_get_price_html.

woocommerce-price-text

For the above product I want to add some text just after the price to a certain range of products. Here below is the code to do that:


So I have set my target products in an array and the additional product text as a variable $textafter, if the product is part of that array it will return the prices and also the additional text which also has a CSS class applied for styling. If the product is not part of that array it will just return the price.

This will occur in the listing and detailed view product pages as the $post->ID is also included in the conditional code.

11 Comments

  1. Max on August 11, 2020 at 1:25 am

    Hi there,

    thanks a lot for this snippet. It works fine on my shop and single product pages.

    However, the text doesn’t appear with products listed in the cross-sell section of the single product page. Is this somehow possible, please?

    Many thanks.
    Max

  2. Terence on September 10, 2015 at 5:01 am

    Hi,

    Thanks for the great tip! I was wondering is it possible to show different texts on variable products?

    For example:
    If I select red apple, $4 (for 1 pack)
    But if select green apple $4 (for 2 packs)

    Thank you
    Terence

  3. Neil on September 4, 2015 at 7:17 pm

    Hi there.

    Thanks for this awesome solution. Like Mark above I’m also having an issue with displaying per category.

    What I’d like to do is add the text after a specific category only.

    I’ve tried your example and messed around with the code but haven’t got much further.

    Any help much appreciated!

    Thanks,

    Neil

  4. Umberto on August 24, 2015 at 2:01 pm

    Hi i am trying this on the latest version of woocommerce. But when i add it to my functions.php file the website loads on a blank page, and i have to restore the functions.php file through ftp in order to get it back to normal. Any help?

  5. Umberto on August 24, 2015 at 1:05 pm

    Does this work with the latest version of woocommerce?

  6. Razvan on August 19, 2015 at 9:23 pm

    Hello,
    What can I do If I want to put this for all my products?

    Thank you!

  7. Bart on August 4, 2015 at 12:57 pm

    Hi,

    Thanks! Awesome snippet. I was wandering if it is possible to put the text next to the price instead of placing it on the next line.

  8. Jason on March 27, 2015 at 10:33 am

    Great snippet thanks very much,
    was wondering if its possible to filter this per category ie only for category one, three, and five?
    Thanks

    • Neil Gee on March 27, 2015 at 11:16 pm

      Try adjusting the conditional…
      if ( in_array( $product_id, $my_product_array ) && in_category( array( 1,3,5 ) ) {
      //code goes here
      }

      • mark on May 14, 2015 at 10:06 am

        hi, thanks for this great snippet..

        although the array for the product worked, but the one in the comment for the categories didnt, my snippet is:
        function themeprefix_custom_price_message( $price ) {

        global $post;

        $product_id = $post->ID;
        $my_product_array = array(14460,12767);//add in product IDs
        if ( in_array( $product_id, $my_product_array ) && in_category( array(268)) ) {
        //$textafter = ‘Per day’; //add your text
        $monthly_price= get_post_meta(get_the_ID(), ‘monthly_price’, TRUE);
        return $price . ‘ ‘ . $monthly_price .” ;
        }

        else {
        return $price ;
        }
        }
        add_filter( ‘woocommerce_get_price_html’, ‘themeprefix_custom_price_message’ );

        can you tell whats wrong in it please?

        thanks
        mark

  9. George on March 5, 2015 at 1:50 am

    Was looking how to do this for the website I listed. Great solution thank you.

Leave all Comment