Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BurlesonBrad/784d473435d0c61fe7444ad5c4ff235b to your computer and use it in GitHub Desktop.
Save BurlesonBrad/784d473435d0c61fe7444ad5c4ff235b to your computer and use it in GitHub Desktop.
If a WooCommerce Order has VIRTUAL checked, then simply AUTOCOMPLETE that order
/**
* 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