I’m using add_to_cart method to add a variation to cart with Ajax, this works but I can’t find a way to add multiple variations using the same request.
If I send one_data
it works, but not with multi_data
product_id
in this code works if the product is variable, so it’s the variation id.
Code:
<code>jQuery(function($) {
$(document.body).on('click', '#button', function(e) {
//const one_data = { product_id: 592, quantity: 1 }
const multi_data = [
{ product_id: 592, quantity: 2 },
{ product_id: 593, quantity: 1 },
{ product_id: 594, quantity: 3 }
];
$.ajax({
type: 'POST',
url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ),
data: multi_data,
success: function(res) {
console.log(res);
},
});
});
});
</code>
<code>jQuery(function($) {
$(document.body).on('click', '#button', function(e) {
//const one_data = { product_id: 592, quantity: 1 }
const multi_data = [
{ product_id: 592, quantity: 2 },
{ product_id: 593, quantity: 1 },
{ product_id: 594, quantity: 3 }
];
$.ajax({
type: 'POST',
url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ),
data: multi_data,
success: function(res) {
console.log(res);
},
});
});
});
</code>
jQuery(function($) {
$(document.body).on('click', '#button', function(e) {
//const one_data = { product_id: 592, quantity: 1 }
const multi_data = [
{ product_id: 592, quantity: 2 },
{ product_id: 593, quantity: 1 },
{ product_id: 594, quantity: 3 }
];
$.ajax({
type: 'POST',
url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ),
data: multi_data,
success: function(res) {
console.log(res);
},
});
});
});
New contributor
Becker Armata is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.