PHP Tutorials:How to change the price for all products programmatically in WooCommerce?

There are 2 methods to do this.

  1. Through the mysql database.
  2. Through PHP code in functions.php file

Now it depends upon your requirement that what exactly you need.

Update price through the database

Login to your cPanel and goto PHPmyadmin. Select your WordPress database. Click the SQL tab and paste this command.

UPDATE wp_postmeta SET `meta_value` = 'YOUR PRICE' WHERE `meta_key`='_price'

Click the run button. The price of all products will get updated to the price you set in the ‘YOUR PRICE’.

Update price through the functions.php file of the theme

The first method might not work for you or someone else. If you need to do some logical with the price then you can use the following.

Login to your FTP and navigate to wp-contentthemesyourthemedirectory. Open the functions.php file in a text editor. Creating a shortcode and placing the code inside in function will be a wise way to do that. Otherwise, your code will keep on executing if someone opens your website. Here is the code for you.


add_shortcode( 'shotcode_update_price','shotcode_update_price' );
function shotcode_update_price(){
	global $wpdb;

	$prefix = $wpdb->prefix;
	$results = $wpdb->get_results( "SELECT `meta_id`, `meta_value` FROM {$prefix}postmeta WHERE `meta_key`='_price'", ARRAY_A );
	if ( !empty($results) ) {
		foreach ($results as $key => $value) {
			$new_price = $value['meta_value'] * 1.30; //add 30% to price
			$wpdb->query( "UPDATE {$prefix}postmeta SET `meta_value`=$new_price WHERE `meta_id`=$value[meta_id]" );


After adding this code to functions.php file. Goto WP admin and add a new page. Use the shortcode [shotcode_update_price]. Publish the page and view in frontend.

I sure this will solve your problem. If you face any error or any issue comment here.

Leave a Reply

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