首页 文章

Magento:产品添加到购物车后的自定义Javascript警报

提问于
浏览
1

我一直在关注各种教程,尝试使用Magento事件观察器在客户将产品添加到购物车后显示自定义Javascript警报,但似乎无法得到任何东西 . 我甚至接近走下正轨吗?

我的模块:

<?xml version="1.0"?>
<config>
    <modules>
        <Shoplio_XS>
            <active>true</active>
            <codePool>local</codePool>
        </Shoplio_XS>
    </modules>
</config>

我的config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Shoplio_XS>
            <version>0.1.0</version>
        </Shoplio_XS>
    </modules>
    <frontend>
        <events>
            <sales_quote_product_add_after>
                <observers>
                    <Shoplio_XS_Model_Observer>
                        <type>singleton</type>
                        <class>Shoplio_XS_Model_Observer</class>
                        <method>Mytestmethod</method>
                    </Shoplio_XS_Model_Observer>
                </observers>
            </sales_quote_product_add_after>
        </events>
    </frontend>
</config>

我的观察者.php:

<?php class Shoplio_XS_Model_Observer
{
    public function Mytestmethod($observer) {
        $event = $observer->getEvent();

        // Javascript Alert Here

    } 
}

我主要关注本教程:http://goo.gl/DRwd5

唯一的区别是,我不想在购物车页面上显示任何内容,因为我将产品添加到购物车后将客户留在产品页面上 . 我只是想在产品添加到购物车后在产品页面上显示自定义Javascript警报 .

2 回答

  • 0

    您不能使用Magento Observer执行Javascript,因为观察者代码在服务器端“执行”,然后将结果发送回浏览器(而JS在浏览器的客户端完成)

    你能做的是

    在app / design / frontend / default / [theme] /template/catalog/product/view.phtml

    var productAddToCartForm = new VarienForm('product_addtocart_form');
     productAddToCartForm.submit = function(button, url) {
            if (this.validator.validate()) {
    
                alert('here');
    
                var form = this.form;
                var oldUrl = form.action;
    
                if (url) {
                   form.action = url;
                }
                var e = null;
                try {
                    this.form.submit();
                } catch (e) {
                }
                this.form.action = oldUrl;
                if (e) {
                    throw e;
                }
    
                if (button && button != 'undefined') {
                    button.disabled = true;
                }
    
            }
    
     }.bind(productAddToCartForm);
    
  • 0

    我决定退回Magento核心消息 . 我首先将Core Messages Block移动到我的本地文件夹,然后在第255行添加以下代码:

    /app/code/local/Mage/Core/Block/Messages.php

    if(strstr($html, 'was added to your shopping cart')) {
        $html.= '<script type="text/javascript">setTimeout("myFunction()",1000);</script>';
    }
    

    上面的代码片段确保仅在出现核心添加到购物车成功消息时才会延迟执行我的自定义Javascript功能 .

    我在之后添加了:

    $html.= ($this->_escapeMessageFlag) ? $this->htmlEscape($message->getText()) : $message->getText();
    

    然后,在我的view.phtml文件中,我添加了以下Javascript函数,用我的自定义消息打开一个模态框:

    function xsModal() {
        Modalbox.show($('modal-id'), {title: 'My Custom Message', width: 600});
    }
    

    奇迹般有效!

相关问题