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 this 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:
- 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.
- Obtaining the connected products and to setting the same stock values for them.
Easy peasy.
add_action( 'woocommerce_product_set_stock', 'wpdev_update_shared_stock' );
function wpdev_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:
- It is amazing that we have WC_Product object available as the $product argument of the function!
- On line 10 you can see kind of the missing code, it is because you can choose your own way how to connect products with each other in order for them to have a shared stock. Most likely you can use WooCommerce settings pages for that.
- Lines 16 and 30 are also crucial, especially the first one of them – it is because when we are about to run set_stock_status() and set_stock_quantity(), our function is about to run a one more unnecessary time! So we are disconnecting it before using those hooks.
- I hope you know where to insert the code as well.
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', 'wpdev_variation_shared_stock' );
function wpdev_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();