Skip to content

Instantly share code, notes, and snippets.

@jorpdesigns
Last active July 12, 2021 17:34
Show Gist options
  • Save jorpdesigns/02764950d5bdb9597cacdd4e8eb1a036 to your computer and use it in GitHub Desktop.
Save jorpdesigns/02764950d5bdb9597cacdd4e8eb1a036 to your computer and use it in GitHub Desktop.
Snippet to automatically mark WooCommerce orders containing only virtual products as completed
<?php
add_action('woocommerce_order_status_changed', 'auto_complete_virtual_orders');
function auto_complete_virtual_orders($order_id) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
if ($order->data['status'] == 'processing') {
$virtual_order = null;
if ( count( $order->get_items() ) > 0 ) {
foreach( $order->get_items() as $item ) {
if ( 'line_item' == $item['type'] ) {
$_product = $order->get_product_from_item( $item );
if ( ! $_product->is_virtual() ) {
// once we find one non-virtual product, break out of the loop
$virtual_order = false;
break;
} else {
$virtual_order = true;
}
}
}
}
// if all are virtual products, mark as completed
if ( $virtual_order ) {
$order->update_status( 'completed' );
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment