我已重寫了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();
}
}
?>
什麼確切的問題你有?請發佈完整的錯誤消息,你已經嘗試解決問題。 – codedge
現在它工作正常.... thnx –