Orders pending payment in woocommerce when paying

Hello Victor.

You can modify the state in which you want them to stay, for example, if you want them to stay processing:

// Automatically update the status of orders to processing
add_action( ‘woocommerce_order_status_processing’, ‘update_order_status_to_completed’ );
add_action( ‘woocommerce_order_status_on-hold’, ‘update_order_status_on-hold’ );
function update_order_status_to_completed( $order_id ) {
global $woocommerce;

//ID’s of the payment gateways affected
$paymentMethods = array( ‘redsys’, ‘bacs’, ‘cheque’, ‘cod’, ‘paypal’ );

if ( !$order_id ) return;
$order = new WC_Order( $order_id );

if ( !in_array( $order->payment_method, $paymentMethods ) ) return;
$order->update_status( ‘processing’ );
}

We only modify the line ” $order->update_status( ‘processing’ ); ” indicating the state in which the order will remain, in this case Processing.

You can put the state you want:

  • Completed: Order completed and complete.
  • Pending (pending payment): Order received, no payment yet
  • Processing: Payment has been received and inventory has been updated to remove the item unit. The order is pending to be completed manually
  • On hold: Payment pending payment. The item is deducted from the inventory but before it is completed you have to confirm the payment and complete it (manually)
  • Cancelled: Canceled order, either by the customer or a store manager

For virtual products you can use the following:

add_action(‘woocommerce_thankyou’, ‘we_autocomplete_virtual_orders’, 10, 1 );
function we_autocomplete_virtual_orders( $order_id ) {
if( ! $order_id ) return;
// Get order
$order = wc_get_order( $order_id );
// get order items = each product in the order
$items = $order->get_items();
// Set variable
$only_virtual = true;
foreach ($items as $item ) {
// Get product id
$product = wc_get_product( $item );
// Is virtual
$is_virtual = $product->is_virtual();
// Is_downloadable
$is_downloadable = $product->is_downloadable();
if ( ! $is_virtual && ! $is_downloadable ) {
$only_virtual = false;
}
}
// true
if ($only_virtual) {
$order->update_status( ‘completed’ );
}
}

See also  Buy Now Button - Paypal - in Wordpress Checkout

The same as the previous one, if you want the product to remain in another state, you only have to modify the line ” $order->update_status( ‘completed’ );

All the best

AnswerQuote

Answered: 04/14/2022 11:12 am

Loading Facebook Comments ...
Loading Disqus Comments ...