title: WooCommerce AJAX Add to Cart for Variable products, still redirecting to product page
body: <p>Previously, after adding a product to cart from archive page, the page stays on the same page (not redirecting), but this only works for a simple product. What I'm trying to do is enable this feature for the variable product also (display product options at archive page without having to go to product page first).</p>
  
  <p>Based on this link:</p>
  
  <p><a href="https://gist.github.com/rynaldos/ae17fcdb74bd304cfb64dc8692208b2d" rel="nofollow noreferrer">[WooCommerce 3.0+] Ajax add to cart on variable products archive page</a></p>
  
  <p>Using this code:</p>
  
  <pre><code>if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
  
     if ( ! function_exists( 'woocommerce_template_loop_add_to_cart' ) ) {
  
          function woocommerce_template_loop_add_to_cart() {
               global $product;
  
               if ($product-&gt;get_type() == "variable" ) {
                   woocommerce_variable_add_to_cart();
               }
               else {
                   wc_get_template( 'loop/add-to-cart.php' );
               }
           }
      }
  
      function ajax_add_to_cart_script() {
  
        wp_enqueue_script( 'add-to-cart-variation', plugins_url() . '/woocommerce-ajax-add-to-cart-variable-products/js/add-to-cart-variation.js', array('jquery'), '', true );
        wp_localize_script( 'add-to-cart-variation', 'AddToCartAjax', array(
          'ajaxurl' =&gt; admin_url( 'admin-ajax.php' ),
  
        ));
      }
      add_action( 'wp_enqueue_scripts', 'ajax_add_to_cart_script' );
  
      add_action( 'wp_ajax_woocommerce_add_to_cart_variable_rc', 'woocommerce_add_to_cart_variable_rc_callback' );
  
      function woocommerce_add_to_cart_variable_rc_callback() {
          $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) );
          $quantity = empty( $_POST['quantity'] ) ? 1 : apply_filters( 'woocommerce_stock_amount', $_POST['quantity'] );
          $variation_id = $_POST['variation_id'];
          $variation  = $_POST['variation'];
          $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
  
          if ( $passed_validation &amp;&amp; WC()-&gt;cart-&gt;add_to_cart( $product_id, $quantity, $variation_id, $variation  ) ) {
              do_action( 'woocommerce_ajax_added_to_cart', $product_id );
              if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
              wc_add_to_cart_message( $product_id );
          }
  
          $this-&gt;get_refreshed_fragments();
          } else {
              $this-&gt;json_headers();
  
          $data = array(
              'error' =&gt; true,
              'product_url' =&gt; apply_filters( 'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id )
              );
          echo json_encode( $data );
          }
          die();
      }  
  }
  </code></pre>
  
  <p>I'm half-way there, but the biggest problem now is after adding a variable product AND/OR a simple product to cart, I'm being redirected to the product page (the product is added to cart just fine).</p>
  
  <p>I'm not sure if this is the problem, but I found the difference in HTML before and after adding the code above is this:</p>
  
  <p>Before:</p>
  
  <pre><code>&lt;a href="/product-category/meats/?instock_products=in&amp;#038;v=b718adec73e0&amp;#038;add-to-cart=861" data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart" data-product_id="861" data-product_sku="" aria-label="Add &amp;ldquo;Whole Pork Fillet (400G Up) Chilled&amp;rdquo; to your cart" rel="nofollow"&gt;
  </code></pre>
  
  <p>After:</p>
  
  <pre><code>&lt;a href="/product-category/meats/?instock_products=in&amp;#038;v=b718adec73e0&amp;#038;add-to-cart=861" data-quantity="1" class="button" &gt;
  </code></pre>
  
  <p>Not sure though if that's the main cause. Any help is appreciated.</p>
  
  <p>Thanks.</p>