How to Bulk Reset WooCommerce Product Stock Daily
In this tutorial, I’ll show you how to set up an automatic product stock reset in a WooCommerce store. For example, let’s assume that “Product A” has a stock quantity of “10” at the beginning of the day. During the day, it may have some sales, but it doesn’t matter how many sales it had; you want it to have the same stock quantity tomorrow morning.
You may need functionality like this as a part of risk management measures, or maybe you just need to launch only a limited amount of sales of a specific digital product.
Anyway, below we will take a look at two methods on how we can perform an automatic daily (or weekly or whatever) stock reset on your WooCommerce store – we’ll use a programmatic approach, and I will also show how to do it using two stores and a plugin.
Method 1. Programmatically
Let’s start with a programmatic approach; however, maybe that’s not what you need, and you would prefer to do it without touching any code at all – in that case, the second part of the tutorial is for you.
Bulk update product stock quantities
First things first, we will need to store original stock quantities somehow, which we will need to set for our products every day. It can be provided easily in an array:
$set_stock_quantities = array(
array(
'product_id' => 5,
'stock_qty' => 10,
),
array(
'product_id' => 15,
'stock_qty' => 110,
),
// etc.
);The product_id array key can also contain variation IDs – there is no problem with that.
You can also simplify the array this way:
$set_stock_quantities = array(
5 => 10,
15 => 110,
// etc.
);Great, now, once you have this array, you can easily update product stock quantities in bulk. Let’s use the second simplified version of the array.
foreach( $set_stock_quantities as $product_id => $stock_qty ) {
// let's try to get this product (or variation) first
$product = wc_get_product( $product_id );
if( ! $product ) {
// this product doesn't exist anymore, continue to the next iteration
continue;
}
$product->set_stock_quantity( $stock_qty );
$product->save();
}This is how it works.
- We hardcoded original stock quantities (we need to reset to) as an array in PHP (however, you can store it as a CSV file if you want).
- We looped through them and updated all quantities for products and variations.
The next question is – how to automatically perform this action daily?
Reset product stock daily using Action Scheduler
It is time to perform an automatic stock reset every certain period of time. In our case, we decided to do it daily, but you can also do it, I don’t know, every week, or even every hour.
To do that, we need to create a scheduled task. In WooCommerce, we usually do that using its Action Scheduler; in WordPress, directly via WP_Cron.
Here is a step by step we need to do to perform this action properly:
- Connect the code above to a custom action hook.
- Use the
as_schedule_recurring_action()to create a scheduled recurring action.
Below is the ready-to-use code:
add_action( 'rudr_reset_stock', function() {
// here we will need to hardcode the original stock quantities
$set_stock_quantities = array(
5 => 10,
15 => 110,
// etc.
);
foreach( $set_stock_quantities as $product_id => $stock_qty ) {
// let's try to get this product (or variation) first
$product = wc_get_product( $product_id );
if( ! $product ) {
// this product doesn't exist anymore, continue to the next iteration
continue;
}
$product->set_stock_quantity( $stock_qty );
$product->save();
}
} );The code above should be placed in your theme’s functions.php file or in a custom plugin. But the code below should run only once.
// when the first time we need to run the reset
$timestamp = strtotime( 'tomorrow 01:00:00', current_time( 'timestamp' ) );
// creating a scheduled recurring task
as_schedule_recurring_action( $timestamp, DAY_IN_SECONDS, 'rudr_reset_stock' );Method 2. Using Two WooCommerce Stores and a Plugin
It is quite a common case among store owners when they have two WooCommerce stores:
- In the first store, they manage the stock directly (sometimes they also do it for orders).
- From time to time, they sync the updated product stock to the second, live store.
This whole scenario can be significantly optimized when the stores are a part of a single WooCommerce multisite network.
In any case, it can be easily achieved with my Inventory Sync for WooCommerce plugin.
There are only two steps we need to do.
Step 1. Connect “Store 2” to “Store 1”
First of all, let’s decide what each store is for.
| Store name | Store description |
|---|---|
| Store 1 | This is the store where we manage everything: the product stock, in our case, or any other product information, e.g. customers and orders. |
| Store 2 | This is the live store, available to our customers. Once we’ve finished managing the product/order data, we push it to our target store, this one. |
So, what we need to do is to connect “Store 2” to “Store 1”. It can be done in the plugin settings, in WooCommerce > Settings > Products > Inventory:

As you can see from the screenshot, depending on whether you’re using WordPress Multisite for your WooCommerce stores or not, the connection happens differently:
- If yes, select the target store from the list.
- If not, you need to connect “Store 2” using the REST API authentication data (the consumer key and consumer secret).
That’s pretty much it with the first step.
Step 2. Run the bulk stock resync manually or automatically
Once the stores are connected, you can easily push the stock quantities of all products from “Store 1” to “Store 2” using a tool:

Of course, you can run this tool manually every day; however, I assume you would prefer to update stock daily automatically. At this moment, my plugin doesn’t have that feature; however, you can easily do it using a simple code snippet:
add_action( 'rudr_start_inventory_sync', function( $store_id_or_url ) {
// you do not need to make any changes in the lines below
$hook_name = is_numeric( $store_id_or_url ) ? 'rudr_isfw_resync_wpmu' : 'rudr_isfw_resync';
if( false === as_next_scheduled_action( $hook_name, array( 'store' => (string) $store_id_or_url ) ) ) {
as_schedule_recurring_action( time(), MINUTE_IN_SECONDS, $hook_name, array( 'store' => $store_id_or_url ) );
}
} );The snippet above, you’d need to add to your functions.php file or to a custom plugin. The snippet below needs to be run only once:
$timestamp = strtotime( 'tomorrow 01:00:00', current_time( 'timestamp' ) );
$store_id_or_url = 'store2.rudrastyh.com'; // provide a store URL
//$store_id_or_url = 5; // if you're using a multisite network, the store ID must be without quotes
$interval_in_seconds = DAY_IN_SECONDS; // we will do it every 24 hours (daily)
as_schedule_recurring_action( $timestamp, $interval_in_seconds, 'rudr_start_inventory_sync', array( 'store' => $store_id_or_url ) );If you want to add this feature to the plugin, please let me know in the comments below.
Misha Rudrastyh
Hey guys and welcome to my website. For more than 15 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me