簡體   English   中英

重寫Magento目錄產品視圖操作

[英]Rewrite Magento catalog product view action

我編寫了ProductController的擴展名,當需要/ catalog / product / view / [ID-PROD] URL時,需要重定向到規范URL。 我使用以下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>

這是我的控制器:

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();
        }
    }
}

當我嘗試獲取現有產品時,一切正常,但如果嘗試獲取ID不存在的產品,則會出現錯誤:

在第56行的/app/code/core/Mage/Catalog/Block/Product/View.php中的非對象上調用成員函數getMetaTitle()

我發現有關此錯誤的更多問題,但是就我而言,NOROUTE操作很簡單,我無法初始化產品,因為我沒有。

請為這個問題回復我。 提前致謝。

是的,您必須首先在操作中加載產品並在注冊表中設置產品對象,這將解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM