Adding custom info messages when triggering a custom cart event

Hey guys,

I have a custom event to save a cart item for later, similar to how Amazon does it. When the event is triggered, I post some data to an endpoint, then remove the cart item. What I'm struggling with is adding a notification that it was successful. I see that we have a method for FC.util.addError, but not for addInfo. I added a custom method for that, but when I add an item to FC.json.messages.info and then remove a cart item, the message is cleared. What's the right way to do this?

Thanks!

-Jacob


FC.client.wrap("save-item", function (params) {
var cart = FC.cart;
event.preventDefault();

$.ajax({
type: "post",
url: MY_AJAX_URL,
dataType: "json",
data: {
action: 'save_item_for_later',
product: params.product,
}
}).always(function (data) {
cart.removeItem({
id: params.id
});
*** FC.util.addInfo('save-for-later', data.msg); ***
});

});



jQuery('[data-fc-id="item-save-link"]').on('click', function() {
var $this = jQuery(this),
item_id = $this.data("fc-item-id"),
product = FC.json.items.find(function(item) {
return item.id == item_id
});

FC.client.event("save-item").trigger({
id : $this.data("fc-item-id"),
product : product
});
});
Tagged:
Comments
  • fc_adamfc_adam FoxyCart Team
    @jacobdubail,

    Without seeing it in action, I think what you're seeing is your custom message is being blown away when the removeItem function call returns and updates the JSON from serverside. You could switch out the removeItem call for a FC.client.request(...).done(function() {...}); call - setting the add to cart URL to remove the product in the request, and then adding your custom message in the done callback function. That should let your custom message persist until the next call updates the JSON.
  • That worked great. If you're interested in the code, let me know.
Sign In or Register to comment.