WooCommerce: Remove “add-to-cart=123” URL Parameter After Add to Cart

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.

I used the “add-to-cart” URL parameter to add a product to the cart but now I want to remove it once I’ve added it. The snippet below should help with that

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!

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

6 thoughts on “WooCommerce: Remove “add-to-cart=123” URL Parameter After Add to Cart

  1. Heya, I just tried this and it still doesn’t remove the ‘add-to-cart’ parameter from the url. Appreciate any help. Thanks.

    1. Weird, works perfectly on my test website. Maybe try temporarily disabling all plugins except Woo and see if it works

  2. 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!

    1. 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?

      1. This is not working when hitting a URL in direct mode. This is because there’s no referer there.

        This works.

        add_filter( 'woocommerce_add_to_cart_redirect', function ( $url ) {
        	return add_query_arg( 'add-to-cart', null );
        } );
        
        1. Ah, well, that’s awesome! Thank you

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *