Search code examples
jquerywordpresswoocommercehook-woocommercewoocommerce-bookings

Woocommerce Autocomplete Issues on Cancellation


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.


Solution

  • 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...