UPDATE: This method is fine if you want to redirect a product with a specific product category. However, if you don’t need this category requirement, I released a plugin on WordPress.org for WooCommerce add-to-cart custom redirects on a per-product basis.
Recently, I helped a reader with a customization they were looking for in their WooCommerce store. Their goal was to redirect users after they added a specific product to their cart. This redirect (add Product A to cart and redirect to Product B) would only happen if Product A was added AND Product A belonged to a certain category. I wrote the code to work out the redirect but, at first, the problem was that it would redirect regardless of what product was added to the cart. Finally, I was able to fix it and the resulting code is pasted below (which can be pasted into your theme’s functions.php file):
add_filter( 'woocommerce_add_to_cart_redirect', 'rv_redirect_on_add_to_cart' ); function rv_redirect_on_add_to_cart() { //Get product ID if ( isset( $_POST['add-to-cart'] ) ) { $product_id = (int) apply_filters( 'woocommerce_add_to_cart_product_id', $_POST['add-to-cart'] ); //Check if product ID is in the proper taxonomy and return the URL to the redirect product if ( has_term( 'posters', 'product_cat', $product_id ) ) return get_permalink( 83 ); } }
What this code is doing is:
- Gets the ID of the Product that was just added to the cart;
- Checks to see if that product is in the correct WooCommerce product category;
- Gets the permalink of the redirect product and returns it to the WooCommerce add_to_cart_redirect filter.
Buğra YAZAR says
It really worked for me, thanks ren!
Alkesh Miyani says
I am looking to stop redirection.
I don’t want to redirect my customer anywhere. Just want to show message as items are added or not.
yes, I am looking for that such functionality on my custom search result page.
Adding items in cart is working nice but there is low quantity and customer trying to add items x+1 items then it redirecting to me on product detail page.
That I don’t want.
Thanks for your help in advance!
Tobias says
Hello,
Please support redirect after add to cart when AJAX is used.
all suggestions are only for the non AJAX Version.
I can not have most of my shop without AJAX add products to cart. i only need a redirect for one Product.
regards Tobias
Matteo says
Hi, from woocommerce 3 your function is broken :\
Can you help me to fix it?
The woocommerce_add_to_cart_redirect hook doesn’t read the product id, i need a big help!
Thank you in advance!
Lexi says
I’m afraid I can’t find a way to do this by category, but I was able to accomplish it by product ID:
add_filter( ‘woocommerce_add_to_cart_redirect’, ‘rv_redirect_on_add_to_cart’ );
function rv_redirect_on_add_to_cart() {
//Get product ID
if ( isset( $_POST[‘add-to-cart’] ) ) {
$product_id = (int) apply_filters( ‘woocommerce_add_to_cart_product_id’, $_POST[‘add-to-cart’] );
$stand_selection_id = ‘1321’; // ‘from’ product
//Check if product ID is in the proper taxonomy and return the URL to the redirect product
if ( $stand_selection_id == $product_id )
return get_permalink( 57 ); // ‘to’ product
}
}
Stephen says
Hi, I get a 404 when using this with the latest Woocommerce. Products add to the basket in the sidebar but the page redirects to a 404.
igor says
Hey
This is what i need and i used the plugin you made.
the question is can it be redirected only after purchasing the product or subscription?
Ben Niehues IV says
I spent too much time working on a solution only to find this solution the next day! Does exactly what it is supposed to do. Thank you sir!
Ren says
Fantastic! I’m very glad to hear it helped. Thanks for sharing!
John McCarthy says
In reply to my last comment, sorry!
I wasn’t cycling through categories with a `foreach` as well as something issues with my workings.
Please delete my comment if you feel it’s unnecessary. I’ll check back, if it’s published I’ll add my working code .
Thanks
John McCarthy says
Hi Ren,
I’m trying to do something similar to this. The difference is that I’d like to grab the current product category and append that to the returned url in the `woocommerce_add_to_cart_redirect` filter.
So far I can’t pull in the category. I’m missing something about where the filter fires?
My code so far
`global
`$terms = get_the_terms( $post->ID, ‘product_cat’ );`
`$product_cat = $terms->name’ which would get me the category in my thinking anyway.
I’ve looked into using your plugin, but for this I only need to go back to the parent category. This could be something to add to the plugin as a checkbox on the product page?
Gerald Mack says
external custom redirect does not work when you are not on the product page.
Jack says
Hi,
This looks like what I think I need… But I am not 100% sure…
I want to offer product A to customers, but I also want to upsell Product B which is on a different URL to Product A.
Both product A & B have their own sales pages so both are not being sold via my website where the products are then used by students (video courses).
Ideally when a customer adds Product A to their cart via the landing page, I want the URL to direct them to Product B’s landing page for an up sell. And hopefully enticing them to add Product B to their cart.
How do I do this?
Also, is there a way to add two product codes to the URL in example here –
http://yourproducturl.com/checkout/?add-to-cart=ID-1 + ID2 – (product A) + ID2 (product B)
Thanks
Jack
Ren says
Hi Jack,
To address the first part of your question, you can use this code or the plugin to redirect to any URL, including external links. While I don’t know the specifics of your sales funnel, it’s generally not recommended to redirect to an external URL to upsell Product B because it takes the user away from the site on which they’re supposed to buy Product A. Trying to sell products located on different sites to the same user is more difficult for this reason; it makes it easy for the user to get lost in the sales process and give up on Product A.
For adding multiple products to the cart using a URL, I don’t think this is possible with default WooCommerce capabilities. I looked at the
add_to_cart_action()
method, located in the WC_Form_Handler class, and it isn’t coded to handle multiple product IDs. Therefore, you’d need to code a custom handler to add multiple products to the cart via URL.Hopefully this helps!
adam says
Thank you so much for this plugin! I have been searching for hours for a solution to this problem as I am still just beginning to learn php so it’s not easy for me.
This is great. Cheers
Daniel says
I noticed that with the latest update to woocommerce 2.4 your plugin gets a notice.
Notice: Undefined index: add-to-cart in /wp-content/plugins/woocommerce-add-to-cart-custom-redirect/woocommerce-custom-redirect.php on line 85
Just a FYI! 🙂
Ren says
Hi, Daniel. Thanks for the note. I’ll take a look into it.
VR51 says
Thank you so much for the plugin. It works great for directing customers through the shopping cart.
All I need to do now is figure out how to give the cart a carousel effect so the added product slides out of view and the next product slides into view. Might settle for improved Woo notices or a loading icon until I can work on the carousel effect.
Thanks million!
rodney says
Hi, awesome idea for a plugin. I have one issue that maybe i need to add the code to get it to work. I’m trying to connect my site to a different checkout (cratejoy.com). My products have variables. So how can i set the redirect URL based on variables.
Basically i’ve created the products on Cratejoy.com so i have a link to where the user should go when they click Add to Cart on my Woocommerce site. Example, I’m going to use my variations to not get confused but how do I specify the URL if, You chose Movieblox Kidz, then on the product page you chose the variations “Boy” & “DVD”. Then the Add to Cart button pops up and i need to redirect you once you click on that button to the product i created in Cratejoy that is “MovieBlox Boyz DVD”.
my site is movieblox.com and i have it under maintenance but let me know if you need anything from me to help me out. thank you so much this was exactly what i was looking for I just don’t know php.
Ren says
I have a plugin that adds a per-product redirect to any URL. It might be what you’re looking for.
https://wordpress.org/plugins/woocommerce-add-to-cart-custom-redirect/
rodney says
Yeah i downloaded that and it seems awesome but that changes it per product. But not per variation of that product. So in my example, “DVD” is one choice and “Blu-Ray” is a different choice so i would need two different URLs for that one product
rodney says
The only thing i thought of but dont know exactly how to customize your code to do this was to add a category for DVD and one for Blu-Ray and so on for all my variables.
Variations are Boy, Girl, DVD, Blu-Ray and the different combinations would have to be addressed in the code to.
Then use your code to trigger the different URL by the combination of variations. That way I’ll be able to send the customer that chose a Girl Blu-Ray to one link and the one that chose a Girl DVD to another without having to create new products or extra steps to checkout. Hope that makes sense lol
Ren says
Okay, so you need conditional redirects based on which variation of a product is added to the cart. Yes, this feature is not yet supported in my plugin. It’s a more advanced requirement so you’ll likely need to hire a developer to create the functionality.
rodney says
Yeah i thought so, I came up with what i’m hoping will be a workaround without doing the code. Although, without you having to type it up for me as im sure you’re a busy guy, couldn’t i alter your code to do that. You’re already finding the category that it belongs to or the tag used with it. So wouldn’t i be able to do this conditional redirect by using your code and categorizing my products. Just a thought. Thanks again
Ren says
Yeah, if you’re comfortable with code, my plugin would be a good thing to work off of. What you need to do, though, is, after a product is added to the cart, check to see if the variation ID exists in the $_POST data and, if it does, use it to set your conditional redirects. All of this would be done in the
woocommerce_add_to_cart_redirect
filter. That is basically the entire gist of it. Hopefully that helps!Sarah says
Hi, can you suggest any solutions for this?
*Editor’s note: Broken link removed.
Sarah says
Thanks for the code, I added the tag to 8 products, but the filter is just returning 2, any idea why the tags filter works on some products but not others??
Ren says
As written, the code works for product categories, not tags. You can change
product_cat
toproduct_tag
if you need to use tags instead.Sarah says
Hi Ren,
thanks for the reply, but I am using product-tag – here’s the code I’m using…
add_filter (add_to_cart_redirect, redirect_elsewhere);
function redirect_elsewhere() {
global $woocommerce;
//Get product ID
$product_id = (int)
apply_filters(‘woocommerce_add_to_cart_product_id’, $_POST[‘product_id’]);
if( has_term( ‘waiver’, ‘product_tag’, $product_id ) ){
//Set redirect URL
$redirect_url = ‘ https://mySitePageContaining-waiver/‘;
//Return the new URL
return $redirect_url;
};
}
Sarah says
sorry, just to add, I tried creating a category and using product_cat instead but for some reason that returned nothing.
Ren says
Since it had been a while that I last looked over the code, I reworked it to make it a bit cleaner and fix a couple things. Somehow, I forgot to include quotes around the filter and function names in add_filter(). Try the updated code and let me know if that works for you.
Sarah says
hi thanks again for the swift reply, it made no difference, but actually I think the problem is with my theme, not the code!
Sarah says
bit confused here, the default theme just seems to be returning everything! Any more ideas?
Sarah says
I’ve just realised that the filter is working on ‘variable’ products, and not ‘simple’ products. Why would this happen?
Ren says
That’s strange because there isn’t anything in the code restricting the functionality to any specific type of product, especially variable. I just checked the snippet again and it still works for me with both product categories and tags. Without seeing what’s going on, it’s hard for me to give further advice. If you’d like, I’m available to take a look at your admin to see if I can figure it out.
Sarah says
seems to be working on a shop page but not a wordpress page, that must be why it worked on variable products, because you had to click through to the shop page before you add to cart. I think my theme is not ideal for woocommerce.
Rik says
Hi Ren, Thank-you for the fast reply. We have inserted the code into our functions.php file and updated the specific products with the “rhino” tag but the function does not appear to be re-directing to the URL specified when adding a product with the matching tag to the shopping cart.
See here: http://www.cadjewelryschool.com/product-tag/rhino/
Any ideas?
Ren says
Hi, Rik. I’ll have to play around with this when I get a chance to see if I can figure it out for you.
Rik says
Hi Ren,
We still cannot get this to work on our site. Our WP theme is DIVI, and we use woocommerce. We’d be happy to pay you for your time if you think you can resolve this for our site. Feel free to pm me. Thanks
Ren says
Hi, Rik. I sent you an email.
Rik says
Hi Ren,
Firstly, thanks for posting the code above.
We are interested in doing a similar thing except we have several product ID’s where we would want to redirect to a specific url on add to cart.
eg:
product category = Software
and the products have the term: Rhino
Or could we input a string of product ID’s? eg: 1234, 4321, 5678, 8765
Also we would like to redirect to a specific url : http://www.cadjewelryschool.com/rhinogold-4-0-essentials-training-online/
Any help would be much appreciated.
Thanks, Rik
Ren says
Hi, Rik. If the products all have the tag “Rhino”, you can switch the taxonomy name part of the has_term() function to ‘product_tag’ rather than ‘product_cat’. This will make the conditional check to see if the product has the tag of Rhino and redirect if true. You can set the redirect URL to whatever you want. So your final code may look something like this: