WooCommerce: Redirect to Custom Thank You Page

A customer recently needed to redirect customers to a custom thank-you page, specifically for a particular product variation. Below, you’ll find three useful snippets that enable you to redirect customers after an order. The first snippet redirects all orders, the second redirects customers when a single variation ID matches, and the third redirects customers when you want to specify more than one variation ID for redirection.

Redirect All Orders

add_action( 'woocommerce_thankyou', 'woo_custom_thank_you');
  
function woo_custom_thank_you( $order_id ){
    $order = wc_get_order( $order_id );
    $url = 'https://yourwebsite.com/thank-you-page';
    if ( ! $order->has_status( 'failed' ) ) {
        wp_safe_redirect( $url );
        exit;
    }
}

Redirect A Single Product Variation ID

add_action( 'woocommerce_thankyou', 'redirect_one_variation', 1 );

function redirect_one_variation( $order_id ) {
    $order = wc_get_order( $order_id );
    foreach( $order->get_items() as $item ) {
        // Add Your Variation ID Here
        if ( isset( $item['variation_id'] ) && $item['variation_id'] == 1005 ) {
            // Set Custom Page URL Here
            $redirect_url = 'https://yourwebsite.com/thank-you-page';
            wp_safe_redirect( esc_url( $redirect_url ) );
            exit;
        }
    }
}

Redirect Multiple Variation IDs

add_action( 'woocommerce_thankyou', 'redirect_multiple_variations', 1 ); 
function redirect_multiple_variations( $order_id ) {
    $order = wc_get_order( $order_id );

    // Array of Variation IDs to redirect
    $variation_ids_to_redirect = array( 10719, 10721, 10723, 10725 );

    foreach( $order->get_items() as $item ) {
        if ( isset( $item['variation_id'] ) && in_array( $item['variation_id'], $variation_ids_to_redirect ) ) {
            // Set Custom Page URL Here 
            $redirect_url = esc_url( 'https://yourwebsite.com/thank-you-page' );
            wp_safe_redirect( $redirect_url );
            exit(); // Exit after the redirect
        }
    }
}

WordPress Maintenance from £30.00/month

Experience unparalleled WordPress support and maintenance with our exceptional service plans. Entrust us with all your WordPress needs, guaranteeing the seamless operation and upkeep of your website.

Get Started