Jump to content
aelle robbiani

[solved] updateCart custom js

Recommended Posts

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 by aelle robbiani (see edit history)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More