If I use cart.addLine() within the cart itself, th...
# suitecommerce
d
If I use cart.addLine() within the cart itself, the afterAddLine event is firing repeatedly for each line in the cart, rather than just once. Is this expected behaviour?
c
I had the same question a couple weeks ago and believe the answer was yes.
d
As the cart seems to be lacking a recalc function, I'm trying to fire an event any time an item is added or removed - but the lack of any contextual value from afterAddLine to say whether the item is new or existing means I'm having recursion issues.
s
Yeah, this is expected behaviour. It may seem counter-intuitive but it's actually pretty useful. So, after being called, it will iterate over every line in the cart once. The idea being that if you have something that might want to do something because of the cart's contents, then you can use this opportunity. Eg:
Copy code
container.getComponent('Cart').cancelableOn('afterUpdateLine', function (line) {
  if (line.internalid === '123') {console.log('Item 123 is in the cart!')}
})
But yes, I get that it may be the case that you want it just to fire on the specific line you just added; in which case you'll need to add some sort of condition to catch the line you want. In 21.1, we are adding the
getLatestAddition()
method to specifically return the line item that was last added.
c
@Steve Goldberg Will cart.addLine() cause afterUpdateLine to trigger? Or is afterUpdateLine not available?
s
It shouldn't cause it to trigger (it might trigger if you're adding a line that already matches an existing line, but I haven't tried that particular use case)
d
getLatestAddition() is exactly what I need to resolve the problem I'm having at the moment. Broadly speaking, when is 21.1 likely to be released? I appreciate this is probably not firmly defined at the moment, but there are now two new methods I could really do with using for the site we're currently working on. Just a vague indication of release date would help me understand whether changing our planned launch date may be beneficial.
s
We can't give dates. Theoretically, your account could get it anywhere in the next 1-6 months, assuming the release goes to plan. getLatestAddition() is available on the LiveOrder.Model singleton if you want to temporarily use that in your customisation and swap it out once the release goes live.
c
@Dominic B would this work?
Copy code
var self = this;

this.cart.addLine({
    line: {
        quantity: 1,
        item: {
            internalid: self.model.get('internalid'),
        }
    }
}).then(
    function onSuccess(line) {

        //  get data of recently item added to the cart
        var cartLines = self.cart.getLines().then(function (cartLines) {

            var currentLine;

            // get item data of added item
            _.each(cartLines, function (cartLine) {
                if(line === cartLine.internalid){
                    // do something
                };
            });

        });

    },
    function onError() {
        // error code
    }
);