magento以编程方式检测产品页面

时间:2013-01-15 15:03:42

标签: magento url-rewriting magento-1.7

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

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

但到目前为止,我们正在测试开发。从今天开始,我们正在测试预生产服务器,包括许多模块和插件。到目前为止它一直工作,但现在它不起作用,它不会检测何时加载产品页面。我认为与url重写有关,以显示更“友好”的网址,但无法检测如何解决它。我看了一下Alan Storm教程In Depth Magento Dispatch,但我仍然可以得到错误或者我需要改变什么。

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

1 个答案:

答案 0 :(得分:2)

您已经确认观察者正在被击中?确保您触发的事件具有该事件的唯一名称。例如,在以下代码中,名称“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
相关问题