Yesterday, a customer sent me a message wanting to know how she could conditionally modify the redirects she set for her products using Custom Redirects for WooCommerce. I thought this would be a good topic for a quick tutorial, so I put together a simple code snippet for accomplishing just that.
In this code, we’re first checking to make sure we’re only applying it to add-to-cart-redirects (not after-purchase redirects). Then, we’re retrieving a store’s cart URL, and referring URL (URL of customer’s previous location). We check to see if these URLs are the same, meaning if the last page the user was on was the cart page. If a product is added from the cart, we want to redirect the user back to the cart (keeping them in the cart), rather than applying the redirect that was set for the product.
You can post this code to your theme’s
functions.php file, or to a function plugin. Just make sure to pay attention to the
<?php at the top.
That’s all there is to this one. Feel free to post any questions in the comments!