I would like to change the status of every new WooCommerce order depending on a custom field (shopart) with PHP.
I already tried to write a function in the functions.php file but I failed.
// set Custom Order Status at WooCommerce Checkout Process add_action( 'woocommerce_thankyou', 'uebes_thankyou_change_order_status' ); function uebes_thankyou_change_order_status( $order_id ){ if( ! $order_id ) return;{ $order = wc_get_order( $order_id ); // update order status dependimg to custom field shopart if(get_post_meta($order->id,'shopart',true) == 'Shop Vorbestellungen'){ $order->update_status( 'vorbestellung' ); }else{ $order->update_status( 'bestellung-neu' ); } } }
Advertisement
Answer
// set Custom Order Status at WooCommerce Checkout Process add_action('woocommerce_thankyou', 'uebes_thankyou_change_order_status', 10, 1); function uebes_thankyou_change_order_status($order_id) { if (!$order_id) return; //create an order instance $order = wc_get_order($order_id); // update order status dependimg to custom field shopart if (get_post_meta($order_id, 'shopart', true) == 'Shop Vorbestellungen') { $order->update_status('vorbestellung'); } else { $order->update_status('bestellung-neu'); } }