2016-05-14 37 views
0

我已重写了CompareController.php在本地文件夹中,但随后不工作我我的两个控制器和Magento的核心CompareController.php控制器,但随后也addtocompare功能正在如何比较magento中的同类产品?

<?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

什么确切的问题你有?请发布完整的错误消息,你已经尝试解决问题。 – codedge

+0

现在它工作正常.... thnx –

回答

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> 
+0

好吧,得到了你的观点,我已经试过这个,但它不工作.... –

+0

它是100%的工作,也许你有错误/ –

+0

其工作现在......实际上它正在与另一个分机 –