The “add-to-cart” parameter in WooCommerce is used to create a custom URL that adds a specific product to the shopping cart. The “123” value is the product ID, and you can even specify a custom quantity. By clicking a link with this parameter in the URL, the user can add the specified product (ID# 123 in this example) to their cart without having to navigate to the product page itself.
The problem is that WooCommerce itself doesn’t automatically remove the “add-to-cart” parameter after a successful add to cart, and if you reload the page you will add to cart again! So, let’s see how we can remove this parameter in case you disabled the “redirect to cart” option.
PHP Snippet: Remove “add-to-cart=123” From URL Upon WooCommerce Add to Cart
/**
* @snippet Remove "add-to-cart" From URL
* @tutorial Get CustomizeWoo.com FREE
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 8
* @community Join https://businessbloomer.com/club/
*/
add_filter( 'woocommerce_add_to_cart_redirect', 'wp_get_referer' );
wp_get_referer is a function in WordPress used to retrieve the URL of the webpage that linked to the current one. It essentially tells you where the user came from before landing on the current page. Which means, this will take the user back to the original URL before they added to cart, hence removing the “add-to-cart” URL parameter!
Heya, I just tried this and it still doesn’t remove the ‘add-to-cart’ parameter from the url. Appreciate any help. Thanks.
Weird, works perfectly on my test website. Maybe try temporarily disabling all plugins except Woo and see if it works
Hi Rodolfo, I’m trying that solution on my website, but it seems that the woocommerce_add_to_cart_redirect hook doesn’t work with variations products.
Do you know any other solution?
Thanks!
This should work for all products, no matter the type. Do you have other custom plugins or code snippets affecting the redirect or customizing the different product types maybe?
This is not working when hitting a URL in direct mode. This is because there’s no referer there.
This works.
Ah, well, that’s awesome! Thank you