Multiple Products from the Same Stock

In this tutorial I will show you how you make both Stock Quantity and Stock Status to be automatically shared between different products on your WooCommerce store. But if you come to this tutorial looking for a product inventory synchronisation tool between different WooCommerce stores, then please take a look at my plugin.

Also I am going to show you an example with variations of the same product.

The whole idea is pretty simple and can be breaked down into two steps:

  1. Creating a custom function connected either to the woocommerce_product_set_stock or to the woocommerce_variation_set_stock action hook if we talking about variations.
  2. Obtaining the connected products and to setting the same stock values for them.

Easy peasy.

add_action( 'woocommerce_product_set_stock', 'rudr_update_shared_stock' );

function rudr_update_shared_stock( $product ) {

	$new_manage_stock = $product->get_manage_stock();
	$new_stock_status = $product->get_stock_status();
	$new_stock_qty = $product->get_stock_quantity();

	// we have to get the connected products
	$products = array( ... ); // let's say we have product IDs here

	if( ! $products ) {
		return;
	}

	remove_action( 'woocommerce_product_set_stock', __FUNCTION__ );
	foreach( $products as $product_id ) {

		$product = wc_get_product( $product_id );
		if( ! $product ) {
			continue;
		}

		$product->set_manage_stock( $new_manage_stock );
		$product->set_stock_status( $new_stock_status );
		$product->set_stock_quantity( $new_stock_qty );
		$product->save();

	}
	add_action( 'woocommerce_product_set_stock', __FUNCTION__ );

}

Some moments to consider:

Set all Variations Out of Stock if One Variation is Out of Stock

The process with variations is going to be more automated because we don’t have to get the connected products stored in options or somewhere else, we just need to get other variations of the same product.

add_action( 'woocommerce_variation_set_stock', 'rudr_variation_shared_stock' );

function rudr_variation_shared_stock( $updated_variation ) {

	$new_stock_status = $updated_variation->get_stock_status();
	$new_stock_qty = $updated_variation->get_stock_quantity();
	
	// let's get variation product, so we can get other variations
	$product = wc_get_product( $updated_variation->get_parent_id() );
	// getting variations, you can also use get_children() method
	$variations = $product->get_available_variations();
	$variation_ids = wp_list_pluck( $variations, 'variation_id' );

	remove_action( 'woocommerce_variation_set_stock', __FUNCTION__ );

	foreach( $variation_ids as $variation_id ) {

		if( $variations_id === $updated_variation->get_id() ) {
			continue;
		}
		
		$variation = wc_get_product_object( 'variation', $variation_id );
		$variation->set_stock_status( $new_stock_status );
		$variation->set_stock_quantity( $new_stock_qty );
		$variation->save();

	}
	
	add_action( 'woocommerce_variation_set_stock', __FUNCTION__ );

}

Lines 11 and 12 can be replaced with just one line:

$variation_ids = $product->get_children();

More examples of using shared inventory for product variations you can find here.

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