简体   繁体   English

将项目添加到购物车后,Magento调用Modalbox的最佳方法

[英]Magento Best Way to Call Modalbox After Item is Added to Cart

I'm looking for a way to call a Modalbox after a customer has added an item to a cart. 我正在寻找一种在客户将商品添加到购物车后调用Modalbox的方法。

How do you call a Modalbox from within php? 您如何在php中调用Modalbox? I was thinking of calling the modalbox after the success message in the CartController.php, however, I don't know how to do this from PHP, only from an HTML link with OnClick. 我当时想在CartController.php中的成功消息后调用模式框,但是,我不知道如何从PHP做到这一点,只能从带有OnClick的HTML链接中做到。

Obviously you cannot do exactly that as PHP is run on the server and the Modalbox is run in JavaScript in the client's browser - potentially on opposite sides of the world, as much as we like to forget physical barriers do have an effect on software. 显然,您无法完全做到这一点,因为PHP在服务器上运行,而Modalbox在客户端浏览器中的JavaScript中运行-可能在世界的相反方向,就像我们想忘记的物理障碍确实会对软件产生影响一样。 The best we can do is insert Javascript code in appropriate places so that the browser eventually executes it. 我们能做的最好的事情就是在适当的位置插入Javascript代码,以便浏览器最终执行它。

The first concrete sign of an item being added is the checkout_cart_product_add_after event. 要添加的项目的第一个具体标志是checkout_cart_product_add_after事件。 Create a module and amend it's config.xml file; 创建一个模块并修改它的config.xml文件;

<config>
    <!-- ...usual blocks, helpers and models stuff here... -->
    <frontend>
        <!-- this is the safest way to catch items being added -->
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <yourmodule_product_add>
                        <class>yourmodule/observer</class>
                        <method>onProductAdd</method>
                    </yourmodule_product_add>
                </observers>
            </checkout_cart_product_add_after>
        </events>
        <!-- will need to insert a block later -->
        <layout>
            <updates>
                <yourmodule>
                    <file>yourmodule.xml</file>
                </yourmodule>
            </updates>
        </layout>
    </frontend>
</config>

That declares an observer which it expects to find in Your/Module/Model/Observer.php ; 那声明了一个期望在Your/Module/Model/Observer.php找到的Your/Module/Model/Observer.php

class Your_Module_Model_Observer
{
    public function onProductAdd()
    {
        // page might redirect immediately so make a flag for now
        Mage::getSingleton('checkout/session')->setShowModalbox(true);
    }
}

You should be able to see what will happen next. 您应该能够看到接下来会发生什么。 A file was earlier declared as app/design/frontend/base/default/layout/yourmodule.xml ; 先前app/design/frontend/base/default/layout/yourmodule.xml文件声明为app/design/frontend/base/default/layout/yourmodule.xml

<layout>
    <!-- hook this block for all pages because anything
         might be shown after adding a product -->
    <default>
        <reference name="before_body_end">
            <block type="yourmodule/modalbox" name="yourmodule_modalbox" />
        </reference>
    </default>
    <default>
</layout>

The final step is to make Your/Module/Block/Modalbox.php which will actually insert the relevant Javascript when necessary; 最后一步是制作Your/Module/Block/Modalbox.php ,它将在必要时实际插入相关的Javascript;

class Your_Module_Block_Modalbox extends Mage_Core_Block_Text_Tag_Js
{
    protected function _toHtml()
    {
        if (!Mage::getSingleton('checkout/session')->getShowModalbox())
            return $this;

        $url = 'URL to show in Modalbox';
        $this->setContents("Modalbox.show({$url}, {width: 600});");
        Mage::getSingleton('checkout/session')->unsShowModalBox();

        return parent::_toHtml();
    }
}

This is the only best practice way I can think of but I am enamoured with Mage_Core_Block_Text_Tag_Js having just discovered it. 这是我能想到的唯一的最佳实践方法,但是我被刚刚发现的Mage_Core_Block_Text_Tag_Js所迷住。 As the saying goes 'when all you have is a hammer all problems start to look like nails'. 俗话说“当你只有一把锤子时,所有问题都开始看起来像钉子”。 I may be missing something simpler and more obvious. 我可能缺少一些更简单,更明显的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM