How to Change the WooCommerce Price via Functions.php

Recently, we had had a project where we needed to show a discounted price on woocommerce products without editing the price in the database of our WP install. Thanks to WooCommerce’s handy hooks, this was pretty simple. Here’s our code snippet.

 

function return_custom_price($price, $product) {
    global $post, $blog_id;
    $price = get_post_meta($post->ID, '_regular_price');
    $post_id = $post->ID;
    $price = ($price[0]*2.5);
    return $price;
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);
In this example, we needed to raise our base price by 2.5x, but you could use this to modify the price in almost anyway you’d like.

5 Comments

  • francesco

    January 31, 2015 - 6:21 pm

    Hello.
    and i i wish display “price on request” instead “free” when price is null or 0 is possibile with some variation of this code?
    regards
    francesco

    Reply to comment
  • Pixel Theme Studio

    March 12, 2015 - 10:15 pm

    Hello Francesco, unfortunately, you wouldn’t be able to do the price on request this way. If I get some time, I’ll try to post a tutorial of this!

    Reply to comment
  • Jonny

    June 30, 2015 - 6:53 pm

    This is exactly what i have been looking for, but unfortunately it just sets all my prices to £0.00 … Any suggestions ? Thanks

    Reply to comment
  • kiamaru

    August 8, 2015 - 4:07 pm

    hi
    don’t work with variation product. give price free.
    how use it with variation product.
    regards

    Reply to comment
  • mehdi

    June 28, 2016 - 7:56 pm

    very tanks

    Reply to comment

Leave a Review

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