dragon071111 2014-05-20 16:52
浏览 32
已采纳

重写Magento目录产品视图操作

I write an extension for ProductController, I need to make a redirect to canonical URL when there is a request for /catalog/product/view/[ID-PROD] URL. I create a module with this config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <MyModule_CanonicalRedirect>
            <version>0.0.0.0.1</version>
        </MyModule_CanonicalRedirect>
    </modules>


    <frontend>
        <routers>
            <MyModule_canonicalredirect>
                <use>standard</use>
                <args>
                    <modules>
                        <MyModule_CanonicalRedirect>MyModule_CanonicalRedirect
                        </MyModule_CanonicalRedirect>
                    </modules>
                    <frontName>canonicalredirect</frontName>
                </args>
            </MyModule_canonicalredirect>
        </routers>
    </frontend>


    <global>
        <rewrite>
            <MyModule_CanonicalRedirect>
                <from><![CDATA[#^/catalog/product/view/#]]></from>
                <to>/canonicalredirect/index/view/</to>
            </MyModule_CanonicalRedirect>
        </rewrite>
    </global>
</config>

and this is my controller:

include_once('Mage/Catalog/controllers/ProductController.php');
class MyModule_CanonicalRedirect_IndexController extends Mage_Catalog_ProductController {
    public function viewAction()
    {
        // Get initial data from request
        $categoryId = (int) $this->getRequest()->getParam('category', false);
        $productId  = (int) $this->getRequest()->getParam('id');
        $specifyOptions = $this->getRequest()->getParam('options');

        $prod = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($productId);


        if($prod->getId() == $productId) {
            $prodUrl = $prod->getUrlModel()->getUrl($prod, array('_ignore_category'=>true));
            Mage::app()->getFrontController()->getResponse()->setRedirect($prodUrl, 301);
        }
        else {
            parent::viewAction();
        }
    }
}

When I try to get a existing product, all is ok, but if I try to get a product with an ID that does not exist I have the error:

Call to a member function getMetaTitle() on a non-object in /app/code/core/Mage/Catalog/Block/Product/View.php on line 56

I find more question about this error but, in my case there is a simple forward to NOROUTE action, I can't init the product because I haven't it.

Please reply me for this issue. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongwenyou4298 2014-05-20 17:33
    关注

    yes you have to first load the product in your action and set the product object in the registry it will solve your problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比