Skip to content

Instantly share code, notes, and snippets.

@BurlesonBrad
Created January 24, 2019 17:09
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/297e3b62ee512df823c4a39c8e4dc68d to your computer and use it in GitHub Desktop.
Save BurlesonBrad/297e3b62ee512df823c4a39c8e4dc68d to your computer and use it in GitHub Desktop.
Automatically Complete All Virtual Orders In WooCommerce
/**
* 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