Automatically set product to draft if sold out

https://app.asana.com/0/1200637280721383/1206700572996964/f
/*
* Unpublish products after order
*/
function tzn_custom_unpublish_products($order_id) {
    $order = new WC_Order( $order_id );
    $all_products = $order->get_items();
    foreach ($all_products as $product){
        $product_object = wc_get_product($product['product_id']);
        if( ! $product_object->get_stock_quantity() ) {
            wp_update_post(array(
                'ID' => $product['product_id'],
                'post_status' => 'draft'
            ));
        }
    }
}
add_action( 'woocommerce_thankyou', 'tzn_custom_unpublish_products', 10, 1 );

Leave a Reply

Your email address will not be published. Required fields are marked *