title: Woocommerce redirect after add-to-cart error
body: <p>I have being doing research for a few days but still cannot find an answer.</p>
  
  <p>I am using Woocommerce without the Single Product page. So I am using urls like <a href="http://domain.com/?add-to-cart=ID" rel="nofollow">http://domain.com/?add-to-cart=ID</a> to add products to cart.</p>
  
  <p>My products are sold individually which means that I can only add the product to cart once. Now when I add a product for the first time, I am redirected to the cart page which is what I wanted. However, when I add the product the second time, it is refreshing the page I am on. But I want to be redirected to the cart page and shown the error message like 'You cannot add the product twice' on the <strong>cart page</strong>.</p>
  
  <p>When I was reading the Woocommerce core source code, I found the code below in the <strong><em>add-to-cart()</em></strong> function in file <strong><em>class-wc-ajax.php</em></strong>.</p>
  
  <pre><code>// If there was an error adding to the cart, redirect to the product page to show any errors
          $data = array(
              'error'       =&gt; true,
              'product_url' =&gt; apply_filters( 'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id )
          );
  
          wp_send_json( $data );
  </code></pre>
  
  <p>So I tried to add a filter by adding the code below in my theme <strong><em>functions.php</em></strong> file.</p>
  
  <pre><code>function filter_woocommerce_cart_redirect_after_error($redirect, $product_id) {
    $redirect = esc_url( WC()-&gt;cart-&gt;get_cart_url() );
    return $redirect;
  }
  
  add_filter( 'woocommerce_cart_redirect_after_error', 'filter_woocommerce_cart_redirect_after_error', 10, 2 );
  </code></pre>
  
  <p>But nothing was changed. </p>
  
  <p>Can anyone help? Thanks!</p>