title: Add to cart and redirect to checkout for default variable products in WooCommerce
body: <p>I am trying the following code by LoicTheAztec - <a href="https://stackoverflow.com/questions/54865565/add-to-cart-and-redirect-to-checkout-for-variable-products-in-woocommerce">Add to cart and redirect to checkout for variable products in WooCommerce</a> </p>
  
  <p>Now, the problem is whenever I am clicking on the Buy Now button with the default variations set in the Product editor, the code is not working.</p>
  
  <p>The Buy Now URL in this case becomes …/?addtocart=0&amp;quantity=n</p>
  
  <p>The Theme I am using - Basel</p>
  
  <p>Any solutions?</p>
  
  <p>Tried the code by LoicTheAztec. </p>
  
  <pre><code>function add_custom_addtocart_and_checkout() {
      global $product;
  
      $addtocart_url = wc_get_checkout_url().'?add-to-cart='.$product-&gt;get_id();
      $button_class  = 'single_add_to_cart_button button alt custom-checkout-btn';
      $button_text   = __("Buy &amp; Checkout", "woocommerce");
  
      if( $product-&gt;is_type( 'simple' )) :
      ?&gt;
      &lt;script&gt;
      jQuery(function($) {
          var url    = '&lt;?php echo $addtocart_url; ?&gt;',
              qty    = 'input.qty',
              button = 'a.custom-checkout-btn';
  
          // On input/change quantity event
          $(qty).on('input change', function() {
              $(button).attr('href', url + '&amp;quantity=' + $(this).val() );
          });
      });
      &lt;/script&gt;
      &lt;?php
  
      elseif( $product-&gt;is_type( 'variable' ) ) : 
  
      $addtocart_url = wc_get_checkout_url().'?add-to-cart=';
      ?&gt;
      &lt;script&gt;
      jQuery(function($) {
          var url    = '&lt;?php echo $addtocart_url; ?&gt;',
              vid    = 'input[name="variation_id"]',
              pid    = 'input[name="product_id"]',
              qty    = 'input.qty',
              button = 'a.custom-checkout-btn';
  
          // Once DOM is loaded
          setTimeout( function(){
              if( $(vid).val() != '' ){
                  $(button).attr('href', url + $(vid).val() + '&amp;quantity=' + $(qty).val() );
              }
          }, 300 );
  
          // On input/change quantity event
          $(qty).on('input change', function() {
              if( $(vid).val() != '' ){
                  $(button).attr('href', url + $(vid).val() + '&amp;quantity=' + $(this).val() );
              }
          });
  
          // On select attribute field change event
          $('.variations_form').on('change blur', 'table.variations select', function() {
              if( $(vid).val() != '' ){
                  $(button).attr('href', url + $(vid).val() + '&amp;quantity=' + $(qty).val() );
              }
          });
      });
      &lt;/script&gt;
      &lt;?php
      endif;
      echo '&lt;a href="'.$addtocart_url.'" class="'.$button_class.'"&gt;'.$button_text.'&lt;/a&gt;';
  } ```
  
  I expect this code to work when the user does not change or update any variations on the product page. That is when the user clicks on the Buy Now button with the default variations.
  </code></pre>