aelle robbiani Posted November 2, 2017 Share Posted November 2, 2017 (edited) Hi to all, at first sorry for my english. I have a custom blockcart in a site with Prestashop 1.7.2.4 The js function for update quantity in blockcart is: function changeQuantity(minimal_quantity, operator, id, id_attribute, quantincart){ if (operator == 1){ //subtract $.ajax({ type: 'POST', headers: { "cache-control": "no-cache" }, async: true, cache: false, data: 'controller=cart&add=1&ajax=true&qty=1&op=down&id_product='+ id +'&ipa='+ id_attribute , success: function(response){ console.log(response); prestashop.emit('updateCart', { reason: response }); }, error: function (request, status, error) { console.log(error); } }); }else if (operator == 2){ //sum $.ajax({ type: 'POST', headers: { "cache-control": "no-cache" }, async: true, cache: false, data: 'controller=cart&add=1&ajax=true&qty=1&id_product='+ id +'&ipa='+ id_attribute , success: function(response){ console.log(response); prestashop.emit('updateCart', { reason: response }); }, error: function (request, status, error) { console.log(error); } }); } } Ok, if the user is guest no problem, update work great and blockcart refresh also, but if user is logged in don't work. Have idea for it? I don't kown why console.log is empty after ajax call (response function) also, whether setting dataType (json, html ecc) or not set it. Naturally in debug mode i haven't errors. Please give me a tip Edited November 2, 2017 by aelle robbiani (see edit history) Link to comment Share on other sites More sharing options...
aelle robbiani Posted November 2, 2017 Author Share Posted November 2, 2017 solved myself this is my solution: function changeQuantity(minimal_quantity, operator, id, id_attribute, quantincart){ var token = prestashop.static_token; //important for logged user if (operator == 1){ //subtract var actionURL = '/index.php'; var query= 'controller=cart&add=1&action=update&ajax=true&qty=1&op=down&token='+token+'&id_product='+ id +'&ipa='+ id_attribute; $.post(actionURL, query, null, 'json').then(function (resp) { prestashop.emit('updateCart', { reason: resp }); console.log(resp); }).fail(function (resp) { prestashop.emit('handleError', { eventType: 'addProductToCart', resp: resp }); }); }else if (operator == 2){ //sum var actionURL = '/index.php'; var query= 'controller=cart&add=1&action=update&ajax=true&qty=1&op=up&token='+token+'&id_product='+ id +'&ipa='+ id_attribute; $.post(actionURL, query, null, 'json').then(function (resp) { prestashop.emit('updateCart', { reason: resp }); console.log(resp); }).fail(function (resp) { prestashop.emit('handleError', { eventType: 'addProductToCart', resp: resp }); }); }else if (operator == 3){ //delete var actionURL = '/index.php'; var query= 'controller=cart&add=1&action=update&ajax=true&qty='+quantincart+'&op=down&token='+token+'&id_product='+ id +'&ipa='+ id_attribute; $.post(actionURL, query, null, 'json').then(function (resp) { prestashop.emit('updateCart', { reason: resp }); console.log(resp); }).fail(function (resp) { prestashop.emit('handleError', { eventType: 'addProductToCart', resp: resp }); }); } } Thanks to all, bye Link to comment Share on other sites More sharing options...
Zions1 Posted November 5, 2019 Share Posted November 5, 2019 Hi, I'm trying to apply your solution inside the loop. So, when you click a button you are able to add multiple products with multiple quantities. When, I'm in incognito mode and I'm adding multiple product (let's say Product A with quantity 2 and Product B with quantity 3), in cart I can see sometimes one of them and sometimes both added to the cart. However, when I've already had some products in cart, everything is working fine, and always I can see both products with proper quantities. So, the situation when the above solution is not working is during first attempt. So it's really hard to debug and find an issue. Can anyone help with this? Link to comment Share on other sites More sharing options...
ruthcast Posted November 21, 2019 Share Posted November 21, 2019 On 11/5/2019 at 4:04 PM, Zions1 said: Hi, I'm trying to apply your solution inside the loop. So, when you click a button you are able to add multiple products with multiple quantities. When, I'm in incognito mode and I'm adding multiple product (let's say Product A with quantity 2 and Product B with quantity 3), in cart I can see sometimes one of them and sometimes both added to the cart. However, when I've already had some products in cart, everything is working fine, and always I can see both products with proper quantities. So, the situation when the above solution is not working is during first attempt. So it's really hard to debug and find an issue. Can anyone help with this? Hi ZionS1 I am trying to send several products to the cart from a single button to add to the cart, and I want to adapt the aelle solution to do so. Can you tell me how you did it? Link to comment Share on other sites More sharing options...
Zions1 Posted November 22, 2019 Share Posted November 22, 2019 Yes, have a look below. I'm receiving product id and calculating the quantity of the product based on data form JS. $('.add-all-selected-products-button').click(function(e){ const elements = document.getElementsByName("qtyForm"); const token = prestashop.static_token; const url = prestashop.urls.pages.cart; for(let element of elements) { const idProduct = element.id.replace("quantityForm_to_cart_", ""); const value = $('#quantityForm_to_cart_'+idProduct+'').val(); const step = element.step; const qty = value / step; if (qty !== 0) { const query = 'controller=cart&add=1&action=update&ajax=true&token=' + token + '&id_product=' + idProduct + '&id_customization=0&qty=' + qty; $.post(url, query, null, 'json').then((resp) => { prestashop.emit('updateCart', { reason: {}, resp: resp }); console.log(resp); }).fail((resp) => { prestashop.emit('handleError', {eventType: 'addProductToCart', resp: resp}); }); } } }) 2 Link to comment Share on other sites More sharing options...
ruthcast Posted November 22, 2019 Share Posted November 22, 2019 thank you very much! I solved it with your help. Link to comment Share on other sites More sharing options...
ruthcast Posted December 4, 2019 Share Posted December 4, 2019 On 11/22/2019 at 9:30 AM, Zions1 said: Yes, have a look below. I'm receiving product id and calculating the quantity of the product based on data form JS. $('.add-all-selected-products-button').click(function(e){ const elements = document.getElementsByName("qtyForm"); const token = prestashop.static_token; const url = prestashop.urls.pages.cart; for(let element of elements) { const idProduct = element.id.replace("quantityForm_to_cart_", ""); const value = $('#quantityForm_to_cart_'+idProduct+'').val(); const step = element.step; const qty = value / step; if (qty !== 0) { const query = 'controller=cart&add=1&action=update&ajax=true&token=' + token + '&id_product=' + idProduct + '&id_customization=0&qty=' + qty; $.post(url, query, null, 'json').then((resp) => { prestashop.emit('updateCart', { reason: {}, resp: resp }); console.log(resp); }).fail((resp) => { prestashop.emit('handleError', {eventType: 'addProductToCart', resp: resp}); }); } } }) Hi @Zions1 Are you here yet? I managed to add several products to the cart with your help, but now the client has asked me to show the modal window that appears when products are added to the cart individually. Do you have any idea how to do it?. Thanks in advance. Link to comment Share on other sites More sharing options...
Darius1990 Posted March 30, 2020 Share Posted March 30, 2020 for me helped just changing "static_token" to "prestashop.static_token" Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now