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
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.
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.
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.
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.
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.
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.
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.