How to Create WooCommerce Redirect After Checkout

WooCommerce redirect after checkout custom redirect
Do you want to create a custom WooCommerce Redirect After Checkout that takes your customers to a beautiful landing page? The most common WooCommerce redirects is after the payment or checkout. By default, the customer is redirected to the default WooCommerce ‘Thank You’ page.

If you are looking for the best solution to create a WooCommerce redirect after checkout; this post will guide you on how to implement the WooCommerce user redirect after checkout to a custom thank you page.

This can be a good solution for the user to be redirected to a page where they can have after-sale service or customer support after successful checkout.

Create WooCommerce Redirect After Checkout Step by Step

The ideal way to create a WooCommerce redirect after checkout is to understand the steps involved in the checkout process from the cart page to when the customer makes a payment. The steps can be outlined as follows:

  1.  The customer places the order after clicking on proceed to checkout from the WooCommerce cart page
  2. The customer checks the content of the cart and selects the payment method
  3. The customer proceeds with the order by clicking the Place Order button
  4. . If the payment is successful, the customer is redirected to the thank you page

To create a WooCommerce redirect after checkout you need to intercept the default WooCommerce after checkout redirect using an action filter as you will see in the snippet I will share in the next section.

Let us look at how to implement a quick solution that redirects users after checkout is complete. I have two perfect solutions for WooCommerce redirect after checkout.

You can use the code snippet I will share in this tutorial or you can implement the redirect based using the plugin I specially created to solve this problem.

If you do not want to edit the functions.php file or add snippets and prefer to use a plugin, I have created a plugin for this redirect and you can find it here.

WooCommerce Redirect After Checkout Code Snippet

This WooCommerce code snippet shared below can be added to the functions.php file to create a redirect to a custom page after the user has completed a payment.

For this snippet to be effective you need to add it to the active child theme functions.php and change the URL in the functions wp_redirect() to the custom page you want to redirect the users.

This is a useful WooCommerce redirect snippet that is often used by store owners who want to showcase up-sell products on the thank you page or who want to show customers a  custom welcome page after checkout is completed.

This can be a great solution for a WooCommerce membership site and can be a good place to help users start the onboarding process.

add_action( 'woocommerce_thankyou', 'njengah_woocommerce_redirect_after_checkout');

function njengah_woocommerce_redirect_after_checkout( $order_id ){

$order = wc_get_order( $order_id );

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

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

wp_safe_redirect( $url );

exit;

}

}

WooCommerce Custom Redirect After Order is Placed

You can create WooCommerce redirect after checkout based on several conditions before we look at how to create the general WooCommerce redirect after checkout let me tabulate the most common ways to create WooCommerce after checkout redirects :

Redirect Option Description & Example
WooCommerce Redirect After Checkout  Per Product This is the type of redirect that is applied to only a specific product. For example when a user buys an apple they are redirected to the Page A but if they buy any other product like an orange they are redirected to the normal WooCommerce redirect order page.
WooCommerce Redirect After Checkout Per Category This is the type of redirect that is applied to only a specific product category. For example when a user buys a product in Category Apples they are redirected to the Page A but if they buy any other product in Category Oranges they are redirected to the normal WooCommerce redirect order page.
WooCommerce Redirect After Checkout for Specific User This is the type of redirect that is applied to only a specific user. For example when a user with ID 32 buys an apple they are redirected to the Page A but if another user with ID 35  buy an apple they are redirected to the normal WooCommerce redirect order page.
WooCommerce Redirect After Checkout for Specific User Role This is the type of redirect that is applied to only a specific user role. For example when a user with user role MEMBER buys an apple they are redirected to the Page A but if another user with any other user role buy an apple they are redirected to the normal WooCommerce redirect order page.
WooCommerce Redirect After Checkout for Specific Payment Method This is the type of redirect that is applied to only a specific payment method. For example when a user pays for an apple via PayPal they are redirected to the Page A but if another user pays for the Apple with Direct bank transfer  they are redirected to the normal WooCommerce redirect order page.
WooCommerce Redirect After Checkout for Specific Time or Date Range This is the type of redirect that is applied to only a specific time or date. For example when a buys an apple at between 12:00 to 1300 or between date 1st and 3rd  they are redirected to the Page A but if same user buys the Apple with at different time and dates they are redirected to the normal WooCommerce redirect order page.
WooCommerce Redirect After Checkout for Specific Purchase History This is the type of redirect that is applied to only a specific customer purchase history. For example when a user buys an apple and they had bought an knife before they are redirected to the Page A but if they buy an apple but they had not bought a knife before they are redirected to the normal WooCommerce redirect order page.
WooCommerce Redirect After Checkout for Specific Geographical Location This is the type of redirect that is applied to only a specific country. For example when a user in the US  buys an apple they are redirected to the Page A but if another user in UK buys an apple they are redirected to the normal WooCommerce redirect order page.
WooCommerce Redirect After Checkout for Specific Order Total This is the type of redirect that is applied to only  to order amount total. For example when a user order total is $500 they are redirected to the Page A but if another user order is (Not Equal, Less Than, Greater Than) $500  they are redirected to the normal WooCommerce redirect order page.
WooCommerce Redirect After Checkout for Specific Coupon / Product Discount This is the type of redirect that is applied to only discount or coupons. For example when a user applied a specific coupon they are redirected to the Page A but if another user applied a different coupon code or applied no coupon code they are redirected to the normal WooCommerce redirect order page.
WooCommerce Redirect After Checkout for Specific Shipping This is the type of redirect that is applied to only shipping methods. For example when a user selects flat shipping rate they are redirected to the Page A but if another user selects free shipping they are redirected to the normal WooCommerce redirect order page.

To redirect the user after the order is placed you need to use the template redirect hook and a callback function with the redirect URL. In a quick summary here are the details in step by step :

  • #1) First it’s a good practice to check if the user is in the checkout page, order page or the order received page before you create the custom redirect after checkout.
  • #2)To redirect your customers to a custom page automatically after the order is placed we need to make use of the template_redirect() function.
  • #3) Add the template_redirect action hook for example -? add_action(‘template_redirect’, ‘your_callback_function’);
  • #4) Create the call-back function you referenced in the template redirect hook above and the name should match the name used in the hook.
  • #5) 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.
  • #6)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 the WooCommerce redirect after checkout.
  • #7) Save the changes or update your theme functions.php or the plugin file and you will successfully create the redirect to the preferred page after WooCommerce checkout.

WooCommerce Redirect Action Hook

First, we create an action hook with the template redirect and a callback function as shown below:

add_action( ‘template_redirect’, ‘woocommerce_redirect_after_checkout’ );

This code registers the template redirect hook that implements the callback function that we will use to target the ‘order placed’ action. Basically, this tells WordPress that whenever an order is placed the redirect action is initiated.

WooCommerce Redirect Callback function

We now create the callback function that has the code that is executed in the redirect. The following is the code for the callback function:

function woocommerce_redirect_after_checkout() {
global $wp;

if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {

$redirect_url = 'https://yoursite/url-to-redirect-after-checkout';

wp_redirect($redirect_url );

exit;
}

}

In this callback function, we are using the global $wp object to access the post query method to find out if the order has been placed and if place the user is redirected to the predefined redirect URL.

This code should be added to the functions.php file and the complete code should be as follows:

add_action( ‘template_redirect’, ‘woocommerce_redirect_after_checkout’ );

 

function woocommerce_redirect_after_checkout() {

global $wp;

 

if ( is_checkout() && ! empty( $wp->query_vars[‘order-received’] ) ) {

 

$redirect_url = ‘https://yoursite/url-to-redirect-after-checkout’;

 

wp_redirect($redirect_url );

 

exit;

}

 

}

After adding this code to the functions.php file in your theme; test with one product to ensure the redirect is successful after clicking the place order button as shown below:

Comments are closed.