Answer the question
In order to leave comments, you need to log in
Why are not all items added to the cart?
$(".tocart").on( "click", function() {
var cart_date = $('.sbor .tocart').attr("data-cart").split(';');
cart_date.forEach(function(item, i, cart_date) {
product = item.split(',');
varid=product[2];
qty=product[1];
var data = {
action: 'my_action',
product_id: product[0],
qty : qty,
variable_id: varid ,
};
jQuery.post( myajax.url, data, function(response) {
});
});
});
add_action('wp_ajax_my_action', 'my_action_callback');
add_action('wp_ajax_nopriv_my_action', 'my_action_callback');
function my_action_callback() {
$product_id= intval( $_POST['product_id'] );
$qty= intval( $_POST['qty'] );
$variable_id= intval( $_POST['variable_id'] );
global $woocommerce;
$woocommerce->cart->add_to_cart($product_id, $qty, $variable_id);
wp_die();
}
Answer the question
In order to leave comments, you need to log in
Woocommerce makes checks before adding to the cart and in case of an error it fills in the notification. Then he displays these messages to the user.
Your code is ignoring this.
Check the messages, maybe they have the answer.
Look at the native add-to-cart code.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question