Autocomplete WooCommerce orders that contain only virtual products.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment