skip to Main Content

im trying to change order status to "completed" and skip payment page (go to thank you page) after order is processed.

so far i manage to change order status to completed but instead of redirecting me to thank you page, i will be redirected to payment page with the following error :

enter image description here

this is the code i use :

add_filter( 'woocommerce_checkout_order_processed' , 'skip_join_payment' );
function skip_join_payment( $order_id ) {
     if ( ! $order_id ) {
        return;
    }
    if( $_COOKIE['isjoinForFree'] == "yes" ){
        $order = wc_get_order( $order_id );
        $order->update_status( 'completed' );

        ** i tried to redirect here but getting error on checkout page **
        // if ( $order->has_status( 'completed' ) ) {
                // header("Location: /jtnx/");
                // die();
        // } 


    }
}

in addition i tried adding another hooks with redirect :

add_action( 'woocommerce_payment_complete', 'skiped_join_payment_redirect', 1 ); 
function skiped_join_payment_redirect ( $order_id ){
    $order = wc_get_order( $order_id );
    if( $_COOKIE['isjoinForFree'] == "yes" ){
    $order = wc_get_order( $order_id );
    if ( $order->has_status( 'completed' ) ) {
            header("Location: /jtnx/");
            die();
        } 
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    if anyone step into this error that was my fix. the reason for the redirect fail occur cause of the checkout ajax still running, i added this code into my function.php, works well.

    add_action( 'template_redirect', 'joined_for_free_redirect_to_thankyou' );
        function joined_for_free_redirect_to_thankyou(){
            if( !is_wc_endpoint_url( 'order-pay' )) {
                return;
            }
            
            global $wp;
            $order_id =  intval( str_replace( 'join/order-pay/', '', $wp->request ) );
            $order = wc_get_order( $order_id );
            if ( $order->has_status( 'completed' ) && $_COOKIE['isjoinForFree'] == "yes" ) {
                wp_redirect( site_url() . '/jtnx/' );
                exit;
            } 
        }
    

    if anyone think he has a better way to fix it i will still be happy to hear :)


  2. try steps below:

    1. use absolute, full target page address in header like
      header('Location: http://www.example.com/');. See header for the reason.
    2. Ensure that if( $_COOKIE['isjoinForFree'] == "yes" ) { is true.
    3. If you need, insert the else { part of if( $_COOKIE['isjoinForFree'] == "yes" ) { loop
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search