I have used a hook of woocommerce to complete my order status
add_action( 'woocommerce_thankyou','custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
$order->update_status( 'completed' ); }
Which is working fine.
But when a user tries to cancel the payment from the payment page it and redirect to the site then still the order is getting completed.
Use below code :-
add_action( 'woocommerce_thankyou','custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id || $_GET['payu_in_status'] == 'failed' ) {
return;
}
$order = wc_get_order( $order_id );
$order->update_status( 'completed' ); }
Check this and let me know whether it works...