1
votes

I would like to automark just success paid orders to "Completed" status. I have searched a lot on Stack and Google, and found this answer code:
WooCommerce: Auto complete paid Orders (depending on Payment methods)

But problem is that the code mark all placed orders to "Completed" status not depending if order was success placed or not.

What do I need to change in the code to automark ONLY Paid orders to "Completed" status?

1

1 Answers

3
votes

New enhanced and simplified code version replacement (March 2019):

See: WooCommerce: Auto complete paid orders


Original answer:

For Paypal and other third party gateways, the "paid" order status to target is "processing" (and "completed"), so you can lightly change the code to:

add_action( 'woocommerce_thankyou', 'wc_auto_complete_paid_order', 20, 1 );
function wc_auto_complete_paid_order( $order_id ) {

    if ( ! $order_id )
        return;

    // Get an instance of the WC_Product object
    $order = wc_get_order( $order_id );

    // No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
    if ( in_array( $order->get_payment_method(), array( 'bacs', 'cod', 'cheque', '' ) ) ) {
        return;
    // Updated status to "completed" for paid Orders with all others payment methods
    } elseif ( in_array( $order->get_status(), array('on-hold', 'processing') ) ) {
        $order->update_status( 'completed' );
    }
}

Code goes in function.php file of the active child theme (or active theme). tested and works.

This way you will avoid "failed", "Cancelled" or "Pending" orders.