Home > database >  How to redirect to thank you page after woocommerce checkout order processed
How to redirect to thank you page after woocommerce checkout order processed

Time:03-12

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();
        } 
    }
}

CodePudding user response:

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

CodePudding user response:

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 :)

  • Related