Set WooCommerce order status when order contains a specific item

Am trying to set any order that has the item ( item id 1511 ) to be set as 'processing'

I tried to play around with this code in this answer, but without the desired result. I'm not sure what am doing wrong?

I tried going like this:

function action_woocommerce_order_status_changed( $order_id, $new_status, $order ) {

        $items = $order->get_items();
        if ( $items as $item ) {
            // Get product id
            $product_id = $item->get_product_id();

            if ($product_id == 1511 ) {
                $order->update_status( 'processing' );
                break;
    }
}
add_action( 'woocommerce_order_status_changed', 'action_woocommerce_order_status_changed', 10, 4 );

I appreciate any advice.

Solutions

woocommerce_order_status_changed action hook is triggered each time when order status change.

So for your question use the woocommerce_checkout_order_created hook (that is executed, before the e-mail notifications are sent) or the woocommerce_thankyou hook depending on your wishes.

So you get:

function action_woocommerce_checkout_order_created( $order ) {
    // Already contains the correct status
    if ( $order->get_status() == 'processing' ) return;
    
    // Set variable
    $found = false;
    
    // Get order items = each product in the order
    $items = $order->get_items();

    foreach ( $items as $item ) {
        // Get product id
        $product_id = $item->get_product_id();

        if ( $product_id == 1511 ) {
            $found = true;
            // true, break loop
            break;
        }
    }

    // True
    if ( $found ) {
        $order->update_status( 'processing' );
    }
}
add_action( 'woocommerce_checkout_order_created', 'action_woocommerce_checkout_order_created', 10, 1 );

OR

function action_woocommerce_thankyou( $order_id ) {
    // Get order
    $order = wc_get_order( $order_id );

    // Already contains the correct status
    if ( $order->get_status() == 'processing' ) return;
    
    // Set variable
    $found = false;

    // Get order items = each product in the order
    $items = $order->get_items();

    foreach ( $items as $item ) {
        // Get product id
        $product_id = $item->get_product_id();

        if ( $product_id == 1511 ) {
            $found = true;
            // true, break loop
            break;
        }
    }

    // True
    if ( $found ) {
        $order->update_status( 'processing' );
    }
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );

Similar questions

Change admin payment status back to unpaid for pending order status in Woocommerce
When Order status in Woocommerce is changed to Processing the payment status is set to paid: But order was accidentally set to processing and shouldn't have gotten the status paid. Now when we set status to pending again it doesn't remove the text: Order #1234 details Payment via Purchase Order. Paid on September 17, 2018 @ 9:18 am Any idea how to ...
Get last old order status before updated status in Woocommerce
I'm currently looking for a way to get the order status before an updated order status. So for example my order has the status in-progress and I change the order status programmatically with this function here to wc-completed: When the order status is wc-completed I've a trigger which sends an email. But I need to check now if the status before the...
Change Woocommerce order status based on payment gateway and transition status
So I know how to change status based on payment gateway. But is there a way to also take the previous order status into account? My issue is as following: we start processing our orders after the customer sign an e-contract. They are then free to pay later via a couple gateways (Stripe, COD, bank transfer or invoice - we send payment links). This m...
Hide order status in the status dropdown on WooCommerce admin orders page
I need to hide the status wc-completed within the dropdown on the WooCommerce admin orders page. Using CSS is not a good option here but I've found no other way. The only thing would be to use the wc_order_statuses to unset this status, but in this case, refunds can't work anymore because wc-completed is the default status for refunds. So maybe the...
Need Bulk Status Change function with a Custom Woocommerce Order Status
I've used the below php to add an 'imported' custom order status in woocommerce and it works fine - However when I try to bulk update the order status - the custom order status does not appear, how can I add the custom status to the bulk actions in Woocommerce Orders. Furthermore, i want the imported status to be considered as having payment captur...
Add a custom meta field value to WooCommerce order if status changes to custom status
I have searched all over the internet. What I'm looking for is to create a custom woocommerce order field which will be automatically added to the order when order status changes to wc-kurzuhradena which the custom order status, with the value of current month and year. Example value: May 2021 So far I have this code which adds a custom field but I...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.