【发布时间】:2014-09-09 08:51:23
【问题描述】:
我无法覆盖 Magento 的核心控制器之一,即愿望清单索引控制器。当我在愿望清单中添加产品时,我需要 Magento 重定向回产品页面而不是愿望清单。这是我到目前为止所做的
- 在 app/code/local 中创建了一个名为 MyCompany 的文件夹,其中包含一个名为 Coreextensions 的子文件夹。
- 在 Coreextensions 文件夹中,我创建了 etc/config.xml,内容如下:
不,此实例旨在在生产之外或在 RDS 免费使用层下使用
<?xml version="1.0"?>
<config>
<modules>
<MyCompany_Coreextensions>
<version>0.1.0</version>
</MyCompany_Coreextensions>
</modules>
<frontend>
<routers>
<wishlist>
<args>
<modules>
<MyCompany_Coreextensions before="Mage_Wishlist">
MyCompany_Coreextensions_Wishlist
</MyCompany_Coreextensions>
</modules>
</args>
</wishlist>
</routers>
</frontend>
</config>
- 在 Coreextensions 文件夹中,我创建了 controllers/Wishlist/IndexController.php,如下所示:
<?php
/* Stay on product page after adding to wishlist */
require_once(Mage::getModuleDir('controllers','Mage_Wishlist').DS.'IndexController.php');
class MyCompany_Coreextensions_Wishlist_IndexController extends Mage_Wishlist_IndexController
{
/**
* Add the item to wish list
*
* @return Mage_Core_Controller_Varien_Action|void
*/
protected function _addItemToWishList()
{
$wishlist = $this->_getWishlist();
if (!$wishlist) {
return $this->norouteAction();
}
$session = Mage::getSingleton('customer/session');
$productId = (int)$this->getRequest()->getParam('product');
if (!$productId) {
$this->_redirect('*/');
return;
}
$product = Mage::getModel('catalog/product')->load($productId);
if (!$product->getId() || !$product->isVisibleInCatalog()) {
$session->addError($this->__('Cannot specify product.'));
$this->_redirect('*/');
return;
}
try {
$requestParams = $this->getRequest()->getParams();
if ($session->getBeforeWishlistRequest()) {
$requestParams = $session->getBeforeWishlistRequest();
$session->unsBeforeWishlistRequest();
}
$buyRequest = new Varien_Object($requestParams);
$result = $wishlist->addNewItem($product, $buyRequest);
if (is_string($result)) {
Mage::throwException($result);
}
$wishlist->save();
Mage::dispatchEvent(
'wishlist_add_product',
array(
'wishlist' => $wishlist,
'product' => $product,
'item' => $result
)
);
$referer = $session->getBeforeWishlistUrl();
if ($referer) {
$session->setBeforeWishlistUrl(null);
} else {
$referer = $this->_getRefererUrl();
}
/**
* Set referer to avoid referring to the compare popup window
*/
$session->setAddActionReferer($referer);
Mage::helper('wishlist')->calculate();
$message = $this->__('%1$s has been added to your wishlist. Click <a href="%2$s">here</a> to continue shopping.',
$product->getName(), Mage::helper('core')->escapeUrl($referer));
$session->addSuccess($message);
} catch (Mage_Core_Exception $e) {
$session->addError($this->__('An error occurred while adding item to wishlist: %s', $e->getMessage()));
}
catch (Exception $e) {
$session->addError($this->__('An error occurred while adding item to wishlist.'));
}
//$this->_redirect('*', array('wishlist_id' => $wishlist->getId()));
$this->_redirectReferer();
}
}
-
在 app/etc/modules 中,我创建了 MyCompany_Coreextensions.xml,如下所示:
<?xml version="1.0"?> <!--we need to enable this module as any other if--> <!--you wish to do it as standalone module extension--> <config> <modules> <MyCompany_Coreextensions> <active>true</active> <codepool>local</codepool> </MyCompany_Coreextensions> </modules> </config>
当然,这行不通,让我发疯。如果我在 Core 文件中进行更改,它会按我的意愿工作,但我不想更改 Core 文件......让我说是的,我确实清除了缓存!
【问题讨论】:
标签: magento controller overriding overloading core