Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 551
public9nf Avatar asked Aug 31 '17 15:08

public9nf


People also ask

How do I automatically change the order status in WooCommerce?

To setup Order Status Control, go to WooCommerce > Settings > General and update the Orders to Auto-Complete setting to determine which paid orders should skip the Processing status and go directly to Completed status: None: No orders will be automatically completed.

How do I change my order status in WooCommerce API?

$client->orders->update_status($order_id_to_update, $status); it creates a new order with the exact same information and updates status for both the original and the newly created order.

What does order processing mean WooCommerce?

Processing: This means the customer's payment has been received. The order is now awaiting fulfilment, i.e. for you to process it. If you're using WooCommerce to track your orders, you can click into the order and change the order status to “Completed” once you've dispatched the product(s).

How does WooCommerce order status work?

Completed — Order fulfilled and complete – requires no further action. On hold — Awaiting payment – stock is reduced, but you need to confirm payment. Canceled — Canceled by an admin or the customer – stock is increased, no further action required. Refunded — Refunded by an admin – no further action required.


2 Answers

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)

like image 184
LoicTheAztec Avatar answered Oct 19 '22 18:10

LoicTheAztec


Nowadays, if the payment gateway that you use properly sets the order status using WC_Order->payment_complete(), you can use the woocommerce_payment_complete_order_status filter.

This is better than using woocommerce_thankyou hook since we are setting the order status immediately, rather than applying it after it has been already set.

function h9dx3_override_order_status($status, $order_id, $order) {
  if ($status === 'processing') {
    $status = 'pending';
  }

  return $status;
}
add_filter('woocommerce_payment_complete_order_status', 'h9dx3_override_order_status', 10, 3);

Again, this will only work if the payment gateway uses the proper payment_complete wrapper method rather than setting status directly with set_status. You can just search the gateway code for 'payment_complete(' and 'set_status(' to see what it does.

If you develop a plugin for everyone, you will be better off with using woocommerce_thankyou, or you could use a combined approach and use woocommerce_thankyou as the fallback if the order status was not updated.

like image 31
Maciej Krawczyk Avatar answered Oct 19 '22 19:10

Maciej Krawczyk