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;
Add 20% to the price of all products in Woocommerce

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;
Price reduction by 10% in Woocommerce

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;  
} 
Increasing the price of cheap products in Woocommerce

If this article is difficult for you to read in text, you can watch the video version below.

Share this post
Maya
Maya

Hi, my name is Maya and I’m a WordPress plugin developer. I created this website to share some of the helpful codes that I’ve used in my own projects.
If you’re looking for a custom plugin for your website, you can contact me by clicking on Hire a developer in the menu. I’d love to hear from you.

Articles: 54

Leave a Reply

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