我已经覆盖了本地文件夹中的 CompareController.php 但是没有工作,那么我重命名我的控制器和magento的核心 CompareController.php 控制器,但是addtocompare功能正在运行
<?php
//notice that require_once is calling **CompareController.php** under the Product directory
require_once(Mage::getModuleDir('controllers','Mage_Catalog').DS.'Product'.DS.'CompareController.php');
class Company_Catalog_Product_CompareController extends Mage_Catalog_Product_CompareController
{
public function addAction() {
if (!$this->_validateFormKey()) {
$this->_redirectReferer();
return;
}
$productId = (int) $this->getRequest()->getParam('product');
$product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($productId);
$categoryIds = $product->getCategoryIds();
$productPresent = false;
$found = array();
$compareProducts = Mage::helper('catalog/product_compare')->getItemCollection();
$itemCount = Mage::helper('catalog/product_compare')->getItemCount();
if($itemCount) {
$compareProductId = $compareProducts->getFirstItem()->getId();
$compareProductCollection = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($compareProductId);
$compareProductCats = $compareProductCollection->getCategoryIds();
foreach($categoryIds as $num) {
if (in_array($num,$compareProductCats)) {
$found[$num] = true;
}
}
foreach($compareProducts as $products) {
if($productId == $products->getId()) {
$productPresent = true;
}
}
//Check if categories of products to be compared are matching
if(empty($found)){
Mage::getSingleton('catalog/session')->addError(
$this->__('You cannot compare %s with the items in the comparison list. Please select products from the same category.', Mage::helper('core')->escapeHtml($product->getName()))
);
$this->_redirectReferer();
return;
}
}
//Add product in comparison list
if ($productId && (Mage::getSingleton('log/visitor')->getId() || Mage::getSingleton('customer/session')->isLoggedIn())) {
if ($product->getId()/* && !$product->isSuper()*/) {
Mage::getSingleton('catalog/product_compare_list')->addProduct($product);
Mage::getSingleton('catalog/session')->addSuccess(
$this->__('The product %s has been added to the comparison list.', Mage::helper('core')->escapeHtml($product->getName()))
);
Mage::dispatchEvent('catalog_product_compare_add_product', array('product'=>$product));
}
Mage::helper('catalog/product_compare')->calculate();
}
$this->_redirectReferer();
}
}
?>
答案 0 :(得分:0)
您无法以这种方式覆盖控制器。 Magento类加载器不在控制器类的本地文件夹中查找。
但是你可以在你想要覆盖的控制器之前添加你自己的控制器的调用 为此,您需要使用新控制器创建自己的模块,并在config.xml中添加以下行:
<config>
...
<frontend>
<routers>
<catalog>
<args>
<modules>
<MyModule_Compare before="Mage_Catalog">MyModule_Compare</MyModule_Compare>
</modules>
</args>
</catalog>
</routers>
</frontend>
...
</config>