Auto complete an order

//******************************************************
//******************************************************
//********** AUTO COMPLETE WOOCOMMERCE ORDERS **********
//******************************************************
//******************************************************
add_action( 'woocommerce_thankyou', 'my_woocommerce_thankyou' );
function my_woocommerce_thankyou( $order_id )
{ 
  if (!$order_id)
      return;

  $order = wc_get_order($order_id);
  
  //----- AUTO-COMPLETE THE ORDER -----
  //Don't auto complete for orders paid with Bank wire, Cash on delivery and Cheque payment methods
  if (in_array($order->get_payment_method(), array( 'bacs', 'cod', 'cheque', '')))
  {
    return;
  } 
  elseif($order->has_status('processing'))    //For paid orders with all other payment methods auto complete the order
  {
    $order->update_status( 'completed' );
  }
  
}

Redirect on order completed

//*********************************************************
//*********************************************************
//********** REDIRECT ON WOOCOMMERCE ORDER COMPLETE **********
//*********************************************************
//*********************************************************
add_action( 'woocommerce_thankyou', 'my_woocommerce_thankyou' );
function my_woocommerce_thankyou( $order_id )
{ 
  if (!$order_id)
      return;

  $order = wc_get_order($order_id);
 
  //----- REDIRECT ON ORDER COMPLETED -----
  $url = 'https://yoursite.com/custom-url';
  if (!$order->has_status('failed'))
  {
    wp_safe_redirect( $url );
    exit;
  } 
}
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *