Hello everyone, I need to validate the quantity af...
# suitecommerce
l
Hello everyone, I need to validate the quantity after clicking the Add to cart. I need to check if that quantity is the minimal required, if not then I need to cancel that click of that add to cart. Anyone knows any classes that helps to build this via Extension?
e
l
thanks @eminero! I’m already testing the beforeAddLine 👍 Do you know if when trying to cancel the add to cart of the item, is there any other option besides using : return new jQuery.Deferred().reject(); ?
e
Honestly not, I think I have only used adding some logic but not cancelling the event.
👍 1
s
The only alternative to a rejected deferred object is to throw an error, but I think that offers a worse user experience. What's not to like about a rejected promise?
I suppose if you want to throw an error but don't want it to bubble up you could wrap it in a try/catch statement and do something instead of emitting the error 🤔
l
@Steve Goldberg and is there any way I could customize the message showing in that rejected deferred object?
s
I believe passing a string into
reject()
does this, no?
Are you trying to show a custom error message or something?
Because I have an example of this on the developer portal:
Copy code
mountToApp: function mountToApp (container) {
            var PDP = container.getComponent('PDP');

            if (PDP) {
                PDP.cancelableOn('beforeQuantityChange', function preventMoreThanFive (quantity) {
                    if (quantity > 5) {
                        PDP.showMessage({
                            message: 'Sorry, you\'re only allowed a maximum of five of this item',
                            type: 'error',
                            selector: 'Notifications',
                            timeout: 10000
                        })
                        return new jQuery.Deferred().reject()
                    }
                });
            }
        }
l
Yep, I actually used that one but I was trying to customize somehow the default popup with the Error message from the deferred
s
What default popup?
I don't think returning a rejected deferred object does anything other than cancel the action?
l
When I use the deferred it shows this popup:
message has been deleted
That’s the popup when I click the add to cart and it has the deferred in place
s
Ah
I don't think you can cancel the error popup from appearing but as I said earlier: passing in a string to reject() will overwrite the message
return new jQuery.Deferred().reject('custom error');
Yeah the code doesn't have a mechanism in it to prevent popups
👍 1
l
Nice! thanks! that works! And do you know if I could change the title as well?
s
There doesn't appear to be a mechanism for that either
You would probably have to do a bit of work on the default error template to change it to support it
l
Ok, I’ll take what I can then. Thanks for your help!
s
Even then I don't know how you would bubble up a custom title to it using this mechanism
no problems
l
@Steve Goldberg last question, if I use the deferred to cancel the add to cart, that same result will apply if I use it on the shopping cart and quick view, right? The result will be the same?
s
Yes. Adding an event to the cart component applies wherever someone might add an item to the cart, regardless of where it happnes
l
Ok, so for the Update popup from the shopping cart too?
s
AFAIK yes
l
ok
k
@Steve Goldberg, do the events also listen to custom cart component addLine calls?
s
yes