Set WooCommerce order status when order is created from processing to pending

青春壹個敷衍的年華 提交于 2019-12-18 06:51:40

问题


When a woocommerce order is created the status of the order is "processing". I need to change the default order-status to "pending".

How can I achieve this?


回答1:


The default order status is set by the payment method or the payment gateway.

You could try to use this custom hooked function, but it will not work (as this hook is fired before payment methods and payment gateways):

add_action( 'woocommerce_checkout_order_processed', 'changing_order_status_before_payment', 10, 3 );
function changing_order_status_before_payment( $order_id, $posted_data, $order ){
    $order->update_status( 'pending' );
}

Apparently each payment method (and payment gateways) are setting the order status (depending on the transaction response for payment gateways)…

For Cash on delivery payment method, this can be tweaked using a dedicated filter hook, see:
Change Cash on delivery default order status to "On Hold" instead of "Processing" in Woocommerce

Now instead you can update the order status using woocommerce_thankyou hook:

add_action( 'woocommerce_thankyou', 'woocommerce_thankyou_change_order_status', 10, 1 );
function woocommerce_thankyou_change_order_status( $order_id ){
    if( ! $order_id ) return;

    $order = wc_get_order( $order_id );

    if( $order->get_status() == 'processing' )
        $order->update_status( 'pending' );
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works

Note: The hook woocommerce_thankyou is fired each time the order received page is loaded and need to be used with care for that reason...
Now the function above will update the order status only the first time. If customer reload the page, the condition in the IF statement will not match anymore and nothing else will happen.


Related thread: WooCommerce: Auto complete paid Orders (depending on Payment methods)




回答2:


// Rename order status 'Processing' to 'Order Completed' in admin main view - different hook, different value than the other places
add_filter( 'wc_order_statuses', 'wc_renaming_order_status' );
function wc_renaming_order_status( $order_statuses ) {
    foreach ( $order_statuses as $key => $status ) {
        if ( 'wc-processing' === $key ) 
            $order_statuses['wc-processing'] = _x( 'Order Completed', 'Order status', 'woocommerce' );
    }
    return $order_statuses;
}


来源:https://stackoverflow.com/questions/45985488/set-woocommerce-order-status-when-order-is-created-from-processing-to-pending

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!