【问题标题】:magento detecting product page programmaticallymagento 以编程方式检测产品页面
【发布时间】:2012-12-29 17:12:52
【问题描述】:

在观察者文件上,我需要检测加载的页面是否对应于产品。我一直在使用与checked answer on this question 上使用的方法类似的方法:

if (!(Mage::registry('current_product'))) return;

但直到现在我们还在开发中进行测试。从今天开始,我们将在预生产服务器上进行测试,其中包含大量模块和插件。到目前为止,它工作得很好,但现在它不起作用,它没有检测到何时加载产品页面。我认为与 url 重写有关,以显示更“友好”的 url,但无法检测到如何解决它。我查看了 Alan Storm 教程In Depth Magento Dispatch,但我仍然可以了解问题所在或需要更改的地方。

有什么想法吗?或者关于如何检测产品页面何时加载的其他解决方案?

【问题讨论】:

    标签: magento url-rewriting magento-1.7


    【解决方案1】:

    你已经确认观察者被击中了?确保您触发的事件具有该事件的唯一名称。例如,在以下代码中,名称“catalog_product_set_price”对于“catalog_product_load_after”事件必须是唯一的。我假设你正在进入观察者,但以防万一。如果您将一堆新模块添加到实例中,则可能会出现这种风险。

            <catalog_product_load_after>
                <observers>
                    <catalog_product_set_price>
                        <type>singleton</type>
                        <class>NamespaceModule_Model_Observer</class>
                        <method>set_price2</method>
                    </catalog_product_set_price>
                </observers>
            </catalog_product_load_after>
    

    您可以使用以下数组来检查您的位置:

    $currentPageArray = array(
                'request_string'=> Mage::app()->getFrontController()->getRequest()->getRequestString(),
                'uri'           => Mage::app()->getFrontController()->getRequest()->getRequestUri(),
                'route'         => Mage::app()->getFrontController()->getRequest()->getRouteName(),
                'action'        => Mage::app()->getFrontController()->getRequest()->getActionName(),
                'controller'    => Mage::app()->getFrontController()->getRequest()->getControllerName()
            );
    

    它会输出如下内容:

    Array
    (
        [request_string] => /some-category/some-product-page-url.html
        [uri] => /catalog/product/view/id/64806/category/17
        [route] => catalog
        [action] => view
        [controller] => product
    )
    

    你可以尝试不同的观察者。

    • controller_action_layout_render_before_catalog_product_view
    • controller_action_postdispatch_catalog_product_view

    【讨论】:

    • 不错的答案,仅针对详细信息获得了赞成票。是的,观察者被击中,这不是问题。在您发布答案的同时,我正在精确测试“Mage::app()->getFrontController()->getRequest()->getControllerName()”的使用,它确实有效。我也会尝试使用最后建议的观察者进行测试。
    猜你喜欢
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    相关资源
    最近更新 更多