I am trying to edit the price value for a single product.

In single-product/price.php there is a template call to $product->get_price_html. How can I edit that function/method to change the way the HTML is presented?

At the moment even if I delete all the contents of the function located in class-wc-product it still miraculously displays? Anyone know why?


Core and plugin files should never be edited directly, as any updates could overwrite your changes. If you look in WooCommerce source at the get_price_html method, there are a number of filters available to modify the output of the function.

See add_filter in Codex for more info on adding filters to apply_filters calls.

From get_price_html in class-wc-product:

return apply_filters('woocommerce_get_price_html', $price, $this);

So to add your own filter:

add_filter( 'woocommerce_get_price_html', 'wpa83367_price_html', 100, 2 );
function wpa83367_price_html( $price, $product ){
    return 'Was:' . str_replace( '<ins>', ' Now:<ins>', $price );
  • Thanks for the answer, by the way why when I delete the contents of the main function it still returns the output as normal – Lucky Luke Jan 27 '13 at 20:09
  • 1
    So lets say if there was a sale on and it returns me <del>£2</del><ins>£1</ins>, how can i change that into Was:<del>£2</del> Now:<ins>£1</ins> with a filter? – Lucky Luke Jan 27 '13 at 20:19
  • not sure, not too familiar with WooCommerce, perhaps another class extends it. see edit above for your second question. – Milo Jan 27 '13 at 20:27
  • Brill, ;), great help – Lucky Luke Jan 27 '13 at 20:33
  • I am trying to know that what happening in default woocommerce_get_price_html filter for $price. In my site,woocommerce shows 0$ for free products instead Free! – SKMohammadi Dec 7 '16 at 9:42
function wpa83368_price_html( $price,$product ){
   // return $product->price;
    if ( $product->price > 0 ) {
      if ( $product->price && isset( $product->regular_price ) ) {
        $from = $product->regular_price;
        $to = $product->price;
        return '<div class="old-colt"><del>'. ( ( is_numeric( $from ) ) ? woocommerce_price( $from ) : $from ) .' Retail </del>  | </div><div class="live-colst">'.( ( is_numeric( $to ) ) ? woocommerce_price( $to ) : $to ) .'Our Price</div>';
      } else {
        $to = $product->price;
        return '<div class="live-colst">' . ( ( is_numeric( $to ) ) ? woocommerce_price( $to ) : $to ) . 'Our Price</div>';
   } else {
     return '<div class="live-colst">0 Our Price</div>';
  • 5
    Even if your code can work (and I have no reason to think it doesn't) this is a Q/A site, not a code repository so it's better share expertice and knowledge explaining your code, instead of just write code with no explaination nor inline comments... – gmazzap Jan 3 '14 at 14:38
  • the code also uses object properties which is not good. – Svetoslav Marinov May 8 '18 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.