简体   繁体   中英

setting custom order status in woocommerce

I have followed this tutorial and to add a custom order status "Awaiting Shipment":

My problem is I'm trying to update the status via a php function, but it stays sets on pending payment! So it is executing and changing the correct order but not with this new status.

My code:

$order = new WC_Order($order_id);
$order->update_status('Awaiting shipment', 'order_note');

I can set 'Awaiting Shipment' in the WordPress dashboard ok...

What am I doing wrong?

You need to set it using the slug awaiting-shipment instead, so your code will be:

$order = new WC_Order( $order_id );
$order->update_status('awaiting-shipment', 'order_note');

This time it will work…

Also 'order_note' is optional and should be replaced with a real explicit text as an order note should be.

To finish you also can use $order = wc_get_order( $order_id );

Reference: WC_Order update_status() method

Related thread: WooCommerce: Auto complete paid orders

Try this below

add_action( 'woocommerce_thankyou', 'my_custom_status_update' );

function my_custom_status_update( $order_id ) {

    $order = new WC_Order( $order_id );
    $order->update_status( 'awaiting-shipment' );

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM