Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hchouhan/227d09b80cc614f557dfd3fbc9c67f96 to your computer and use it in GitHub Desktop.
Save hchouhan/227d09b80cc614f557dfd3fbc9c67f96 to your computer and use it in GitHub Desktop.
// Mark virtual orders as complete instead of processing
function rp4wp_virtual_order_payment_complete_order_status( $order_status, $order_id ) {
$order = new WC_Order( $order_id );
if ( 'processing' == $order_status && ( 'on-hold' == $order->status || 'pending' == $order->status || 'failed' == $order->status ) ) {
$virtual_order = false;
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've found one non-virtual product we know we're done, break out of the loop
$virtual_order = false;
break;
} else {
$virtual_order = true;
}
}
}
}
// virtual order, mark as completed
if ( $virtual_order ) {
return 'completed';
}
}
// non-virtual order, return original status
return $order_status;
}
add_filter( 'woocommerce_payment_complete_order_status', 'rp4wp_virtual_order_payment_complete_order_status', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment