Automatically Complete WooCommerce Orders

  Javascript

Here’s a script you can use to automatically complete virtual orders from any status. This example uses the ‘processing’ status.


add_action(‘woocommerce_order_status_changed’, ‘ts_auto_complete_virtual’);

function ts_auto_complete_virtual($order_id)
{

if ( ! $order_id ) {
return;
}

global $product;
$order = wc_get_order( $order_id );

if ($order->data[‘status’] == ‘processing’) {

$virtual_order = null;

if ( count( $order->get_items() ) > 0 ) {

foreach( $order->get_items() as $item ) {

if ( ‘line_item’ == $item[‘type’] ) {

$_product = $order->get_product_from_item( $item );

if ( ! $_product->is_virtual() ) {
// once we find one non-virtual product, break out of the loop
$virtual_order = false;
break;
}
else {
$virtual_order = true;
}
}
}
}

// if all are virtual products, mark as completed
if ( $virtual_order ) {
$order->update_status( ‘completed’ );
}
}
}

For full explanation and script to automatically set to complete status based on payment method, check out this link: https://www.tychesoftwares.com/how-to-automatically-complete-woocommerce-orders-when-they-go-to-the-processing-status/

LEAVE A COMMENT