WooCommerce Redirect after Checkout: How to Set It Up

WooCommerce Redirect after Checkout: How to Set It Up

How can you redirect customers to a beautiful ‘Thank You’ page that is customized for your specific product?

One of the most common WooCommerce redirects is after the checkout payment. By default, they are redirected to a ‘Thank You’ page.

Likely, you want the user to arrive on a custom page once the order has been placed, where the after-sale service and support can be managed.

This article created by our team at wpDataTables will guide you on how to implement the WooCommerce user redirect to a custom page after checkout.

The WooCommerce Redirect After Checkout Code Snippet

The code snippet below allows for the redirect to a custom page after the user has completed a purchase when using the Subscription add-on plugin. Simply add the snippet to the active child theme function.php and replace the URL in the function wo_redirect() to the appropriate page.

This snippet is frequently used when website owners want to show customers a welcome page after checkout. Welcome pages are great ways to educate customers, upsell a product, and canvas for referrals.

add_action( 'woocommerce_thankyou', 'bbloomer_redirectcustom');

function bbloomer_redirectcustom( $order_id ){

    $order = wc_get_order( $order_id );

    $url = 'https://yoursite.com/custom-url';

    if ( ! $order->has_status( 'failed' ) ) {

        wp_safe_redirect( $url );

        exit;

    }

}

Setting Up a WooCommerce Thank You Page, by Overwriting WooCommerce Templates

To redirect the user after an order has been placed, you need to use the template redirect hook and a callback function with the redirect URL. In a quick summary here are the necessary steps:

First, it is good practice to check if the user is on the checkout page, order page, or on the order received page before you create the custom redirect after checkout.

Redirecting customers to a custom page automatically after the order is placed needs the template_redirect() function.

Add the template_redirect action hook for example

-? add_action('template_redirect', 'your_callback_function');

Create the callback function you referenced in the template redirect hook above, noting that the name in the call back function and the hook should match.

In the callback function, use the wp_redirect() function to add the page where you want the user to be redirected after successful checkout. Always add an exit after the wp_redirect function to avoid redirect problems.

This code is added to your functions.php file in the theme. Locate that file and open it in the editor to add this action hook for WooCommerce redirect after checkout.

Save the changes or update your theme functions.php or the plugin file and you will have successfully created the redirect to the preferred page after WooCommerce checkout.

Customizing WooCommerce Thank You Page with Filters

Changing the Thank You Page Title

Instead of creating a custom ‘Thank You’ page, as shown above, you might just want to change the title of the page.

You can add the below snippet with a plugin or in the theme’s functions.php file.

<?php

add_filter( 'the_title', 'woo_title_order_received', 10, 2 );

 

function woo_title_order_received( $title, $id ) {

            if ( function_exists( 'is_order_received_page' ) && 

                 is_order_received_page() && get_the_ID() === $id ) {

                        $title = "Thank you for your order! :)";

            }

            return $title;

}

WooCommerce provides the function is_order_received_page() in includes/wc-conditional-functions.php file.

Personalize the Title

You can enhance your website by personalizing the ‘Thank You’ page title to include details like the customer’s name or welcoming them in their native language.

You can add the below code with a plugin or in your theme’s functions.php:

<?php

 add_filter( 'the_title', 'woo_personalize_order_received_title', 10, 2 );

function woo_personalize_order_received_title( $title, $id ) {

    if ( is_order_received_page() && get_the_ID() === $id ) {

                global $wp;

         // Get the order. Line 9 to 17 are present in order_received() in includes/shortcodes/class-wc-shortcode-checkout.php file

        $order_id  = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars['order-received'] ) );

        $order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );

 

        if ( $order_id > 0 ) {

            $order = wc_get_order( $order_id );

            if ( $order->get_order_key() != $order_key ) {

                $order = false;

            }

        }

 

        if ( isset ( $order ) ) {

            //$title = sprintf( "You are awesome, %s!", esc_html( $order->billing_first_name ) ); // use this for WooCommerce versions older then v2.7

                $title = sprintf( "You are awesome, %s!", esc_html( $order->get_billing_first_name() ) );

        }

    }

    return $title;

}

Changing the Text Before Order Information

The filters available in WooCommerce allow you to change the text shown on the ‘Thank You’ page. The default text that is shown before the order information is “Thank you. Your order has been received.”.

This text comes from the file templates/checkout/thankyou.php. The filter available to change this text is: woocommerce_thankyou_order_received_text

<?php
 

add_filter('woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 10, 2 );

function woo_change_order_received_text( $str, $order ) {

    $new_str = $str . ' We have emailed the purchase receipt to you.';

    return $new_str;

}

The above code will append the text “We have emailed the purchase receipt to you.” to the existing text.

If you want to overwrite the text completely, you simply need to avoid chaining together the $str variable to your message variable $new_str.

$new = ‘We have emailed the purchase receipt to you.’;

If you want the customer to download a form, this is when the instructions can be added.

<?php

 

add_filter('woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 10, 2 );

function woo_change_order_received_text( $str, $order ) {

    $new_str = 'We have emailed the purchase receipt to you. Please make sure to fill <a href="http://localhost:8888/some-form.pdf">this form</a> before attending the event';

    return $new_str;

}

I Don’t Code – Is There a Trustworthy Plugin for That?

 Writing code is a skill but these plugins can be used to achieve the same result.

This example is for the WooCommerce Redirect Thank You plugin.

Installing the Plugin

After installing the WooCommerce Redirect Thank You plugin, you can create your ‘thank you’ pages. You may want to set up a general thank you page appropriate for global usage, or you can design pages to showcase specific products.

This allows for cross-selling, up-selling, discounts, or coupons, all of which could promote future sales. In the ‘Thank You’ coding example shown above an up-sell with a coupon offer has been implemented using a WooCommerce block.

In the WordPress dashboard, hover over WooCommerce and click on Settings, next select the ‘Thank You’ tab.

(Optional) If you want to set a global ‘Thank You’ page that customers will be sent to on order completion, you can set that page with this setting. This option overrides the WooCommerce order completed page setting.

In the license key field, add the license key sent in the order confirmation email. This will activate notifications of new versions of the plugin and allow automatic updates from your WordPress dashboard.

Other Plugins

YITH Custom Thank You Page for WooCommerce

Boxy WooCommerce custom redirect after checkout

Get Creative with Your Thank You Page

Controlling the design allows for creativity with your ‘Thank You’ page. By using a page builder, you can enhance your customer’s experience, strengthening retention rates.

Keep your ‘Thank You’ page relevant, perhaps including a video of the store owner recommending a product. When you provide information appealing to your customer, they will be motivated to engage your services in the future.

If you enjoyed reading this article about setting up the WooCommerce Redirect after Checkout, you should read these as well:

Up Next:

How to create the Premier League table for your site

How to create the Premier League table for your site