Shantox1407
Shantox1407

Reputation: 57

Changing WooCommerce processing orders status based on product stock quantity

I'm currently working on an automated status changes for WooCommerce orders: When a product stock reaches 50%, I am trying to change all related "processing" orders to 'kundenupdate' order status.

I am using the function retrieve_orders_ids_from_a_product_id() from this answer thread, querying orders with "processing" status, form a product ID in the code below:

function retrieve_orders_ids_from_a_product_id( $product_id ) {
    global $wpdb;

    // Define HERE the orders status for queried orders
    $orders_statuses = "'processing'";

    # Requesting All defined statuses Orders IDs for a defined product ID
    return $wpdb->get_col( "
        SELECT DISTINCT woi.order_id
        FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim, 
             {$wpdb->prefix}woocommerce_order_items as woi, 
             {$wpdb->prefix}posts as p
        WHERE  woi.order_item_id = woim.order_item_id
        AND woi.order_id = p.ID
        AND p.post_status IN ( $orders_statuses )
        AND woim.meta_key IN ( '_product_id', '_variation_id' )
        AND woim.meta_value LIKE '$product_id'
        ORDER BY woi.order_item_id DESC"
    );
}

/**
 * Automated order status changes
 */
add_action('woocommerce_order_status_changed', 'woocommerce_auto_processing_orders');
function woocommerce_auto_processing_orders( $order_id ) {
    if ( ! $order_id )
        return;

    $order = wc_get_order( $order_id );

    $items = $order->get_items();

    foreach ( $items as $item ) {
        //product_id gets tracked automatically
        $product = wc_get_product( $item['product_id'] );
        $product_id = $item->get_product_id();

        $orders_ids_array = retrieve_orders_ids_from_a_product_id( $product_id );

        $stock = $product->get_stock_quantity();

        //If order is "processing".
        foreach ( $orders_ids_array as $order_id_array ) { 
            if( $order_id_array->has_status( 'processing' ) ) {
                if( $stock == 350 ) {
                    $order_id_array->update_status( 'kundenupdate' );
                } elseif( $stock == 140 ) {
                    $order_id_array->update_status('on-hold');
                }
            }
        }
    }
}

If I change the code for only one order, it works. But if I try to get all orders for a specific product_id, which all are in the state 'processing', it doesn't work.

I also don't know, if the function retrieve_orders_ids_from_a_product_id() is working correctly, since I don't know, how to debug it in php.

How do I get a correct array of all orders for a specific product_id, which all are in the state 'processing'?

Upvotes: 2

Views: 922

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253814

There are a some mistakes and errors in your code:

  • The function retrieve_orders_ids_from_a_product_id() works fine and gives an array of order IDs, but NOT an array of WC_Order Objects: so for example, in your code, the method has_status() can't be used on a non WC_Order Object.
  • The current WC_Order object is already included with woocommerce_order_status_changed hook, so the argument is missing from your function.
  • The hook woocommerce_order_status_changed is included in WC_Order status_transition() method source code, that is used by update_status(), so you can't use it with this hook, as it makes a kind of infinite loop.
  • And some other things…

WooCommerce orders and reducing product stock quantity process explained:

The product stock status is reduced using wc_maybe_reduce_stock_levels() function which is hooked in those 4 hooks (see it on the function source code):
- woocommerce_payment_complete - has $order_id as argument,
- woocommerce_order_status_on-hold - has $order_id and $order as arguments,
- woocommerce_order_status_processing - has $order_id and $order as arguments,
- woocommerce_order_status_completed - has $order_id and $order as arguments.

When the related products stock quantities are reduced by an order, a specific meta data is set to this order, which meta key is _order_stock_reduced and meta value is 1, avoiding to reduce it multiple times.

In the code below, we will use those 4 hooks.

To avoid problems using WC_Order update_status() method with those hooks (as explained before), we will use instead wp_update_post() WordPress function.

You can try the following:

function get_processing_orders_from_a_product_id( $product_id ) {
    global $wpdb;

    # Requesting processing Orders IDs for a defined product ID
    return $wpdb->get_col( "
        SELECT DISTINCT woi.order_id
        FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim,
             {$wpdb->prefix}woocommerce_order_items as woi,
             {$wpdb->prefix}posts as p
        WHERE  woi.order_item_id = woim.order_item_id
        AND woi.order_id = p.ID
        AND p.post_status = 'wc-processing'
        AND woim.meta_key IN ( '_product_id', '_variation_id' )
        AND woim.meta_value LIKE '$product_id'
        ORDER BY woi.order_item_id DESC"
    );
}


add_action( 'woocommerce_payment_complete', 'orders_status_change_based_on_product_stock', 10, 2 );
add_action( 'woocommerce_order_status_on-hold', 'orders_status_change_based_on_product_stock', 10, 2 );
add_action( 'woocommerce_order_status_processing', 'orders_status_change_based_on_product_stock', 10, 2 );
add_action( 'woocommerce_order_status_completed', 'orders_status_change_based_on_product_stock', 10, 2 );
function orders_status_change_based_on_product_stock( $order_id, $order = '' ) {
    if( ! $order || ! is_a( $order, 'WC_Order') ) {
        $order = wc_get_order( $order_id ); // Get the WC_Order object if it's empty
    }

    // Loop Through order items
    foreach ( $order->get_items() as $item ) {
        $product   = $item->get_product(); // Get the WC_Product object
        $stock_gty = (int) $product->get_stock_quantity(); // the product stock quantity

        // Getting the new order status from stock quantity thresholds
        if ( $stock_gty === 140 ) {
            $new_status = 'on-hold';
        }
        elseif ( $stock_gty === 350 ) {
            $new_status = 'kundenupdate';
        }

        // Updating related processing orders status if any stock quantity threshold is reached
        if( isset($new_status) && $processing_orders = get_processing_orders_from_a_product_id( $item->get_product_id() ) ) {
            // Loop through all related processing orders (IDs)
            foreach ( $processing_orders as $processing_order_id ) {
                // We don't use update_status() WC_Order method to avoid an problems using those hooks
                wp_update_post( array( 'ID' => $processing_order_id, 'post_status' => $new_status ) );
            }
        }
    }
}

Code goes in functions.php file of your active child theme (or active theme). It should better work.

This might be a problem: What about if the stock of the product is reduced by 3 for example (order item quantity) and jumps over the 350 (or 140) stock quantity threshold? You might be obliged to think it different.

Upvotes: 1

Related Questions