Central Stock for WooCommerce

In this tutorial, I will show you how to configure central stock for WooCommerce using a plugin, but we will also take a look at the programmatic approach.

What Does It Mean – Having a Central Stock for WooCommerce Stores?

Ok, but what do I mean by having a central stock in WooCommerce?

It is when you have multiple WooCommerce stores, but you can manage the product stock for those stores in a single place. It can be another store or just an external CSV file.

For example, let’s say we have “Store 1”, “Store 2”, “Store 3”, “Store 4”, and “Store 5”, and one of these stores we use as a central stock, “Store 1”, or any other one. Then the following scenarios will take place:

TriggerResult
The stock is changed in a sub-store, “Store 2” or “Store 3”, for example.The product stock will be automatically synced to the main store, which, in our example, is “Store 1”.
The stock has changed in the main store.The product stock will be automatically synced to all the sub-stores: “Store 2”, “Store 3”, “Store 4”, and “Store 5”.

That’s mine (and some of my clients’) understanding of what central stock is. If you have different ideas about it, please let me know in the comments section below.

Another question is – “How will the same products be connected across stores?” I guess you already know the answer – the best way to connect them is by an SKU. Connecting products by their slugs or the “GTIN, UPC, EAN, or ISBN” field value is also possible, but it is not used that often.

Configuring Central Stock Using a Plugin for WooCommerce

In this part, I will show you how to use Inventory Sync for WooCommerce to configure central stock for your WooCommerce stores.

By the way, below are screenshots for a WordPress multisite installation, but it will work for standalone stores just as well. The only thing is that when connecting standalone stores, you would need to provide their REST API authentication data.

WooCommerce (WordPress) Multisite network – is a special type of WordPress configuration, where you use a single site database and a single WordPress installation, but you can create as many sites as you want in it. It is not just when you have multiple stores.

All right, let’s do it step by step.

Step 1. Connect all sub-stores in the main store’s settings

First of all, on the main store, in our case, it is “Store 1”, visit the plugin settings page in WooCommerce > Settings > Products > Inventory and add every sub-store there:

Connect WooCommerce sub-stores to the central stock

As I mentioned before, here, in the screenshots, all we need to do is select a store from the dropdown list and click the “Add new store” button. It is that simple because, in my case, the stores are a part of a multisite network. If your stores are standalone ones, of course, our central stock store will not know about their existence, and we need to provide a store URL, as well as authentication data, which is the consumer key and secret.

Step 2. Connect the main store in each store’s settings

Then, connect the store with the central stock in every store’s settings:

Central stock for WooCommerce

That’s pretty much it, our WooCommerce central stock is set up and running at this point.

Configuring Central Stock Programmatically

In case you were looking for a programmatic approach to creating a central stock for your WooCommerce stores, then below you will find a step-by-step.

Choosing the correct WooCommerce hooks

This part can be quite tricky, and let me explain to you why. The trickiness begins when customers can create huge orders with a lot of products and variations (100+).

Because, for simple stores with small orders, the following two hooks are more than enough:

  • woocommerce_product_set_stock – triggered when the stock of a specific product has been changed.
  • woocommerce_variation_set_stock – the same for product variations.

For example:

add_action( 'woocommerce_product_set_stock', function( $product ) {
	
	// the product stock is changed

} );

add_action( 'woocommerce_variation_set_stock', function( $variation ) {
	
	$product = wc_get_product( $variation->get_parent_id() );

	// the product variation stock is changed

} );

But if your stores are supposed to have orders with a lot of products, then it is better to use a single hook for stock synchronisation. But even merging 100+ hooks into one could not be enough, so you’d probably need to unload some tasks on Action Scheduler or something.

Let’s assume that our example stores don’t get orders with a lot of products, or you can still change your mind and use my Inventory Sync for WooCommerce plugin – it is optimized for the best performance.

Finding out the product ID by the SKU

But, before we dive into the actual central stock synchronisation process, I would also want to highlight a function that we will need to use to get a product with the same SKU either in the central stock store or in the sub-store.

The solution will depend on whether you’re using a WordPress Multisite network or not.

Is it a WordPress Multisite?How to get a product ID by SKU
YesWith the wc_get_product_id_by_sku() and switch_to_blog() functions, read more here.
No (standalone stores)With a REST API request.

Syncing the stock

Finally, it is time to talk about the product inventory syncing between the central stock store and its sub-stores.

Let’s say that we decided to use the woocommerce_product_set_stock hook, then, if we want to sync the product inventory information to the central stock, the code structure may look something like this:

add_action( 'woocommerce_product_set_stock', 'rudr_sync_central_stock' );
			  
function rudr_sync_central_stock( $product ) {
	
	// get the updated product inventory data
	$stock_status = $product->get_stock_status();
	$stock_qty = $product->get_stock_quantity();
	$sku = $product->get_sku();
	
	// stores
	$stores = array(
		2, // I am just using sub-store IDs within a Multisite Network here
		3, 
		4, 
		5,
	);
	
	remove_action( 'woocommerce_product_set_stock', __FUNCTION__ );

	foreach( $stores as $store_id ) {
		switch_to_blog( $store_id );
		
		$product2_id = wc_get_product_id_by_sku( $sku );
		$product2 = wc_get_product( $product2_id );
		if( $product2 ) {
			$product2->set_stock_status( $stock_status );
			$product2->set_stock_quantity( $stock_qty );
			$product2->save();
		}

		restore_current_blog();
	}

	add_action( 'woocommerce_product_set_stock', __FUNCTION__ );

}

There is a chance that you have a lot of questions about this code, so let me explain everything to you:

  • You could probably guess that the code above is for WordPress Multisite network installations. If you’re using standalone stores, instead of wc_get_product_id_by_sku() and switch_to_blog() functions, you would need to send two REST API requests to a target store.
  • The same code must be used on every sub-store as well, but in that case, it will be enough to specify just a single central stock store in the $stores array. And yes, using an array becomes not really that necessary.
  • Using both remove_action() and add_action() is crucial, because if you skip this step, your code will be looped at the moment you run $product2->save().

That’s pretty much it. Have any questions? Feel free to ask in the comments section below.

Misha Rudrastyh

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

Follow me on X