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!
how can we make the redirect to happen faster? i put redirect to custom page but when the visitor hit the add to cart button it takes a while
It sounds like the redirect location may not be the quickest in terms of page speed or load times. If the redirect is still to your website, you can probably improve load times with some optimization work. If the redirect is to some external website, the page speed would be out of your control.