我在重写 Magento 的核心控制器之一 WishList 索引控制器时遇到问题。当我在愿望清单中添加产品时,我需要 Magento 重定向回产品页面而不是愿望清单。这是我到目前为止所做的
不,此实例旨在用于生产之外或在 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>
<?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>
当然,这不起作用,而且让我发疯。如果我在核心文件中进行更改,它会按照我想要的方式工作,但我不想更改核心文件...让我说是的,我确实清除了缓存!
如果您使用 Magento EE,您应该替换:
<MyCompany_Coreextensions before="Mage_Wishlist">
与:
<MyCompany_Coreextensions before="Enterprise_Wishlist">
你的步骤是正确的,但只有两件事:
require_once Mage::getModuleDir('controllers', 'Mage_Wishlist') . DS . 'IndexController.php';
到控制器类声明的第一个;MyCompany_Coreextensions_IndexController
而不是 MyCompany_Coreextensions_Wishlist_IndexController
(您也必须将其更改为 config.xml -> <MyCompany_Coreextensions before="Mage_Wishlist">MyCompany_Coreextensions</MyCompany_Coreextensions>
而不是 <MyCompany_Coreextensions before="Mage_Wishlist">MyCompany_Coreextensions_Wishlist</MyCompany_Coreextensions>
)此外,您还没有“包含”或“需要”愿望清单的原始(核心)IndexController,您必须在模块的索引控制器文件中执行如下操作:
require_once Mage::getModuleDir('controllers', 'Mage_Wishlist') . DS . 'IndexController.php';
上述代码应放置在模块的 IndexController.php 文件中的任何类声明之前。
“<" and tag names, so do remove them or xml structure will break, showing fatal errors in magento.
”之间也有空格一旦您刷新了 magento 缓存,然后尝试将产品添加到愿望清单,您将被重定向到之前的网址。
< codepool >local< /codepool >
如果你还是不明白,就得把
codepool
改成codePool