Search code examples
javascriptjqueryajaxbusiness-catalyst

Do something based on the alert message content


I have created some custom cart alert but I am having an issue at the moment with some scripts running some ajax responses that was supposed to refresh the cart in Business Catalyst.

I am not sure what it is but to give me time to look into whats happening I need to do something depending on the alert message so lets say:

1 item has been added to your cart this will do some window.location.reload();

or if it just says: you need to choose a size DO NOTHING

at he moment moment my alert is:

window.alert = function(message) {

$('.shop-main').addClass('blur');
$('.messageAlert').fadeIn().text(message).prepend('<img src="/images/ui-pieces/shopping-cart.png" width="40" height="40" alt="cart alert"/>');

setTimeout(function() { 

$('.messageAlert').fadeOut(); 
$('.shop-main').removeClass('blur');


  // window.location.reload();

}, 4000);
};

Please let me know I can do at this stage.


Solution

  • Your close, you need to check the alert and run code based upon that.

    Here is what I do within BC:

    window.alert = function(text) {
        if (text.indexOf("Please choose relevant options before") > -1) {
            //CODE TO RUN IF ALERT MATCHES THE ABOVE TEXT
            $('#choose-option').foundation('reveal', 'open');
        } else if (text.indexOf("item(s) added to your cart") > -1) {
            //CODE TO RUN IF ALERT MATCHES THE ABOVE TEXT
            $('#store-modal').foundation('reveal', 'open');
        } else if (text.indexOf("Quantity entered is too large, please enter a smaller quantity.") > -1) {
            //CODE TO RUN IF ALERT MATCHES THE ABOVE TEXT
            $('#limited-quantity').foundation('reveal', 'open');
        } 
    }; 
    

    In my case I'm opening certain modals I have on the page. In place of the modal code you could insert your own JS.