<?php
/**
* Autocomplete orders that contain only virtual products.
* If the order contains a non-virtual product the order will not be completed by this function.
*
* @author Mike Hemberger @JiveDig
*
* @version 1.0.0
*
* @return string Order status.
*/
add_filter( 'woocommerce_payment_complete_order_status', 'prefix_autocomplete_virtual_orders', 10, 2 );
function prefix_autocomplete_virtual_orders( $order_status, $order_id ) {
$order = wc_get_order( $order_id );
if ( 'processing' != $order_status ) {
return $order_status;
}
$all_virtual_products = false;
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 not a virtual product.
if ( ! $_product->is_virtual() ) {
$has_non_virtual_product = true;
break;
}
}
}
}
// If no non-virtual products.
if ( ! $has_non_virtual_product ) {
$all_virtual_products = true;
}
// All virtual products, mark as completed.
if ( $all_virtual_products ) {
return 'completed';
}
// Non-virtual order, return original status.
return $order_status;
}