Add to cart via AJAX Woocommerce without loop

Add to cart via AJAX Woocommerce without loop

To accomplish this functionality you have to make a GET call by javascript, in this call you have to pass the ID of product.

Below you can see an example for this action.

HTML:

<div class="add_to_cart"> 
<a href="#" data_id="12">Add to cart</a> 
</div>

 

Where data_id is the product id.

 

jQuery:

jQuery('.add_to_cart').on('click', function(e){

var href = window.location.href;

var product_id = jQuery(this).attr('data_id');

jQuery.get(href+'?add-to-cart='+product_id, function(){

console.log('cart updated');

});

});

 

Leave a Reply

Your email address will not be published. Required fields are marked *