Skip to content
custom thank you page in WooCommerce featured

How to Redirect to a Conditional Custom Thank You Page in WooCommerce

As you know, the thank you page is the page that is displayed to a client when their order is finalized and includes the order details. The default WooCommerce thank you page can be edited using hooks. Sometimes, however, we want to change the thank you page URL or design completely.

In this article we’re going to show you how to redirect to a custom thank you page in WooCommerce based on various conditions. We’re going to learn how to change the thank you page URL and design using WordPress and WooCommerce native functions and hooks based on the various conditions that we set.

The following code snippets can be used in the JupiterX theme or any other WordPress theme. Please add them to the function.php file in your theme.

Default WooCommerce thank you page

Before we start to write our code snippets, you need to know that the default WooCommerce thank you page is located at the following directory in your website:

wp-content/plugins/woocommerce/templates/checkout/thankyou.php

custom thank you page in WooCommerce - default thank you page

If you are going to show data on another page, you can use this file data and these hooks.

Change WooCommerce thank you page address

Before changing the thank you page URL, you need to make sure that the destination address provides useful information about the order for your client and that you are not redirecting to an unrelated address.
The following code snippet redirects all thank you pages to a page with a custom-address slug:

add_action( 'template_redirect', 'jupiterx_redirect_woocommerce_thankyou_page' );
 
function jupiterx_redirect_woocommerce_thankyou_page() {
    // Make sure we are on the thank you page.
    if( ! is_wc_endpoint_url( 'order-received' ) || empty( $_GET[ 'key' ] ) ) {
        return;
    }
 
    // Our custom URL address.
    $custom_url = site_url( '/custom-address' );
 
    // Redirect client.
    wp_safe_redirect( $custom_url );
 
    // Exit to prevent an infinite loop.
    exit;
}

On the destination page you need to provide order details for the client.

By using SellKit, one of the best WooCommerce funnel builder plugins, you can use beautiful thank you page templates and order details shortcodes for your custom thank you pages without writing a single line of code.

Change WooCommerce thank you page address based on conditions

Let’s change the thank you page address if the payment method is direct bank transfer:

add_action( 'template_redirect', 'jupiterx_redirect_woocommerce_thankyou_page' );
 
function jupiterx_redirect_woocommerce_thankyou_page() {
    // Make sure we are on thank you page.
    if( ! is_wc_endpoint_url( 'order-received' ) || empty( $_GET[ 'key' ] ) ) {
        return;
    }
 
    // Our custom URL address.
    $custom_url = site_url( '/custom-address' );
 
    // Get order id using order key.
    $order_id = wc_get_order_id_by_order_key( $_GET[ 'key' ] );
 
    // Create order object.
    $order = wc_get_order( $order_id );
 
    // Just redirect if payment method is direct bank transfer.
    if ( 'bacs' !== $order->get_payment_method() ) {
        return;
    }
 
    // Redirect client.
    wp_safe_redirect( $custom_url );
 
    // Exit to prevent infinite loop.
    exit;
}

You can handle multiple redirections in one function:

add_action( 'template_redirect', 'jupiterx_redirect_woocommerce_thankyou_page' );
 
function jupiterx_redirect_woocommerce_thankyou_page() {
    // Make sure we are on thank you page.
    if( ! is_wc_endpoint_url( 'order-received' ) || empty( $_GET[ 'key' ] ) ) {
        return;
    }
 
    // Our custom URL address.
    $custom_url_bacs = site_url( '/custom-address' );
    $custom_url_cod  = site_url( '/custom-address-cod' );
 
    // Get order id using order key.
    $order_id = wc_get_order_id_by_order_key( $_GET[ 'key' ] );
 
    // Create order object.
    $order = wc_get_order( $order_id );
 
    // Redirect for BACS.
    if ( 'bacs' === $order->get_payment_method() ) {
        wp_safe_redirect( $custom_url_bacs );
        exit;
    }
 
    // Redirect for BACS.
    if ( 'cod' === $order->get_payment_method() ) {
        wp_safe_redirect( $custom_url_cod );
        exit;
    }
}

We can also redirect clients based on information such as country, postcode and more:

add_action( 'template_redirect', 'jupiterx_redirect_woocommerce_thankyou_page' );
 
function jupiterx_redirect_woocommerce_thankyou_page() {
    // Make sure we are on thank you page.
    if( ! is_wc_endpoint_url( 'order-received' ) || empty( $_GET[ 'key' ] ) ) {
        return;
    }
 
    // Our custom URL address.
    $custom_url_usa     = site_url( '/custom-address-usa' );
    $custom_url_germany = site_url( '/custom-address-eu' );
 
    // Get order id using order key.
    $order_id = wc_get_order_id_by_order_key( $_GET[ 'key' ] );
 
    // Create order object.
    $order = wc_get_order( $order_id );
 
    // Redirect for BACS.
    if ( 'US' === $order->get_shipping_country() ) {
        wp_safe_redirect( $custom_url_usa );
        exit;
    }
 
    // Redirect for BACS.
    if ( 'DE' === $order->get_shipping_country() ) {
        wp_safe_redirect( $custom_url_germany );
        exit;
    }
}

Conclusion

You can redirect your clients to different thank you pages based on your needs using various conditions. As already mentioned, SellKit is one of the best plugins on the market, providing beautiful, ready-to-use templates for your thank you pages and simple shortcodes to provide order details to the client.

Create Your
Dream Website with

Stay in the Loop

Sign up for our newsletter and stay up-to-date on the
latest WordPress trends, insights, and resources.

By entering your email, you agree to our Privacy policy and Terms of Services.

Share

Hadi Mohammade

Hadi Mohammade

I am a web artisan with more than 6 years of experience and am really interested to share my knowledge with everyone out there. Alongside programming I am a professional gamer, playing some online video games with friends after work is the best of fun for me.

No comment yet, add your voice below!


Add a Comment

Your email address will not be published. Required fields are marked *

×

Enjoy 50% OFF Artbees products, and partner deals!

Ends Tue 3 Dec 12:59pm (UTC)