addError, addWarning, rendering

Hey guys,

I seem to always have trouble displaying custom errors/warnings on different FC.client events:

FC.client.on('cart-coupon-add.done', function() {
var qty = 0;

if ( ! jQuery.isEmptyObject(FC.json.items) ) {
jQuery.each(FC.json.items, function(i, item) {
if ( item.category == "DEFAULT" ) {
qty += parseInt(item.quantity);
}
});
}

if ( qty >= 5 ) {
FC.util.addError('coupon', 'You already reached the 20% discount limit.');
FC.cart.renderCouponEntry();
FC.cart.render();
}

});

Which render call should I be making? Or is that the wrong way to go about it? Adding a bit of documentation to the wiki would be nice.

Thanks as always!

-Jacob
Comments
  • fc_adamfc_adam FoxyCart Team
    @jacobdubail,

    Sorry to hear you're having problems - could you confirm the store you've got this code in? I tried it out on my own store, and it worked fine for me - so I wonder if something else may be causing problems for you.
  • And I just tested again this morning and it works like a charm. No idea what my issue was the other day... Thanks!
  • fc_adamfc_adam FoxyCart Team
    @jacobdubail - I'm glad it's working now! Hopefully it stays that way :smile:
  • jacobdubailjacobdubail Member
    edited September 28
    I wish it had stayed that way ;)

    It's acting like the FC.cart.render() is never called. If I call it manually, the error appears. Otherwise, I get nothing.
  • fc_adamfc_adam FoxyCart Team
    @jacobdubail,

    Haha - when does code ever do what it's told :smiley:

    Try switching out this line FC.cart.renderCouponEntry(); for FC.Template(FC.json.context).clearOutput();
  • That works great. How could I have figured that one out of my own? I like to be proactive before I harass you in the forums ;) Thanks!
  • fc_adamfc_adam FoxyCart Team
    @jacobdubail,

    I'm glad that worked! In terms of the `clearOutput()` function call - that's not something that is currently documented. We'll look at where it might make sense to do that.
  • One more bother here for you. Scope changed slightly.

    I now want to block or remove the coupon if we have 5 items in the cart. So, if quantity increments up to 5, I remove coupons and display an error. If 5 items already and coupon is added, block it and add an error.

    FC.client.on('cart-coupon-add', function(params,next) {

    var qty = 0;

    if ( ! jQuery.isEmptyObject(FC.json.items) ) {
    jQuery.each(FC.json.items, function(i, item) {
    if ( item.category == "DEFAULT" ) {
    qty += parseInt(item.quantity);
    }
    });
    }

    if ( qty >= 5 ) {
    FC.util.addError('coupon', 'You already reached the 20% discount limit.');
    FC.Template(FC.json.context).clearOutput();
    FC.cart.render();
    next(false);
    }

    FC.util.removeError('coupon');
    next();

    });


    FC.client.on('cart-item-quantity-update.done', function(params, next) {
    var qty = 0;

    if ( ! jQuery.isEmptyObject(FC.json.items) ) {
    jQuery.each(FC.json.items, function(i, item) {
    if ( item.category == "DEFAULT" ) {
    qty += parseInt(item.quantity);
    }
    });
    }

    if ( qty >= 5 && ! jQuery.isEmptyObject(FC.json.coupons) ) {
    var my_params = [];

    jQuery.each(FC.json.coupons, function(i, coupon) {
    my_params.coupon_code_id = coupon.id;
    FC.client.event('cart-coupon-remove').trigger(my_params);
    });

    FC.util.addError('coupon', 'You already reached the 20% discount limit.');
    FC.Template(FC.json.context).clearOutput();
    FC.cart.render();
    console.log("HI", FC.util.hasError('coupon'), params);

    }

    });


    I know I have a lot of duplicated code in there. It'll get refactored.

    My issue is that when the cart-item-quantity-update runs, the coupon is removed, but the error isn't displayed. Any ideas?

    Thanks again!

    -Jacob
  • fc_adamfc_adam FoxyCart Team
    @jacobdubail,

    I'd say the coupon remove request is happening after you display the error, and it is clearing the error on the coupon when it's completed.

    It looks like you also need to handle cases where the customer may have multiple coupons? If that's the case - you'll also want to ensure all the coupons have been removed before adding the error. You could do that by looping through them and processing one at a time, like this:
    function removeCoupon() {
    if (!jQuery.isEmptyObject(FC.json.coupons)) {
    jQuery.each(FC.json.coupons, function(i, coupon) {
    FC.cart.request({
    'cart': 'remove_coupon',
    'coupon_code_id': coupon.id
    }).done(function() {
    removeCoupon();
    });
    return false;
    });
    } else {
    addCouponError();
    }
    }

    function addCouponError() {
    FC.util.addError('coupon', 'You already reached the 20% discount limit.');
    FC.Template(FC.json.context).clearOutput();
    FC.cart.render();
    }
    Within your cart-item-quantity-update function you can just call removeCoupon(); within the qty >= 5 conditional, and just addCouponError within the cart-coupon-add call (keeping in the next(false);)
  • looks like it's working perfectly. Thanks, @fc_adam!
Sign In or Register to comment.