Home > WooCommerce > WooCommerce Redirect to Custom Thank You Page (After Checkout)

WooCommerce Redirect to Custom Thank You Page (After Checkout)

Do you want to create a woocommerce custom thank you page and redirect to this page after checkout the order? Then you are in right place. In this tutorial, I’ll show you how to create a custom thank you page and redirect to it after checkout.

The woocommerce thank you page is that where it shows the order details after place the order. When the user has products in the cart and makes an order for those products then after placing the order user will redirect to the default thank you page. You will see the order detail on this page.

What if we make a beautiful custom thank you page and redirect the users to this custom page? For this, we have to make an action hook using the template_redirect or woocommerce_thankyou action. You have to add this hook functionality into the theme’s functions.php file.

Note: Recommend to use a child theme for any customization. Check this tutorial on how to create a child theme in WordPress?

So let’s make this functionality to achieve the redirection on the custom thank you page after checkout.

WooCommerce Redirect to Custom Thank You Page

Before redirecting, make sure you have followed the below steps to place an order.

  • Add a product into woocommerce cart and click on the proceed to checkout button.
  • Check the order details and select the payment method.
  • Now click on the place order button.
  • After successfull order, you will redirect to order recieved (deafult thank you) page.

Let’s implement the hook functionality to change the default woocommerce thank you page and redirect it. Users will redirect to the custom thank you page after successful payment.

This is a very useful woocommerce redirect hook that we used when we want to showcase the up-sell product on thank you page or want to show custom beautifully landing page. We will use woocommerce_thankyou action hook to make redirection.

OK.

So, add the following code snippet in your active theme’s functions.php file and save it.

The above code snippet will redirect the users to custom thank you page. In this code, first we get full details of order and then check the order has not been failed. If the order has successfully status then it will redirect the users to custom made thank you page.

Make sure you have created that custom page (‘custom-page-url‘) and have the same page slug as you will define in this hook function.

WooCommerce Custom Redirect After Checkout

You can make many custom redirection after checkout based on the specific conditions or could be multi-conditions. There could be several scenarios based on conditions and you can redirect the users to custom page or any landing page based on the conditions.

Let’s short breif some important scenarios.

1. Redirect After Checkout Based on Payment Method

In this type of redirection, we will check the what payment method was used by user. If the payment method match with our condition then it will redirect to custom page. You can also check the shiiping class and match with condition. Check the general guide on shipping class and payment method.

Function reference:
$order->get_payment_method();

2. Redirect After Checkout Based on Specific Product

In this redirection, we will check specific product then redirect to custom page. For example, when users purchase a ‘Batman T-shirt’ then it will redirect to custom landing page otherwise it will restrict to woocommerce default order recieved page.

Function reference:
$order->get_id(); $item->get_product_id();

3. Redirect After Checkout Based on User

In this redirect, we can check the specific user by ID, username, email, etc. If the user is match then you can make redirection condition and redirect to custom page. You can also redirect to custom page based on the user role.

Function reference:
$order->get_user_id(); $order->get_user();

4. Redirect After Checkout Based on Order Total

Here we will check the total price of order, if the order total will exist in specific range, means if condition is true then it will redirect to custom page. You just have to get the order total from order object then check with condition.

Function reference:
$order->get_total();

There could be more scenarios to redirect after checkout when user has placed an order. Like you can check the geographical location, you can check the purchase history, you can check the coupon code and discount, you can check the time date range of order, etc. Check the tutorial on order details to make suitable conditions for redirection.

Let’s have a look into the first scenario to redirect on woocommerce custom thank you page based on the payment method after checkout.

First, we will check url has ‘key‘ query parameter (order key). Then we will get the order ID from that key using the wc_get_order_id_by_order_key() function. Then get the payment method using the get_payment_method() function and check with condition. If the condition true then it will redirect to custom thank you page.

See the following code snippet.

In the above code, we checked if the payment method is ‘cod‘ then redirect to another page. Make sure you have created that page. If the payment method is not ‘cod‘ then it will redirect to normal deafult woocommerce thank you page.

Conclusion

So in this tutorial, you learned how to create custom thank you page and redirect to it after checkout when user has placed the order.

We also discussed about the many scenarios for woocommerce custom redirect after checkout and explained the one with exmaple code snippet for specific payment method.

I hope you understand the tutorial. If you still have any questions please ask me in the comment section, I’ll respond to you as soon as possible.

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

5 thoughts on “WooCommerce Redirect to Custom Thank You Page (After Checkout)”

    • You can get the customer order count using get_posts() function with post_type => 'shop_order' and then make condition accordingly. You can also use the wc_customer_bought_product() function with customer’s ID, email, or product id.

      Reply
  1. Can I ban someone from entering the thank you page if they haven’t purchased? some kind of redirection if you haven’t bought that product

    Reply
  2. Is it possible to add a code to the new address, which will be generated within each order and will be original?

    By default, if the cash register is redirected to the original thank you page, it looks something like this yyy.com/order-received/15470/?key=wc_order_ybsEAyMNKxmNx&utm_nooverride=1 and it’s safer.

    Hypothetically, if a customer saves a url from my new thank you page, it will always be in the form yyy.com/thank-you-page/. This is not good for security, because if you open it, they will always see the last generated purchase.

    Is there a solution? thanky fo answer

    Reply

Leave a Comment