简体   繁体   中英

How to redirect to thank you page after woocommerce checkout order processed

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:

在此处输入图像描述

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM