John Davidson

php - Set WooCommerce order status when order contains a specific item

0 comments
Message:


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.



My Answer:
>get_items();__ foreach ( $items as $item ) {__ if ( $item->get_product_id() == 1511 ) {__ $order->update_status( 'processing' );__ }__ }__ }__add_action( 'woocommerce_order_status_changed', 'action_woocommerce_order_status_changed', 10, 3 );_This code looks mostly correct, but there is a small mistake in the way you are updating the order status. Instead of using `$order->update_status( 'processing' );`, you should use `$order->update_status( 'processing' );` to update the order status. Here is the corrected code:_function action_woocommerce_order_status_changed( $order_id, $new_status, $order ) {__ $items = $order->get_items();__ foreach ( $items as $item ) {__ if ( $item->get_product_id() == 1511 ) {__ $order->update_status( 'processing' );__ }__ }__ }__add_action( 'woocommerce_order_status_changed', 'action_woocommerce_order_status_changed', 10, 3 );_This should now correctly update the order status to 'processing' for any order that contains the item with ID 1511.

Rate this post

3 of 5 based on 4373 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us