Ecommerce Shopify WordPress Discussion

WooCommerce conditional inventory change

I am trying to get this bit of PHP to work on a build. The goal is to have Item Y update its stock status to "out of stock" if Item X becomes out of stock. Here is what I have: /* Conditional inventory tracking. If Item X is out of stock, make Item Y out of stock also */ function update_related_product_stock( $product_id ) { // Check if the product is Item X or Item Y if ( $product_id == 123 || $product_id == 456 ) { // Replace 123 and 456 with the actual product IDs of Item X and Item Y $item_1_stock = get_post_meta( 123, '_stock', true ); // Replace 123 with the actual product ID of Item X // Update stock of Item Y based on stock of Item X if ( $item_1_stock <= 0 ) { update_post_meta( 456, '_stock_status', 'outofstock' ); // Replace 456 with the actual product ID of Item Y } else { update_post_meta( 456, '_stock_status', 'instock' ); // Replace 456 with the actual product ID of Item Y } } } // Hook the function to run when stock levels are updated add_action( 'woocommerce_product_set_stock', 'update_related_product_stock' ); Can anyone steer me in the right direction with this? Thanks in advance! Item X runs out of stock, Item Y should automatically set to "Out of Stock". Replaced the above with WooCommerce Product IDs on a pair of test items. I set Item X out of stock, but then Item Y remained in stock. I tried both with and without stock quantity management active for Item Y, and saw the same result both with and without it active.
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.