Created
January 24, 2019 17:09
-
-
Save BurlesonBrad/297e3b62ee512df823c4a39c8e4dc68d to your computer and use it in GitHub Desktop.
Automatically Complete All Virtual Orders In WooCommerce
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 Virtual Orders | |
*/ | |
function autocomplete_virtual_orders_in_woocommerce( $order_id ) { | |
// if there is no order id, then stop | |
if ( ! $order_id ) { | |
return; | |
} | |
// grab the woocommerce order id and its exit | |
$order = wc_get_order( $order_id ); | |
$items = $order->get_items(); | |
// if no items, then stop | |
if ( 0 >= count( $items ) ) { | |
return; | |
} | |
// go through these and double check even down to the variation level | |
foreach ( $items as $item ) { | |
// if it is a variation | |
if ( '0' != $item['variation_id'] ) { | |
// make a product based upon variation | |
$product = new WC_Product( $item['variation_id'] ); | |
} else { | |
// else make a product off of the product id | |
$product = new WC_Product( $item['product_id'] ); | |
} | |
// if the product isn't virtual, exit | |
if ( ! $product->is_virtual() ) { | |
return; | |
} | |
} | |
/* | |
* If all these are indeed virtual then complete the order | |
*/ | |
$order->update_status( 'completed' ); | |
} | |
add_action( 'woocommerce_thankyou', 'autocomplete_virtual_orders_in_woocommerce' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment