10

I am trying to add a variable product to the cart of the WordPress plugin, WooCommerce.

So far I have been able to add single/simple products with:

$woocommerce->cart->add_to_cart( [product_id], [quantity] );

However, looking in the WC_Class at the functions signature:

function add_to_cart( $product_id, $quantity = 1, $variation_id = '', $variation = '', $cart_item_data = array() ) {

we can clearly see the function permits inputs of variation_id.

I have tried every combination of nulls and integers along the lines of:

$woocommerce->cart->add_to_cart( 24, 1, 28, null, null ); 

and so on to no avail.

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.

<a id="buy_v" href="#">Buy Variable Product !</a>    
<script>    
   $('#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&add-to-cart=variation&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'*/
    });    
   }   
</script>

Could anyone suggest where I might be going wrong? Thanks.

1
  • Hi. I'm trying to build an ajax variation add-to-cart like above. I checked woocommerce code in and out, but I'd like to better understand the url var structure/possibilities/options. I mean: ?product=tee1&add-to-cart=variation&product_id > do you maybe have a 'map' of all the vars and value types I can pass there? Commented Dec 11, 2013 at 19:29

2 Answers 2

14

Both the above example actually work fine, they just don't display correctly in WooCommerce's own cart.

To make them display correctly, pass in an array for the forth parameter which seems to represent the variation in WooCommerce's own cart:

$arr = array();
$arr['Color'] = 'Green';
$woocommerce->cart->add_to_cart( 24, 1, 28, $arr, null ); 
Sign up to request clarification or add additional context in comments.

2 Comments

Just to add the $arr[] should have the fields that the page provides eg: size: $arr["atribute_pa_size"] = "1kg" in a kvp (key value pair)
earlier i just used product id, quty and variation id. I added product to cart but did not showed variation in cart, checkout and order. This is the proper way.
3

For anyone else attempting something similar, here is my approach.

I created a script to be called via ajax that contains the following:

<?php
require_once("../../../wp-blog-header.php");
header("HTTP/1.1 200 OK");
global $woocommerce;

$quantity       = (isset($_REQUEST['qty'])) ? (int) $_REQUEST['qty'] : 1;
$product_id     = (int) apply_filters('woocommerce_add_to_cart_product_id', $_REQUEST['pid']);   
$vid            = (int) apply_filters('woocommerce_add_to_cart_product_id', $_REQUEST['vid']);   

if ($vid > 0) $woocommerce->cart->add_to_cart( $product_id, $quantity, $vid );         
else $woocommerce->cart->add_to_cart( $product_id, $quantity );    

This successfully adds product variations to the cart

1 Comment

normally, you should use ajaxurl JS variable to get the location to the ajax.php file and send the ajax requests to that file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.