Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

WooCommerce conditional inventory change

$
0
0

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 updatedadd_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.


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>