title: Ajax Add to cart for simple and variable products on WooCommerce single products
body: <p>The code below is working for add to cart with WooCommerce for simple products, but not for variable products. Can someone help me out? Because this code works with the latest WC, please do not remove this question because there are lots of codes for this online that do not work at all.</p>
  <p>The error I am getting that is says in a notice that I should choose a product option, even when I chose one.</p>
  <p>JS:</p>
  <pre><code>jQuery(function($) {
          $('form.cart').on('submit', function(e) {
              e.preventDefault();
  
              var form = $(this);
              form.block({ message: null, overlayCSS: { background: '#fff', opacity: 0.6 } });
  
              var formData = new FormData(form.context);
              formData.append('add-to-cart', form.find('[name=add-to-cart]').val() );
  
      var $thisbutton = form.find('.single_add_to_cart_button'); //
  
  
              // Ajax action
              $.ajax({
                  url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'ace_add_to_cart' ),
                  data: formData,
                  type: 'POST',
                  processData: false,
                  contentType: false,
        beforeSend: function (response) {
             $thisbutton.removeClass('added').addClass('loading');
         },
                  complete: function( response ) {
                      response = response.responseJSON;
  
                      if ( ! response ) {
                          return;
                      }
  
                      if ( response.error &amp;&amp; response.product_url ) {
                          window.location = response.product_url;
                          return;
                      }
  
                      // Redirect to cart option
                      if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) {
                          window.location = wc_add_to_cart_params.cart_url;
                          return;
                      }
  
                      // Trigger event so themes can refresh other areas.
                      $( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] );
  
                      // Remove existing notices
                      $( '.woocommerce-error, .woocommerce-message, .woocommerce-info' ).remove();
  
                      // Add new notices
                      form.closest('.product').before(response.fragments.notices_html)
  
                      form.unblock();
                  }
              });
          });
      });
  </code></pre>
  <p>PHP:</p>
  <pre><code>/* WOOCOMMERCE AJAX ADD TO CART
  --------------------------------------------------- */
  function ace_ajax_add_to_cart_handler() {
    WC_Form_Handler::add_to_cart_action();
      WC_AJAX::get_refreshed_fragments();
  }
  
  /* Add fragments for notices. */
  function ace_ajax_add_to_cart_add_fragments( $fragments ) {
      $all_notices  = WC()-&gt;session-&gt;get( 'wc_notices', array() );
      $notice_types = apply_filters( 'woocommerce_notice_types', array( 'error', 'success', 'notice' ) );
  
      ob_start();
      foreach ( $notice_types as $notice_type ) {
          if ( wc_notice_count( $notice_type ) &gt; 0 ) {
              wc_get_template( &quot;notices/{$notice_type}.php&quot;, array(
                  'messages' =&gt; array_filter( $all_notices[ $notice_type ] ),
          'notices' =&gt; array_filter( $all_notices[ $notice_type ] ),
              ) );
      }
      }
  
      $fragments['notices_html'] = ob_get_clean();
    wc_clear_notices();
      return $fragments;
  }
  
  add_action( 'wc_ajax_ace_add_to_cart', 'ace_ajax_add_to_cart_handler' );
  add_action( 'wc_ajax_nopriv_ace_add_to_cart', 'ace_ajax_add_to_cart_handler' );
  remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 ); // Remove WC Core add to cart handler to prevent double-add
  add_filter( 'woocommerce_add_to_cart_fragments', 'ace_ajax_add_to_cart_add_fragments' );
  </code></pre>