Skip to content

Instantly share code, notes, and snippets.

@ideadude
Created August 17, 2018 13:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ideadude/52c11783e3bcdddd919ba42cd75a88ef to your computer and use it in GitHub Desktop.
Save ideadude/52c11783e3bcdddd919ba42cd75a88ef to your computer and use it in GitHub Desktop.
Autocomplete WooCommerce orders with only virtual products.
/**
* Autocomplete orders with only virtual products.
* From the 2nd edition of Building Web Apps with WordPress
* https://bwawwp.org
*/
function autocomplete_virtual_orders($order_id) {
//get the existing order
$order = new WC_Order($order_id);
//assume we will autocomplete
$autocomplete = true;
//get line items
if (count( $order->get_items() ) > 0) {
foreach ($order->get_items() as $item) {
if($item['type'] == 'line_item') {
$_product = $order->get_product_from_item( $item );
if(!$_product->is_virtual()) {
//found a non-virtual product in the cart
$autocomplete = false;
break;
}
}
}
}
//change status if needed
if(!empty($autocomplete)) {
$order->update_status('completed', 'Autocompleted.');
}
}
add_filter('woocommerce_thankyou', 'autocomplete_virtual_orders');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment