Skip to content

Instantly share code, notes, and snippets.

@bryceadams
Created November 26, 2014 07:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bryceadams/da8c0eaf9e63a4a695aa to your computer and use it in GitHub Desktop.
Save bryceadams/da8c0eaf9e63a4a695aa to your computer and use it in GitHub Desktop.
<?php
/**
* Autocomplete orders with virtual products (WC 2.2+)
*/
add_filter( 'woocommerce_payment_complete_order_status', 'bryce_autocomplete_virtual_orders', 10, 2 );
function bryce_autocomplete_virtual_orders( $order_status, $order_id ) {
$order = wc_get_order( $order_id );
if ('processing' == $order_status && ('on-hold' == $order->status || 'pending' == $order->status || 'failed' == $order->status)) {
$virtual_order = '';
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() ) {
$virtual_order = false;
break;
} else {
$virtual_order = true;
}
}
}
}
if ( $virtual_order ) {
return 'completed';
}
}
return $order_status;
}
@fului
Copy link

fului commented Apr 7, 2016

I wasn't able to get this gist working in wooCommerce 2.5.5, but I found a way that works, based on the code above and the code on this page https://docs.woothemes.com/document/automatically-complete-orders/

However, I have only tested this with virtual products, so I don't know how it will affect non virtual products.

/**
 * Tested with wooCommerce 2.5.5
 */

add_filter( 'woocommerce_thankyou', 'bryce_autocomplete_virtual_orders' );
function bryce_autocomplete_virtual_orders( $order_id ) {

    $order = wc_get_order( $order_id );
    if( 'processing' == $order->status ) {
        $virtual_order = '';
        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() ) {
                        $virtual_order = false;
                        break;
                    } else {
                        $virtual_order = true;
                    }
                }
            }
        }
        if ( $virtual_order ) {
            $order->update_status( 'completed' );
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment