我希望只有2个产品属于同一类别才能比较。我正在开发一个健康产品网站。在健康产品类别链接下,我列出了所有产品。来自不同子类别的产品现在也具有可比性。例如。轮椅与血糖仪相当,没有意义。
我正在研究Magento CE 1.9。有没有人从事类似的要求?非常感谢任何帮助。谢谢!
在@ Manoj的回答之后编辑:
我添加了3个文件:
app/etc/modules/mydir_All.xml
app/code/local/mydir/controllers/Product/CompareController.php
app/code/local/mydir/etc/config.xml
CompareController.php has only one method addAction() with the code suggested by @Manoj.
Here is the content of mydir_All.xml:
<?xml version="1.0" ?>
<config>
<modules>
<mydir>
<active>true</active>
<codePool>local</codePool>
</mydir>
</modules>
</config>
Content of config.xml:
<?xml version="1.0"?>
<config>
<modules>
<mydir>
<version>0.1.0</version>
</mydir>
</modules>
<frontend>
<routers>
<tag>
<args>
<modules>
<mydir before="Mage_Catalog">mydir</mydir>
</modules>
</args>
</tag>
</routers>
</frontend>
</config>
似乎没有任何改变。比较产品的效果与以前一样。
答案 0 :(得分:0)
检查addAction
中的app\code\core\Mage\Catalog\controllers\Product\CompareController.php
。
您可以在添加产品检查之前覆盖CompareController
和addAction
,之前添加的产品属于同一类别。如果不删除以前的比较列表并添加新产品。在这里,您可以在比较列表产品catgory会话中保存当前类别ID。因此,您不需要每次为比较列表中添加的产品获取类别ID。
希望这有帮助!!
答案 1 :(得分:0)
请参考以下代码,您需要覆盖app \ code \ core \ Mage \ Catalog \ controllers \ Product \ CompareController.php。
下面将提到addAction
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.', 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();
}
答案 2 :(得分:0)
终于搞定了。发布这个对于那些与之斗争的人来说可能会派上用场。
这是/app/etc/modules/Company_Catalog.xml(惯例是使用带有Caps中首字母的Company_Module.xml):
<?xml version="1.0" ?>
<config>
<modules>
<Company_Catalog> <!-- notice Company_Module tag here also -->
<active>true</active>
<codePool>local</codePool>
</Company_Catalog>
</modules>
</config>
这是/app/code/local/Company/Catalog/etc/config.xml:
<?xml version="1.0"?>
<config>
<modules>
<Company_Catalog> <!--notice the Company_Catalog tag here -->
<version>0.1.0</version>
</Company_Catalog>
</modules>
<frontend>
<routers>
<catalog> <!-- this has to be the module name -->
<args>
<modules>
<Company_Catalog before="Mage_Catalog">Company_Catalog</Company_Catalog> <!--this has to be in one line else newline/ spaces won't be parsed. Notice the before=Mage_ModuleName -->
</modules>
</args>
</catalog>
</routers>
</frontend>
</config>
这是/app/code/local/Company/Catalog/controllers/Product/CompareController.php:
<?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();
}
}
?>
我建议有人试图第一次重写模块并陷入困境:
希望它有所帮助!