Hi. How can we help?

How to complete automatically all orders that contain gift cards

Thanks to following code all orders that contains at least one gift card virtual will switch automatically to completed status after they have been placed.
Add the code in the file functions.php of your theme.
 
if( !function_exists('yith_wcgc_woocommerce_auto_complete_order') ){
add_action( 'woocommerce_thankyou', 'yith_wcgc_woocommerce_auto_complete_order' );
function yith_wcgc_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}

$order = wc_get_order( $order_id );
$order_items = $order->get_items();

foreach ( $order_items as $item_id => $item_data ){
// Item ID is directly accessible from the $item_key in the foreach loop or
$product = wc_get_product($item_data['product_id']);
if( $product->get_type() == 'gift-card' && $product->is_virtual() ){
$order->update_status( 'completed' );
exit;
}
}


}
}
Was this article helpful?
0 out of 0 found this helpful

Back to Help Center >