2
votes

Im looking for a solution to add a product variation via Ajax.

As i found out all the WooCommerce basic functions allow adding a product to the cart only if it is not a variation item.

im using <?php echo woocommerce_template_loop_add_to_cart() ?> to display the add to cart button but this button is a regular submit button.

how can i make a variable item use the Ajax add to cart ?

1
For future searchers, please see this answerhelgatheviking

1 Answers

0
votes

I created and AJAX add to cart for variable products like this:

Sorry about the delay, here is the expanded answer:

I included a new js file called added-to-cart.js and the file contains the following code. There is some extra code for handling a popup and also increasing the cart counter which you might want to remove.

/* Begin */

jQuery(function($) {

/* event for closing the popup */
$("div.close").hover(
                function() {
                    $('span.ecs_tooltip').show();
                },
                function () {
                    $('span.ecs_tooltip').hide();
                }
            );

$("div.close").click(function() {
    disablePopup();  // function close pop up
})

$("a.close").click(function() {
    disablePopup();  // function close pop up
});

$(this).keyup(function(event) {
    if (event.which == 27) { // 27 is 'Ecs' in the keyboard
        disablePopup();  // function close pop up
    }
});

    $("div#backgroundPopup").click(function() {
    disablePopup();  // function close pop up
});

$('a.livebox').click(function() {
    //alert('Hello World!');
return true;
});

setAjaxButtons(); // add to cart button ajax

function loading() {
    $("div.loader").show();
}
function closeloading() {
    $("div.loader").fadeOut('normal');
}

// AJAX buy button for variable products
function setAjaxButtons() {
   $('.single_add_to_cart_button').click(function(e) {
      var target = e.target;
      loading(); // loading
      e.preventDefault();
      var dataset = $(e.target).closest('form');
      var product_id = $(e.target).closest('form').find("input[name*='product_id']");
      values = dataset.serialize();

        $.ajax({
          type: 'POST',
          url: '?add-to-cart='+product_id.val(),
          data: values,
          success: function(response, textStatus, jqXHR){
                loadPopup(target); // function show popup
                updateCartCounter();
            },
        });    
      return false;
   });    

}

function updateCartCounter() {
    var counter = $('.widget_shopping_cart_content').text().replace(/\s/g, '');
    if (counter == '') {
        $('.widget_shopping_cart_content').text("1");
    }
    else {
        $('.widget_shopping_cart_content').text(++counter);
    }
}

var popupStatus = 0; // set value

function loadPopup(target) {

    var currentPopup = $(target).parents(".single_variation_wrap").find("#toPopup");
    var currentBgPopup = $(target).parents(".single_variation_wrap").find("#backgroundPopup");

    if(popupStatus == 0) { // if value is 0, show popup
        closeloading(); // fadeout loading
        currentPopup.fadeIn(0500); // fadein popup div
        currentBgPopup.css("opacity", "0.7"); // css opacity, supports IE7, IE8
        currentBgPopup.fadeIn(0001);
        popupStatus = 1; // and set value to 1
    }
}

function disablePopup() {
    if(popupStatus == 1) { // if value is 1, close popup
        $(".single_variation_wrap > div:nth-child(2)").fadeOut("normal");
        $(".single_variation_wrap > div:nth-child(4)").fadeOut("normal");
        popupStatus = 0;  // and set value to 0
    }
}
}); // jQuery End