I need to set limit to the compared product, for example: max_limit 4
, But now it is adding Number of products to compare list.
Kindly suggest.
Magento 2 – Set Limit to Compared Products
comparemagento2product
Related Solutions
Yes, it's simple to add these lines to local.xml file
<reference name="product.info">
<block type="page/html_pager" name="product_review_list.toolbar" />
<block type="core/template" name="product_review_list.count" template="review/product/view/count.phtml" />
<block type="review/product_view_list" name="product_customer_reviews" as="product_customer_reviews" template="review/product/view/list.phtml">
<block type="review/form" name="product.review.form" as="review_form">
<block type="page/html_wrapper" name="product.review.form.fields.before" as="form_fields_before" translate="label">
<label>Review Form Fields Before</label>
<action method="setMayBeInvisible"><value>1</value></action>
</block>
</block>
</block>
</reference>
hi you can get current customer/visitor by below code:
Step1: get Compare product collection using resource model
$items=Mage::getResourceModel('catalog/product_compare_item_collection')
->useProductItem(true)
->setStoreId(Mage::app()->getStore()->getId());
Step2: Then filter this collection by current visitor/ customer
which will give your current compare list
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
$items->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId());
}else {
$items->setVisitorId(Mage::getSingleton('log/visitor')->getId());
}
Finally this code $items current exiting product list
Step3: Magento is add compare product using Mage_Catalog_Product_CompareController
at function addAction
and then using controller_action_predispatch_catalog_product_compare_add
check current no of compare
products and redirect to your list page;
xml code for observer:
<global>
<events>
<controller_action_predispatch_catalog_product_compare_add>
<observers>
<controller_action_before>
<class>youmodeltype/yourfunction</class>
<method>myFunction</method>
</controller_action_before>
</observers>
</controller_action_predispatch_catalog_product_compare_add>
</events>
</global>
See: Overriding a core controller's preDispatch method
and Observer code is
<php
class YourmoduleNameSapce_YourmoduldeName_Model_Observer
{
public function myFunction($observer)
{
$controller = $observer->getControllerAction();
$items=Mage::getResourceModel('catalog/product_compare_item_collection')
->useProductItem(true)
->setStoreId(Mage::app()->getStore()->getId());
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
$items->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId());
}else {
$items->setVisitorId(Mage::getSingleton('log/visitor')->getId());
}
/* send error */
if(count($items)>3){
Mage::getSingleton('customer/session')->addError(Mage::helper('captcha')->__('Error.'));
$controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
$controller->getResponse()->setRedirect($url);
}
return $this;
}
}
Edit:
[FULL MOdule] According to your request i have put extension please check
Step1:create config.xml at app>code>local>Stackexchange>Magento38261>etc
code is
<?xml version="1.0" ?>
<config>
<modules>
<Stackexchange_Magento38261>
<version>1.0.0</version>
</Stackexchange_Magento38261>
</modules>
<global>
<models>
<magento38261>
<class>Stackexchange_Magento38261_Model</class>
</magento38261>
</models>
</global>
<frontend>
<events>
<controller_action_predispatch_catalog_product_compare_add>
<observers>
<prevent_add_to_compare>
<class>magento38261/observer</class>
<method>preventadd</method>
</prevent_add_to_compare>
</observers>
</controller_action_predispatch_catalog_product_compare_add>
</events>
</frontend>
</config>
Step2: create Observer.php
and where you have prevent to add compare list
:
and code is:
<?php
class Stackexchange_Magento38261_Model_Observer{
public function preventadd($observer){
$controller = $observer->getControllerAction();
$items=Mage::getResourceModel('catalog/product_compare_item_collection')
->useProductItem(true)
->setStoreId(Mage::app()->getStore()->getId());
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
$items->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId());
}else {
$items->setVisitorId(Mage::getSingleton('log/visitor')->getId());
}
/* send error */
if(count($items)>3){
Mage::getSingleton('catalog/session')->addError(Mage::helper('catalog')->__('Error.'));
$controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
// $controller->getResponse()->setRedirect(Mage::getUrl());
/* start to redirec to lst page */
$refererUrl = Mage::app()->getRequest()->getServer('HTTP_REFERER');
if ($url = Mage::app()->getRequest()->getParam('referer_url')) {
$refererUrl = $url;
}
if ($url =Mage::app()->getRequest()->getParam('r64')) {
$refererUrl = Mage::helper('core')->urlDecode($url);
}
if ($url =Mage::app()->getRequest()->getParam('uenc')) {
$refererUrl = Mage::helper('core')->urlDecode($url);
}
if (empty($refererUrl)) {
$refererUrl =Mage::getBaseUrl();
}
$controller->getResponse()->setRedirect($refererUrl);
}
return $this;
}
}
Step3:module file Stackexchange_Magento38261.xml at app\etc\modules
code is:
<?xml version="1.0" ?>
<config>
<modules>
<Stackexchange_Magento38261>
<codePool>local</codePool>
<active>true</active>
</Stackexchange_Magento38261>
</modules>
</config>
This code have checked and work perfectly on my machine
Best Answer
We can try with Plugin. We use
Magento\Catalog\Helper\Product\Compare
to get the current compared products.app/code/Company/Catalog/etc/frontend/di.xml
app/code/Company/Catalog/Model/Plugin/Compare/LimitToCompareProducts.php