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.

Comments

26 responses to “WooCommerce – Automatically set order status after payment is received”

  1. NielsPilon Avatar
    NielsPilon

    This snippets works really well, thanks for posting!

  2. Lee Avatar
    Lee

    Blimey – worked perfectly, thanks!

  3. AnneGTS Avatar
    AnneGTS

    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!

    1. kookuk Avatar
      kookuk

      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.

  4. Amjad Avatar
    Amjad

    Any way to make pending orders to processing instead of complete? Thanks.

  5. Вячеслав Зудвин Avatar
    Вячеслав Зудвин

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

  6. Dano Avatar
    Dano

    Awesome- just what I’ve been looking for! Thanks Ricardo- you rock

  7. Mohini Modi Avatar
    Mohini Modi

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

  8. Majama Avatar
    Majama

    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

    1. Ricardo Correia Avatar
      Ricardo Correia

      You need to place the function in functions.php file.

      1. stive Avatar
        stive

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

        1. Ricardo Correia Avatar
          Ricardo Correia

          Hi,
          You need to insert the code in your function.php file, the code on this article is out of date, please take a look at http://rcorreia.com/woocommerce/wc-and-wcs-automatically-set-order-status-to-completed/

  9. Zack Avatar
    Zack

    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!!!

  10. Md asad Avatar
    Md asad

    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?

  11. kony Avatar
    kony

    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 ?

  12. Javier Avatar

    Help me a lot!!! Thanks for sharing mate!

  13. ali Avatar
    ali

    awesome! just what i needed. thank you.

  14. ivan Avatar
    ivan

    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 🙂

    1. Daniel Avatar
      Daniel

      it doesn’t work with woocommerce 2.6.8

      1. Ricardo Correia Avatar
        Ricardo Correia

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

  15. Robin Avatar
    Robin

    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

    1. Ricardo Correia Avatar
      Ricardo Correia

      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.

      1. Robin Avatar
        Robin

        ok thanks a lot, i finally managed to get this work uing the paypal IPN + free extension to autocomplete orders .

        https://wordpress.org/plugins/woocommerce-autocomplete-order/screenshots/

  16. Maneesh Garg Avatar
    Maneesh Garg

    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?

    1. Ricardo Correia Avatar
      Ricardo Correia

      Hi Maneesh,
      You can see an updated version of this code in http://rcorreia.com/woocommerce/wc-and-wcs-automatically-set-order-status-to-completed/

  17. Maneesh Garg Avatar
    Maneesh Garg

    Thanks Ricardo.