2016-05-14 66 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

其工作現在......實際上它正在與另一個分機 –