A common WooCommerce request is “How do I add text before the price” or “Can I put text before the price when the product is on sale?”, so I decided to write a plugin to make that easier for people that don’t like getting dirty with code and function files and all that good stuff.

I give you WooCommerce RRP, check it out, or you can download it from the WordPress.org repo – WordPress RRP. A simple plugin that allows anyone to add text before the price without the need to write any code or change any template files.

If you don’t want to use my plugin, that’s cool I guess, two ways you can do it:

  1. Write some PHP in your functions file, or
  2. Write some CSS in your style file

Option 1: Add Text Before The Regular And/Or Sale Price With Code

Here are three snippets of code you can use:

  • First example give you the ability to add text before the regular price and sale price. You can change “RRP:” or “Sale Price:” to what you require
  • Second example will allow you to change the text before the “Regular Price” only. You can change “RRP:” to what you require
  • Third example allows you to change the text before the “Sale Price” only. You can change “Sale Price:” to what you require

Add text before regular price and sale price

function bd_rrp_sale_price_html( $price, $product ) {

  if ( $product->is_on_sale() ) :
    $has_sale_text = array(
      '<del>' => '<del>RRP: ',
      '<ins>' => '<br>Sale Price: <ins>'
    );
    $return_string = str_replace(array_keys( $has_sale_text ), array_values( $has_sale_text ), $price);

  else :
    $retun_string = 'RRP: ' . $price;
  endif;

  return $return_string;
}
add_filter( 'woocommerce_get_price_html', 'bd_rrp_sale_price_html', 100, 2 );

Add text before regular price only

function bd_rrp_price_html( $price, $product ) {

  $retun_string = 'RRP: ' . $price;
  return $return_string;

}
add_filter( 'woocommerce_get_price_html', 'bd_rrp_price_html', 100, 2 );

Add text before sale price only

function bd_sale_price_html( $price, $product ) {

  if ( $product->is_on_sale() ) :

    $return_string = str_replace( '<ins>, '<ins><br>Sale Price: ', $price);
    return $return_string;

  else :
    return $price;
  endif;

}
add_filter( 'woocommerce_get_price_html', 'bd_sale_price_html', 100, 2 );

Option 2: Use CSS To Add Text Before The Price

As all themes are a little different this exact CSS might not work, but hopefully you will get the idea on how you can do it and then apply it to the right selector on your theme. This technique uses the ::before selector that was introduced in CSS2, read all about it here.

Add text before the price on a product that is not on sale

.amount::before {
  content: 'RRP:';
  margin-right: 5px;
}

Add text before the sale price (ins) and before the regular price (del)

del .amount::before,
ins .amount::before {
  margin-right: 5px;
}

del .amount::before {
  content: 'RRP:';
}

ins .amount::before {
  content: 'Now:';
}

And of course if you need to change the content you add for a specific product or category, you can using different CSS selectors, you might find some handy hints at the holy grail for WordPress info – Codex: Post Class

  1. Your plugin is great. We moved to a Word Press MultiUser setup and with for some reason the plugin no longer worked?! 🙁

    We were able to follow your instructions above.

    Wanted a line break and added this:

    del .amount::after {
    content: “\000A”;
    white-space: pre;
    }

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>