I want to redirect to a custom URL after checkout for a single product in Woocommerce, have all other products continue to redirect to the default thank you/order summary page.
I tried using the code snippet in the first answer on this post:
WooCommerce Custom Thankyou redirection based on Product ID
I replaced the product IDs listed as an example in this snippet with the single product ID for the product I want to produce a redirect after checkout for, left the category array blank, and updated the URL. When I tested the checkout redirect for that particular product, but when I tested for other products, they were also redirecting instead of going to the default thank you/order confirmation page.
I know the post is a little old but it says that the code was tested on WooCommerce 3. Is there something I’m missing or should I go another route instead of this snippet?
2
Answers
When you say you left the array blank, you’d be better off removing the array as it’ll return true for every product.
I.e.
has_term()
is asking, does this product have a term / (category)? The answer is almost always going to be True in Woocommerce where the products are sat in store categories.So, when you see the original code in the answer below, it’s testing against the terms that you set in the $product_categories variable.
The solution, get rid of the has_term entirely and replace the above section with this:
To enable a redirection after checkout, when only targeted product(s) are exclusively in the order, use the following revisited code instead:
Code goes in functions.php file of the active child theme (or active theme). Tested and works.