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.
By | 2017-09-13T17:11:30+00:00 August 18th, 2014|Blog|6 Comments

About the Author:

6 Comments

  1. francesco January 31, 2015 at 6:21 pm - Reply

    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

  2. Pixel Theme Studio March 12, 2015 at 10:15 pm - Reply

    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!

  3. Jonny June 30, 2015 at 6:53 pm - Reply

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

  4. kiamaru August 8, 2015 at 4:07 pm - Reply

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

  5. mehdi June 28, 2016 at 7:56 pm - Reply

    very tanks

  6. Akkisha August 21, 2017 at 3:48 am - Reply

    Works fine in product archieve and product pages

    But

    If we add to cart the product
    It shows price for the product as zero

Leave a Reply