Propert way to use FC.cart.renderErrors()

blaiblai Member
in Help
I am trying to show a custom error in the side cart using the following event handler.

FC.client.on('sidecart-show', function() {
FC.util.addError('my-error', 'my error msg');
FC.cart.renderErrors();
FC.cart.render();
});

The error flashes sometimes and then quickly gets hidden.

Comments
  • fc_adamfc_adam FoxyCart Team
    @blai,

    Good question - what you're seeing there, when the sidecart is shown, it fetches the JSON cart object which will blow away any custom errors you're adding before it.

    Are you wanting to add the error after a product is added to the cart? Or something else? If you could perhaps clarify what you're looking to achieve, we can provide some suggestions for how to approach it.
  • blaiblai Member
    edited May 2
    I have two kinds of products categories. One that can be shipped via ground and the other kind the must be shipped UPS Next Day Air. When the Next Day Air item is added to the cart, a webhook calculates the required shipping box based upon the item sizes found in the cart and adds the correct shipping box to the cart.

    Along with the box there is a message that needs to be displayed in the side cart that explains the shipping. This message is dynamic and is constructed and returned by the webhook.

    The code is current listening on the 'cart-submit' and 'cart-item-remove' events.
        FC.client
    .on('cart-submit', cartSubmitHandler)
    .on('cart-item-remove', cartItemRemoveHandler);
    Combining the information from the cart-submit/cart-item-remove call with FC.json.items and passing it to my webhook the code determines the number of shipping boxes required and message to show.

    The response of the webhook is used to construct a URL to add the boxes. The code uses FC.client.request to add the required shipping boxes to the cart.
    function cartSubmitHandler(params, next) {

    ....
    // webhook is called and success handled

    FC.client.request(addBoxesURL).done( function (dataJSON) {
    FC.json.items = dataJSON.items; // not sure if this is required
    FC.util.addError('my-msg',
    'the constructed message to the user from the webhook');

    FC.cart.renderErrors();
    FC.cart.renderCartItemDivs();
    next(true); // next passed in as the second arg from the event handler
    });

    ....
    }
    There appears to be a race condition somewhere. The message is displayed sometimes if the sidecart is open and closed many times.

    Please let me know if there is a better way to handle this?


  • fc_adamfc_adam FoxyCart Team
    @blai,

    Thanks for the additional details. Is the box added to account for additional weight for the next day air shipment?

    In terms of the message disappearing - could you try switching to using the .done version of both events? So it would be cart-submit.done and cart-item-remove.done. That way the JSON should have updated and your local change should then persist until the JSON is next updated.
  • blaiblai Member
    thanks for the correct events. this works much better.
Sign In or Register to comment.