Bulk Update Product Stock Quantities
In the previous tutorial we already discussed updating WooCommerce product stock quantities programmatically when we covered how to do it with REST API specifically.
The whole idea can be deconstructed into two simple things:
- Connect to
woocommerce_product_set_stock
(orwoocommerce_variation_set_stock
action hooks for product variations). - Send a REST API request to
/wc/v3/products/{$product_id}
(or/wc/v3/products/{$product_id}/variations/{$variation_id}
for product variations).
That’s pretty much it. But there is a “but”. If a specific WooCommerce order in your store can contains a lot of products, then you have a problem. For example, there are 50 products in a single order, then guess what? 50 REST API requests are going to be sent in order to sync all the products stock quantities.
So there is no other way than to update the stock quantities in bulk. But how to do it?
If you check an official WooCommerce docs, then you will see that there is a batch endpoint available, which is /wc/v3/products/batch
for products or /wc/v3/products/{$product_id}/variations/batch
for product variations.
So in order to update product stock quantities for a lot of products (up to 100) within a single REST API request, we need to do something like this:
wp_remote_post(
'https://YOUR STORE/wp-json/wc/v3/products/batch',
array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( "$login:$pwd" )
),
'body' => array(
'update' => array(
// first product
array(
'id' => 5, // product ID on Site 2
'stock_quantity' => 2, // product stock quantity to set
),
// second product
array(
'id' => 8,
'stock_quantity' => 120,
),
...
),
)
)
);
Let’s change this code to a more usable format:
$product_ids = array( 1, 8, 25 ... ); // up to 100 products
$products_to_update = array();
foreach( $product_ids as $product_id ) {
if( ! $product = wc_get_product( $product_id ) ) {
continue;
}
if( ! $product->get_manage_stock() ) {
continue;
}
$products_to_update[] = array(
'id' => $product->get_id(),
'stock_quantity' => $product->get_stock_quantity(),
);
}
wp_remote_post(
'https://YOUR STORE/wp-json/wc/v3/products/batch',
array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( "$login:$pwd" )
),
'body' => $products_to_update
)
);
And now a little explanation:
- If you don’t know where to get
$login
and$pwd
variables then I recommend you to read about application passwords. - I am also using
if( ! $product->get_manage_stock() ) {
condition because we don’t want products without any stock quantities to be added to the sync request. - Of course, we can not use
woocommerce_product_set_stock
action hook anymore, becase then our batch request is going ot be sent 50 times anyway, so we just need to switch towoocommerce_reduce_order_stock
instead.
And as always, if you don’t want to deal with the code, I recommend you to take a look at my inventory sync plugin.

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 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