4 Ways to Make Variation Shared Inventory on your Store

Recently I’ve been developing a new plugin Duplicate SKU Stock Sync and I found out that a lot of different solutions and approached are out there on how to make a shared stock for WooCommerce product variations.

Let me describe every one of them in this tutorial.

The possible ways to implement a variation shared stock which are commonly used are:

  • Using the shared stock for all variations of a specific product.
  • Combining specific variations into a group and if any variation from the group has been purchased, reduce the stock levels of the whole group.
  • Assigning a stock quantity to a specific attribute and then managing the inventory on the attribute level.
  • Assigning the same SKU for different variations and reducing the stock for all the products with the same SKU, that’s what my plugin does by the way.

And now let’s dive deep into every of the ways mentioned here and try to decide which one is the best.

Shared Inventory for All Variations of a Product

This is the easiest way of them all and it can be accomplished without any plugins or custom coding – just with standard WooCommerce settings.

I am mentioning it first, because maybe that’s what you need and it is not necessary for you to dive deep into more complicated solutions.

So all you need to do is to go to the edit product page and then to the “Inventory” tab:

Shared inventory for all variations of a WooCommerce product
If you are using settings like this, then if any variation of this specific product is purchased, all the product variations will become out of stock (because Quantity is 1).

But one more thing, if you go to any specific variation of this product and check “Manage Stock?” checkbox and set stock levels individually for this variation, then the shared stock quantity is not going to be applied to this variation:

Individual stock levels of a specific variation of a WooCommerce product

As you can see, these standard WooCommerce settings are pretty neat, so probably you don’t even need to look into the second solution described in this tutorial, because basically in order to make a shared stock for a variation group you just need to do two things:

  1. Allow stock management on a product level (first screenshot),
  2. Exclude specific variations by managing the stock individually for them (second screenshot).

As simple as that.

Shared Inventory for a Group of Variations

First of all – what is a group of variations? Well, it could be a bunch of any variations for which you decided that their stock quantity should be the same.

For example in the code below I decided that my group will consist of three variations and I hardcoded them this way array( 5, 15, 254 ). Of course when you’re creating a WordPress plugin or a client project, nothing should be hardcoded and it is up to you to decide how you want to manage variation groups on your WooCommerce store.

add_action( 'woocommerce_variation_set_stock', 'rudr_grouped_variation_stock' );

function rudr_grouped_variation_stock( $variation ) {

	// $variation is a WC_Product_Variation object

	// get updated inventory information
	$stock_status = $variation->get_stock_status();
	$stock_qty = $variation->get_stock_quantity();

	// let's get a group of this variation
	// you can store it the way you like, I am just proviging IDs statically
	$group = array( 5, 15, 254 );

	// let's get a parent product, so we can get other variations
	$product = wc_get_product( $variation->get_parent_id() );

	// get all variations (both enabled and not enabled)
	$variation_ids = $product->get_children();

	remove_action( 'woocommerce_variation_set_stock', __FUNCTION__ );

	foreach( $variation_ids as $variation_id ) {

		if( ! in_array( $variation_id, $group ) ) {
			continue;
		}

		$variation = wc_get_product_object( 'variation', $variation_id );
		$variation->set_stock_status( $stock_status );
		$variation->set_stock_quantity( $stock_qty );
		$variation->save();

	}

	add_action( 'woocommerce_variation_set_stock', __FUNCTION__ );

}

What I actually want to highlight in this code example:

  • As you can see I used get_children() method to get all product variations, this method allows you to get both enabled and not enabled ones, but if you only want to get the variations which are available for purchase, then use get_available_variations() instead (and actually we’re going to use it in the next example).
  • We also need to use remove_action() for the hook woocommerce_variation_set_stock because it will be fired every time when we change a variation stock quantity programmatically. So the whole thing may end up in an infinite loop.
  • In order to get a variation object you can use the wc_get_product() function, the wc_get_product_object() function (we used it) or new WC_Product_Variation().

I think we’re done here, now let’s configure the variation shared inventory another way – using attribute stock levels.

Attribute Stock Management

Let’s assume that you have a bunch of variation like on a screenshot below and you would like variations with the same color attribute to have a shared stock, in other words we need to manage stock on the attribute level in this case.

Configure variation shared inventory based on an attribute
If a customer purchases either a variation #39 or #34, the stock will be changed for them both.
add_action( 'woocommerce_variation_set_stock', 'rudr_attribute_stock' );

function rudr_attribute_stock( $variation ) {

	// get updated inventory information
	$stock_status = $variation->get_stock_status();
	$stock_qty = $variation->get_stock_quantity();

	// let's get a parent product, so we can get other variations
	$product = wc_get_product( $variation->get_parent_id() );

	// get all variations (enabled)
	$variations = $product->get_available_variations();

	remove_action( 'woocommerce_variation_set_stock', __FUNCTION__ );

	foreach( $variations as $variation_data ) {

		if( 
			! isset( $variation_data[ 'attributes' ][ 'attribute_pa_color' ] ) 
			|| 'blue' !== $variation_data[ 'attributes' ][ 'attribute_pa_color' ] 
		) {
			continue;
		}

		$variation = new WC_Product_Variation( $variation_data[ 'variation_id' ] ));
		$variation->set_stock_status( $stock_status );
		$variation->set_stock_quantity( $stock_qty );
		$variation->save();

	}

	add_action( 'woocommerce_variation_set_stock', __FUNCTION__ );

}

Here I’ve also hardcoded a condition when we check the value of the attribute_pa_color attribute (which is “Color”) and we’re making sure that its value is blue. Once again, nothing should be hardcoded, so please consider adding some settings for it for example.

Using Duplicate SKUs for Specific Variations

The last but not the least, I’m going to show you one more option of configuring the variation shared inventory for WooCommerce stores.

If you think that combining variations into groups and managing stock on the attribute level kind of complicated for you, you can just provide the same SKU for variations you would like to have a shared inventory (and these variations not necessary should relate to a single product).

Seems interesting? Then check my plugin out – Duplicate SKU Stock Sync.

Misha Rudrastyh

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

Follow me on X