Refreshing cart after changing quantity of a product via ajax request

alextorresalextorres Member
in Help edited April 3
Hello,

I'm trying to do what the title says, this is my code:

success: function(data) {
var removedBooks = false,
quantityAdjusted = false,
$errorElement = $('div[data-fc-error-notifier=""]'),
error;
$.each(data, function(k, v) {
var id = $('.fc-cart__item[data-item-code="' + k + '"]').data('fc-item-id');

var $quantitySelector = $('input[data-fc-item-id="' + id + '"]'),
$quantity = $quantitySelector.val();

if( $quantity > v && v > 0 ) {
$quantitySelector.val(v);
#REFRESH CART HERE#
quantityAdjusted = true;
} else if( v === 0 ) {
$('a[data-fc-item-id="' + id + '"]').click();
removedBooks = true;
}
});
}
Is it possible to refresh the cart where my #REFRESH CART HERE# comment is?

Thank you
Comments
  • fc_marijafc_marija FoxyCart Team
    Hi @alextorres -

    Generally, you can use FC.cart.render() for JavaScript, but if you need additional help with that, could you whisper the store (assuming you have this code applied and you're using the custom footer) so we can see it in context?

    Here's our article on using JavaScript: https://wiki.foxycart.com/v/2.0/javascript
  • fc_adamfc_adam FoxyCart Team
    @alextorres,

    Quick follow-up, as @fc_marija noted, you can use FC.cart.render() to trigger the whole cart to be re-rendered. That won't correctly update the prices though with how you're currently approaching the quantity change.

    Instead, you could simply trigger the change event to happen on the quantity input after changing the value - like this: $quantitySelector.val(v).trigger("change");. That will cause our native javascript to detect the new value and update the totals.
  • this $quantitySelector.val(v).trigger("change"); worked perfectly, thanks @fc_adam
Sign In or Register to comment.