How To Update Product Price In Woocommerce Programmatically

As a skilled WooCommerce developer, I have frequently come across the requirement to programmatically update product prices. In this article, I will walk you through the steps of updating product prices in WooCommerce using custom coding. This approach is especially helpful when dealing with a large quantity of products and needing to make price changes in bulk.

Understanding the WooCommerce Product Structure

Before we dive into the code, let’s first understand how WooCommerce structures its products. In WooCommerce, products are stored as a custom post type called “product”. Each product has various meta data associated with it, such as the price, SKU, and stock status.

To update a product’s price programmatically, we will be modifying the meta data associated with the product. Specifically, we will be updating the “_regular_price” and “_sale_price” meta fields.

Getting Started with Custom Code

To update product prices programmatically, you will need to add custom code to your theme’s functions.php file or create a custom plugin. Let’s assume that you are adding the code to your theme’s functions.php file.

First, open your theme’s functions.php file and add the following code:


function update_product_price_programmatically() {
$product_id = 123; // Replace with your product ID
$new_price = 19.99; // Replace with the new price

update_post_meta( $product_id, '_regular_price', $new_price );
update_post_meta( $product_id, '_sale_price', $new_price );
}
add_action( 'init', 'update_product_price_programmatically' );

In the code above, we define a function called “update_product_price_programmatically”. Inside this function, we specify the product ID of the product we want to update and the new price. Then, we use the “update_post_meta” function to update the “_regular_price” and “_sale_price” meta fields with the new price.

We also use the “add_action” function to hook our custom function to the “init” action. This ensures that our function is executed when WordPress initializes.

Updating Multiple Products

If you have multiple products that need their prices updated, you can modify the code to loop through an array of product IDs and update each one individually. Here’s an example:


function update_multiple_product_prices_programmatically() {
$product_ids = array( 123, 456, 789 ); // Replace with your product IDs
$new_price = 19.99; // Replace with the new price

foreach ( $product_ids as $product_id ) {
update_post_meta( $product_id, '_regular_price', $new_price );
update_post_meta( $product_id, '_sale_price', $new_price );
}
}
add_action( 'init', 'update_multiple_product_prices_programmatically' );

In the code above, we define a function called “update_multiple_product_prices_programmatically”. We specify an array of product IDs that need to be updated and the new price. Then, we use a foreach loop to iterate through each product ID and update the prices.

Conclusion

Updating product prices in WooCommerce programmatically can save you significant time and effort, especially when dealing with a large number of products. By utilizing custom code, you can easily make price adjustments in bulk and keep your online store up-to-date.

Remember to always test your code on a staging site before making any changes to your live website. Incorrect code implementation can lead to unintended consequences. With this knowledge, you are now equipped to confidently update product prices programmatically in WooCommerce.