We are planning to start a campaign. We want to give a temporary 10% discount on every product we sell on our site for the next 30 days. Ideally, we’d like to display both the regular price and the discounted price.
- Our shop is built on WooCommerce.
- Our products don’t have variations.
- We have approximately 200,000 products.
- Most tools struggle to handle that many products.
How to do this?
2
Answers
How I have done it:
_sale_price
and_price
with the data from_regular_price
multiplied by0.9
(10%)._sale_price
and updating_price
with_regular_price
by executing SQL queries.Warning: This solutions don't work with variations!
Add the discount to all products
If the products aren't on sale, they don't have
_sale_price
meta_key in the database tablewp_postmeta
. To add these, we can loop trough all products and useupdate_post_meta
. When working with bigger databases, I suggest splitting the update into smaller batches. I used1000
per batch in the code below. If you want the query for all your products at once, replace'posts_per_page' => 1000
with'posts_per_page' => -1
.Warning: This code will not work properly if any of your products already have a
_sale_price
!Remove the discount
I am using SQL queries, because it's fast and easy. If you are looking for an alternative way to do it, check this post: Remove Sale Price from all products in WooCommerce admin
Warning: These SQL queries will delete all your existing sale prices!
Delete all products'
_sale_price
:Update products'
_price
with the values of_regular_price
:References:
Updated (allowing products with zero price and resetting existing sale prices)
Instead of overwriting products prices as you suggest in your comment, you could use the following to add a temporary global discount, that keeps the original product price with a general sale price with a 10% discount:
Code goes in functions.php file of your child theme (or in a plugin). Tested and works. It could work with your very large catalog.