How to change WooCommerce product prices programmatically

An easy two step guide on how to overwrite prices in your Woocommerce shop programatically.

Step 1 – prerequisites

In case you have access to your themes functions.php file, create a backup of it as the first step. Please never use the theme editor in your WordPress dashboard, it will give you a lot of headaches.

If you don’t have access to the functions.php file, download a plugin like the My Custom Functions which gives you the ability to add custom PHP code to your website. For the beginners I recommend this option.

Step 2 – the code

To overwrite the prices in Woocommerce you have two entry points:

  • overwrite the prices displayed in your shop globally
  • overwrite the price before adding the product to the cart (the price is changed only in your cart)

The rest of the checkout process is handled by the Woocommerce engine.

As an example let’s say you would like to raise the price of every product in your shop by 20%:

/**
* Display the custom price in the shop
*/
function fdev_return_custom_price($price, $product) {
// important lines to handle product discounts!
if($product->is_on_sale()){
return $product->get_sale_price();
}
// change the price display
$price = number_format($price * 1.2, 2);
return $price;
}
add_filter('woocommerce_product_get_price', 'fdev_return_custom_price', 10, 2);

And the same 20% price change, but this time you would like to apply the price change in the cart only and leave the prices in your shop intact:

function fdev_custom_price_to_cart_item( $cart_object ) {
foreach ( $cart_object->cart_contents as $key => $value ) {
$product_id = $value['data']->get_id();
$product = wc_get_product($product_id);
// important lines to handle product discounts!
if($product->is_on_sale()){
continue;
}
$new_price = number_format($product->get_price() * 1.2, 2);
$value['data']->set_price($new_price);
$value['new_price'] = $new_price;
}
}
add_action( 'woocommerce_before_calculate_totals', 'fdev_custom_price_to_cart_item' );

In case you encounter any errors, just delete the code from your functions.php file (alternatively replace functions.php with your backup file), or remove the code from the Custom Functions plugin. Happy coding!

Do you have a project for us?

Contact us

Our Services

WEB DESIGN AND DEVELOPMENT

From raw idea to a scalable product
Each website we develop is truly unique, tailor-made for your brand, product or company.

Learn more »

WORDPRESS & WOOCOMMERCE

Custom online-store, theme and plugin development
GoWild Digital leverages the flexibility of the WordPress platform to develop websites that can be easily maintained and upgraded.

Learn more »

TECHNICAL SEO AUDIT

A health check of your website
Our Technical SEO audit identifies technical errors with your website that can be hindering your performance and visibility.

Order now »

WORDPRESS SPEED OPTIMIZATION

Enhance your site’s user experience and rankings
Website speed optimization service that can help your website load at top speeds and generate more conversions.

Order now »

AD-HOC SUPPORT AND MAINTENANCE

For a fixed price
Purchase a block of support minutes to complete any tasks needed. Minutes purchased never expire.

Order now »

WEBSITE MIGRATION

We can help you to migrate your website to your new web hosting
Let us take on the heavy lifting of your next website migration to reduce your stress and to migrate your site efficiently and effectively

Order now »

Update Gone Wrong

An update broke your WordPress site?
Let us help you! Our service provides you with the fix of one error to get your WordPress website back up and running.

Order now »

Repair Hacked Website

Site hacked?
We’ll restore your website fast to help limit the downside. If it gets reinfected in the next 30 days, we fix it for FREE again.

Order now »

Quick Website Repair

Broken WordPress or PHP based website?
Our service provides you with the fix of one error to get your website back up and running.

Order now »

Our Work

Green Guide Budapest

Green Guide Budapest

greenguide.hu
Web design: Mimma Nosek (Behance)
UI/UX mentor: Júlia Tóth (Behance, Dribble)

Trilla

Trilla

trillafesztival.hu

Web design: Anuri Design Studio (Website)

Barber Bay

Barber Bay

barberbay.hu

Web design: Endre Gömöri (LinkedIn)

MOST

MOST

mostmusic.eu

Web design: Anuri Design Studio (Website)

Hungarian World Music Guide

Hungarian World Music Guide

hungarianworldmusicguide.com

Web design: Anuri Design Studio (Website)

Have a question?