-1

Have one small issue with my WooCommerce function , what was supposed to work in way i wanted, but seems that have bugs. I want to mark success paid orders as "Completed" instead of stuck on "Processing" after placed, and non success paid orders to mark as "Cancelled" orders instead stuck at "Payment Pending". This is my bugged function i have:

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 ( $order->has_status('processing') ) {
        $order->update_status( 'completed' );
    }
}
daedsidog
  • 1,732
  • 2
  • 17
  • 36
Doroti Henridgz
  • 105
  • 3
  • 13
  • Please redescribe the problem so we know what behavior you were expecting, and how you've determined that your code didn't conform to your expectation. – Ro Achterberg Dec 17 '18 at 18:20
  • Currently with previous posted code, success placed orders, is marking as "Completed", but Non Paid orders are stuck at "Payment Waithing". – Doroti Henridgz Dec 17 '18 at 19:30
  • You mean they're never updated, even when the 'thank you' page is loaded? Try inserting a few `error_log()` statements throughout the code, to track the execution flow. You're not specifying much context, but one thing to consider is that a 'success page' is not always shown after payment is made. And might therefore not be a very reliable event to listen to. – Ro Achterberg Dec 17 '18 at 21:41

1 Answers1

0

You can try something like this:

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

  if ( ! $order_id ) {
    return;
  }

  $order = wc_get_order( $order_id );

  if( $order->has_status('processing') ) {
    $order->update_status( 'completed' );
  } else {
    $order->update_status( 'cancelled' );
  }

}

The code is untested, I just wrote it down.

Let me know if it helps ;)

Cheers, Francesco

FrancescoCarlucci
  • 1,570
  • 12
  • 13