Stop Add to Cart Redirection After an Error in WooCommerce

Quick Summary
- Hook __return_false to woocommerce_cart_redirect_after_error to keep shoppers on the current page when an Ajax add to cart fails.
- Only Ajax add-to-cart buttons on shop, category and custom loop pages are affected; the single product page form stays unchanged.
- Without the redirect, a stock error message isn't shown on the current page and may only appear on the next page the shopper opens.
On WooCommerce shop and category pages, the Add to cart buttons can add products with Ajax. When that fails, for example because there isn’t enough stock, WooCommerce’s cart redirect after an error sends the shopper to the product page. The issue looks like this: the page suddenly redirects to the product page. At the top, a message such as “…has been added to your cart” appears.
This one-line filter stops that redirect. Add it to your child theme’s functions.php or a code snippets plugin.
// Stop redirecting after stock error
add_filter( 'woocommerce_cart_redirect_after_error', '__return_false' );How the WooCommerce cart redirect after an error works
When an Ajax add to cart fails, WooCommerce passes the product’s URL through the woocommerce_cart_redirect_after_error filter. It then sends the URL back to its add-to-cart script, which opens that page. __return_false is a WordPress helper that simply returns false, so no URL is sent and the shopper stays on the current page. Filters always use the value their callback returns, as explained in WordPress action vs filter hooks.
The filter only affects Ajax add-to-cart buttons, such as the ones on shop and category pages or in a custom product loop that prints WooCommerce’s Ajax add to cart button. The regular add-to-cart form on a single product page isn’t changed.
Keep in mind that the redirect is how shoppers normally see the error. Without it, the current page doesn’t show the error message. WooCommerce keeps notices until a page displays them, so the shopper may only see it on the next page they open.
If a store needs its add-to-cart flow reworked rather than patched, that’s part of my WooCommerce development services.
Related articles
- printing WooCommerce notices on a custom product template: where a held-back cart message can show up again
