Categories
WooCommerce Development

WooCommerce – Automatically set order status after payment is received

This post has an updated version on WooCommerce and WooCommerce Subscriptions – Automatically set order status to completed

Using Filter: woocommerce_payment_complete_order_status

Sometimes when you’re developing an e-commerce website you’ll need to automatically check the payment status and mark an order as completed.

This usually implies that you’re selling a “virtual good” but not always, I’ve been developing a store that sells “virtual goods” that aren’t marked as such in WooCommerce due to some internal functionalities.

WooCommerce does this behavior automatically on virtual goods but not in general, also I’ve other payment methods that have a callback and I need this behavior in those as well.

To achieve this, I’ve used the function:

add_filter( 'woocommerce_payment_complete_order_status', 'rfvc_update_order_status', 10, 2 );

function rfvc_update_order_status( $order_status, $order_id ) {

 $order = new WC_Order( $order_id );

 if ( 'processing' == $order_status && ( 'on-hold' == $order->status || 'pending' == $order->status || 'failed' == $order->status ) ) {

 return 'completed';

 }

 return $order_status;
}

This function does 2 simple things, first using the filter it’s hooked to the payment status change to complete, when it happens, it checks the order status, and if it has changed to “processing” and has previously set as “on-hold”, “pending” or “failed” the new status will be “completed”, allowing all the actions set to this status to occur automatically.

Using this hook will allow you to set this behavior to all payment methods at once, but if you wish you could also check the payment method from the order, and filter the change accordingly.

26 replies on “WooCommerce – Automatically set order status after payment is received”

Is it also possible to only get “pending” orders automatically marked as “processing”? Do you know which code I could use to do this? Thanks!

hi Ricardo

I’m trying to use COD shipment method on woo-commerce and after any product (not virtual product) is ordered, the status remains “pending”, even after I mark the orders as “processing” or “completed”.

Any idea how shall I approach this issue OR how can I make “pending” orders as “processing” by default?

Your insight would be great! Thanks.

that is, if the order status is cancelled, it translates into status completed?

in which file i’ve to put this function ?…it’s not working in function.php

Hello there,
Thanks for this code, I believe that it is that answer I was looking for, but I do not know where to place. Can you place tell me?
Thanks

but how
Where should i insert the code?
I want to update an external database after making an order

Glorious! This works – so simple 🙂 I’m using WooThemes PayPal Express plugin to authorize payments then capture them later. This automatically sets the order to “Complete” after I capture the payment. It saves me that one extra click going from “Processing” to “Complete”. Thank you!!!

Helo

Is it possible that processing order automatically change into complete after 7 days

Every processing order change ofter 7 days to complete.. How is this possible?

After paypal Payment my order status showing ‘Hold On’ . Is it passable to auto move ‘Hold on’ status to ‘Complete’ status. And why not showing Complete Status after payment ?

sorry before, i am still a wordpress beginner, i want to ask where should i put this function ?, i write this in my themes function and it doesn’t work
can someone help me ?
thank you 🙂

Hi Daniel just publish a new “version” of this snippet that works with WooCommerce 2.6.8 you can check it here. Thanks.

Dear ricardo, thanks for all usefull informations !

i’m just wondering, what’s the ,10 ,2 at the end of the Add_ function line stands for /reffers to ?

i’m having a problem with this function, it does the job, but the Stock is not updated…

Woocommerce Version 2.6.14
WP version 4.7.2

Hi Robin, the 10 refers to this function priority and the 2 to the number of parameters passed from the filter to the function. You may need to change the priority of you’ve other plugins also using this hook.
About the stock, it should update q automatically when the status changes to complete as it’s tied to that action and not the one we’re using here.
If you can try the newest article it does the same thing but validates the products in the order, it might be more flexible for your needs.

This code was working perfectly for me from last 1 year, but now the woocommerce is updated to latest version i.e 3.1.2 and now this hook is not working and my orders remain in processing status and I need to mark them complete manually. Can you please help me if there is any changes required for the new version of woocomerce?