根据警报消息内容执行某些操作

Do something based on the alert message content

本文关键字:执行 操作 消息      更新时间:2023-09-26

我已经创建了一些自定义购物车警报,但目前我遇到了一些脚本运行一些 ajax 响应的问题,这些响应应该在 Business Catalyst 中刷新购物车。

不确定它是什么,但为了让我有时间研究发生了什么,我需要根据警报消息做一些事情,所以让我们说:

1 件商品已添加到您的购物车,这将做一些window.location.reload();

或者如果它只是说:你需要选择一个尺寸什么都不做

此时此刻,我的警报是:

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);
};

请让我知道我在这个阶段可以做到。

关闭时,您需要检查警报并基于此运行代码。

以下是我在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');
    } 
}; 

就我而言,我正在打开页面上的某些模态。代替模态代码,您可以插入自己的 JS。

使用此代码 - 开发 => 布局 => 网上商店 => small_product.html(将以下代码粘贴到页面底部)

<span class="cartalert" style="display:none"></span>
<style>
.cartalert {
    position: fixed;
    bottom: 10%;
    right: 10%;
    padding: 15px 20px;
    text-align: center;
    background: #333;
    color: #fff !important;
    z-index:999999;
    border-radius: 5px;
    font-size: 16px;
}
</style>
<script>
$(function() {
     window.alert = function(msg) {
        msg = msg.replace('ERROR: ','');
        $('.cartalert').text(msg).fadeIn().delay(1000).fadeOut()
     } 
});
</script>