In this tutorial, you will learn how to update product prices in the WooCommerce shop by code. You can either increase the prices or discount them.
You can either increase or decrease the price of all products or only certain categories of products by a percentage.
Update WooCommerce product price programmatically
Add The Following Code to the functions.php File
In the WordPress dashboard, go to Appearance ➡ Theme File Editor and copy the following code into the theme’s functions.php file and save it.
You must create a child theme before making any changes to functions.php
file. Otherwise, the applied changes will be lost after each update.
Create child theme in WordPress step by step [without plugin]
As an alternative method, you can use the Code Snippets plugin to insert your codes into WordPress.
/* Changing the price of WordPress products dynamically by redpishi.com */
function filtering_product_prices( $price, $product ) {
$price = (float)$price;
$new_price = $price * 1 ; // New price formula
// paste conditional codes here
return ceil($new_price);
}
add_filter('woocommerce_product_get_price', 'custom_price', 90, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 90, 2 );
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price' , 99, 2 );
add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variation_price', 99, 3 );
function custom_price( $price, $product ) {
wc_delete_product_transients($product->get_id());
return filtering_product_prices( $price, $product );
}
function custom_variation_price( $price, $variation, $product ) {
wc_delete_product_transients($variation->get_id());
return filtering_product_prices( $price, $product );
}
After adding the above code to the “functions.php” file, you can change the price of the products to your liking by giving the desired formula to the two variables “$price” and “$new_price”.
Below we show how to change the price of products with some examples.
Add 20% to the price of all products
To increase the price of all products by 20%, set the “$new_price” variable to the following formula.
$new_price = $price * 1.2;
Price reduction by 10%
To reduce the price of products by 10%, set the variable “$new_price” equal to the following formula.
$new_price = $price * 0.9;
Changing the price of products of a category
To apply a filter on the products of a category or tag, we use the method described in the following article.
How to add PHP code in WordPress [without plugins]
By putting the following formula in the “conditional codes” section, the price of “hats” category products will increase by 10 units.
if (has_term( 'hats', 'product_cat' )) {
return ceil((float)$price + 10);
}
In the next example, we add 5 units to the price of all t-shirts and subtract 6 units from the price of hoodies.
if (has_term( 'tshirts', 'product_cat' )) {
return ceil((float)$price + 5);
} elseif (has_term( 'hoodies', 'product_cat' )) {
return ceil((float)$price - 6);
}
Increasing the price of cheap products
With the following formula, you can set the price of all products cheaper than 5 units equal to 5.
if ( $price < 5 ) {
return 5;
}
If this article is difficult for you to read in text, you can watch the video version below.
That also what I think, but the person who responsible for changing the prices doesn\’t want to do it that way… But anyway… Thank you so much for the script…
Hi,
I have succeeded adding 15% on all prices for 10K products that I have. The problem is, when I create new product or update prices for promotion prices for instance, the prices are added by 15% automatically, which is we don\’t want that.
Any solution for this? Please advise.
Thank you
Hi Martin,
To work around this, you could manually reduce the prices of new products and promotions by 15% before submitting them.
While this may not be the most elegant solution, it is a simple method to address the issue you\’re facing.