title: WordPress WooCommerce - Add a variable product to cart using the WC_Cart class
body: <p>I am trying to add a variable product to the cart of the WordPress plugin, WooCommerce.</p>
  
  <p>So far I have been able to add single/simple products with:</p>
  
  <pre><code>$woocommerce-&gt;cart-&gt;add_to_cart( [product_id], [quantity] );
  </code></pre>
  
  <p>However, looking in the WC_Class at the functions signature:</p>
  
  <pre><code>function add_to_cart( $product_id, $quantity = 1, $variation_id = '', $variation = '', $cart_item_data = array() ) {
  </code></pre>
  
  <p>we can clearly see the function permits inputs of variation_id.</p>
  
  <p>I have tried every combination of nulls and integers along the lines of:</p>
  
  <pre><code>$woocommerce-&gt;cart-&gt;add_to_cart( 24, 1, 28, null, null ); 
  </code></pre>
  
  <p>and so on to no avail.</p>
  
  <p>Ive also tried my own hacky approach that tries to recreate the post events performed by WooCommerce's own product page, again with no luck.</p>
  
  <pre><code>&lt;a id="buy_v" href="#"&gt;Buy Variable Product !&lt;/a&gt;    
  &lt;script&gt;    
     $('#buy_v').click(function(e) {
        e.preventDefault();
        addToCartV(24,26,'Red',1);
        return false;
     });    
     function addToCartV(p_id, v_id, c, q) {    
      $.ajax({
        type: 'POST',
        url: '/wp/?product=tee1&amp;add-to-cart=variation&amp;product_id='+p_id,
        data: { 'attribute_colour': c,
                'variation_id':  v_id,
                'quantity':  q,
                'product_id':  p_id},
        success: function(response, textStatus, jqXHR){
              // log a message to the console
              console.log("It worked!");
          }/*,
        dataType: 'JSON'*/
      });    
     }   
  &lt;/script&gt;
  </code></pre>
  
  <p>Could anyone suggest where I might be going wrong? Thanks.</p>