Created
September 4, 2018 23:09
-
-
Save BurlesonBrad/784d473435d0c61fe7444ad5c4ff235b to your computer and use it in GitHub Desktop.
If a WooCommerce Order has VIRTUAL checked, then simply AUTOCOMPLETE that order
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
/** | |
* 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