Magento Products Comparison limit - magento

I need to limit the number of products added to compare in Magento. Only wanted to have a maximum of 4 products to be compared.
I'm thinking of doing it in the .phtml (where item display looping is) but have no idea where I should edit to display the message "Compare list is full". Any idea?
Thanks!

I've hooked to the catalog_product_compare_add_product event.
Here is my solution:
Create module.
Directories:
app/code/local/Company //this can be any name
app/code/local/Company/Catalog
app/code/local/Company/Catalog/Helper
app/code/local/Company/Catalog/etc
Module config
Create a file: app/code/local/Company/Catalog/etc/config.xml
File contents:
<?xml version="1.0"?>
<config>
<modules>
<Company_Catalog>
<version>0.1</version>
</Company_Catalog>
</modules>
<frontend>
<events>
<catalog_product_compare_add_product>
<observers>
<company_catalog>
<type>singleton</type>
<class>Company_Catalog_Helper_Observer</class>
<method>limitProductCompare</method>
</company_catalog>
</observers>
</catalog_product_compare_add_product>
</events>
</frontend>
</config>
Helper
Create a file: app/code/local/Company/Catalog/Helper/Observer.php
File contents:
<?php
class Company_Catalog_Helper_Observer extends Mage_Catalog_Helper_Data {
const COMPARE_LIMIT = 4;
function limitProductCompare($event) {
if (Mage::helper('catalog/product_compare')->getItemCount()<self::COMPARE_LIMIT) return;
$session = Mage::getSingleton('catalog/session');
Mage::getSingleton('catalog/product_compare_list')->removeProduct($event->getProduct());
$session->getMessages()->clear();
$session->addNotice($this->__('You have reached the limit of products to compare. Remove one and try again.'));
}
}
Enable module
Create file: app/etc/modules/Company_Catalog.xml
File contents:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Company_Catalog>
<active>true</active>
<codePool>local</codePool>
</Company_Catalog>
</modules>
</config>
Profit!
Everything should work fine now. After adding, 5th product gets removed, and nice notice is displayed. Its not the perfect solution (since it removes a product after it was added), but it does the job well.

compare items are added in Mage_Catalog_Product_CompareController and you can see that there are events dispatched that you can hook your observer to or you can add your limits by extending Mage_Catalog_Model_Product_Compare_List and overriding addProduct() or addProducts() methods or even make this in collection classes

Related

magento can not load my custom model

Hi I have a issue which I don't understand, please someone could help me.
I'm on the 1.8.1
I have a Module in app->code->local->Mycompany
Mycampany
TestModel
etc
config.xml
Model
FirstModel.php
code in config.xml
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_TestModel>
<version>0.1.0</version>
</Mycompany_TestModel>
</modules>
<global>
<models>
<TestModel>
<class>Mycompany_TestModel_Model</class>
</TestModel>
</models>
</global>
</config>
code in FirstModel.php
class Mycompany_TestModel_Model_FirstModel extends Mage_Core_Model_Abstract {
public function output()
{
echo "get";
}
}
When I use
Mage::getModel('TestModel/FirstModel')
magento can not load the class.
I tested and it is working on my local machine.
Please help.
Plus. I also tried:
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_TestModel>
<version>0.1.0</version>
</Mycompany_TestModel>
</modules>
<global>
<models>
<testmodel> /* instead of <TestModel> */
<class>Mycompany_TestModel_Model</class>
</testmodel>
</models>
</global>
</config>
I still can not get anything in
Mage::getModel('testmodel/FirstModel')
Thanks very much.
As you use magento fatory method [Mage::getModel('testmodel/FirstModel') ]
that means you need to use it procedure to define an model.
As per as magento, when autoloader execute testmodel/FirstModel run like:
testmodel=Mycompany_TestModel_Model
and
FirsModel is wrong ,File and name should be Firstmodel as after model model folder( Mycompany/TestModel/Model) all folders and files name should first letter with uppercase case and after that all should be lower a
That means file should be
Firstmodel.php instead of FirstModel
and testmodel/FirstModel should be testmodel/firstmodel
NO underscore should be use in folders and file name

Mage::getModel is returning false

I am trying to get to grips with calling and using models in magento.
My current task is to simply display a string held in a model by calling it from a controller.
When trying to call SimpleOutput.php I get an error message saying that a non object has been called. I have var_dumped it as you will see and it return false.
I have looked at my code and with my limited understanding of what I need to do in Magento I have everything correct. Obviously i'm missing something. Could someone please take a look and if it's a typo tell where to look and if it's more than a simple spelling mistake explain what ive missed and what I should have done and why?
My code is below
Ts/Firstmodule/etc/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Ts_Firstmodule>
<version>0.1.0</version>
</Ts_Firstmodule>
</modules>
<models>
<firstmodule>
<class>Ts_Firstmodule_Model</class>
</firstmodule>
</models>
<frontend>
<routers>
<firstmodule>
<use>standard</use>
<args>
<module>Ts_Firstmodule</module>
<frontName>firstmodule</frontName>
</args>
</firstmodule>
</routers>
</frontend>
</config>
Ts/Firstmodule/controllers/indexController.php
class Ts_Firstmodule_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$simple = Mage::getModel('ts_firstmodule/simpleoutput');
var_dump($simple);
}
}
Ts/Firstmodule/model/simpleoutput.php
class Ts_Firstmodule_Model_SimpleOutput extends Mage_Core_Model_Abstract
{
public function basicText()
{
echo 'this is some text from the simple output model inside the basic text function';
}
}
You must wrap <models> in <global>, just like this :
<global>
<models>
<firstmodule>
<class>Ts_Firstmodule_Model</class>
</firstmodule>
</models>
</global>
Don't hesitate to take a look at the source of the simpler core modules (eg, GoogleAnalytics), to see how they're done and understand the logic behind it.
Like always :
Mage::getModel('ts_firstmodule/simpleoutput');
When you do a getModel / getBlock / helper / etc
The first part of the parameter string is the XML node of the layer definied in the config.xml
The second part is the full path to your file from the layer folder container.
So, in your case : Mage::getModel('firstmodule/simpleoutput'); should load Ts/Firstmodule/Model/Simpleoutput.php
Note : be carefull of the case of your resources (take a look a standard magento for good practices) !
You should modify the config.xml file and add a <global> tag around your <models> tag:
<global>
<models>
<firstmodule>
<class>Ts_Firstmodule_Model</class>
</firstmodule>
</models>
<global>
After this, in order to instantiate a model use it like this:
Mage::getModel('firstmodule/simpleoutput')
The first part of the getModel method (up until /) should be the tag name you set in config.xml right under <models> tag. In your case firstmodule.

Event code in Magento v1.7 not working

G'Day all,
I'm trying to write my first magento event handler to capture "checkout_cart_save_after" but I can't get my Observer.php code to fire, after 2 days of looking at web sites on magento events I understand the whole process but the code is not running.. I'm perplexed as to why.
Here is the modules xml file in /var/magento/app/etc/modules/
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Yourmodule>
<codePool>local</codePool>
<active>true</active>
<version>1.0.0</version>
</Namespace_Yourmodule>
</modules>
</config>
As you can see I'm just using Namespace and Yourmodule as I'm writing a tutorial for others....
In my module's etc directory (/var/magento/app/code/local/Namespace/Yourmodule/Module) I have config.xml
<?xml version="1.0"?>
<global>
<events>
<checkout_cart_save_after>
<observers>
<yourmodule_after_observer>
<type>singleton</type>
<class>Namespace_Yourmodule_Model_Observer</class>
<method>checkout_cart_save_after</method>
</yourmodule_after_observer>
</observers>
</checkout_cart_save_after>
</events>
</global>
and in the Modules directory I have my Observer.php file:
class Namespace_Yourmodule_Model_Observer
{
/*----------------------------------------
*
* LogInfo()
*
* Basic logging of activity to disk for debugging
*/
public function LogInfo($msg)
{
$logfile="/logs/Namespace-module.log";
$fd=fopen($logfile,"a");
if($fd)
{
fwrite($fd,$msg."\n");
fclose($fd);
}
}
public function checkout_cart_save_before($observer)
{
LogInfo("save_before called");
}
public function checkout_cart_save_after($observer)
{
LogInfo("save_after called");
}
}
The result of trying to add and remove items from the cart is that no log file is crearted and when manually created, no data is written, system.log shows no errors, if I skew the XML it reports the errror, so the XML file is being read.
Any thoughts on what I have missed???
Sid
Update 12/2:
I have spent the weekend on this and solved the issue, thanks for the hints, it was a combination of minor things that I should have picked up and the format of the XML file now matches what has been suggested above... there is now a tutorial! I have fully documented the process with working code and config files.
See http://z900collector.wordpress.com/magento/magento-events/
Try updating your config.xml to
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Yourmodule>
<version>1.0</version>
</Namespace_Yourmodule>
</modules>
<global>
<!--helpers>
<yourmodule>
<class>Namespace_Yourmodule_Helper</class>
</yourmodule>
</helpers-->
<models>
<yourmodule>
<class>Namespace_Yourmodule_Model</class>
</yourmodule>
</models>
<events>
<checkout_cart_save_after>
<observers>
<yourmodule_after_observer>
<type>singleton</type>
<class>Namespace_Yourmodule_Model_Observer</class>
<method>checkout_cart_save_after</method>
</yourmodule_after_observer>
</observers>
</checkout_cart_save_after>
</events>
</global>
</config>
See Customize Magento using Event/Observer

Change Magento default status for duplicated products

I have a Magento store installed, and when a product is duplicated in the backend, Magento sets its status to Disabled by default. I don't want that to happen, the duplicated product should have its status copied from the original product as well.
In this post a partial solution was given. I see where I can find the config.xml and make the necessarry changes. However, where do I put such an observer class? Which file should I use/create and would that require any changes to the config.xml input?
Or does somebody have an overall solution for this issue? Thanks in advance!
Try this:
Create: app/code/local/MagePal/EnableDuplicateProductStatus/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<MagePal_EnableDuplicateProductStatus>
<version>1.0.1</version>
</MagePal_EnableDuplicateProductStatus>
</modules>
<global>
<models>
<enableduplicateproductstatus>
<class>MagePal_EnableDuplicateProductStatus_Model</class>
</enableduplicateproductstatus>
</models>
<events>
<catalog_model_product_duplicate>
<observers>
<enableduplicateproductstatus>
<type>singleton</type>
<class>enableduplicateproductstatus/observer</class>
<method>productDuplicate</method>
</enableduplicateproductstatus>
</observers>
</catalog_model_product_duplicate>
</events>
</global>
</config>
Create: app/code/local/MagePal/EnableDuplicateProductStatus/Model/Observer.php
class MagePal_EnableDuplicateProductStatus_Model_Observer
{
/**
* Prepare product for duplicate action.
*
* #param Varien_Event_Observer $observer
* #return object
*/
public function productDuplicate(Varien_Event_Observer $observer)
{
$newProduct = $observer->getEvent()->getNewProduct();
$newProduct->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
return $this;
}
}
Create: app/etc/modules/MagePal_EnableDuplicateProductStatus.xml
<?xml version="1.0"?>
<config>
<modules>
<MagePal_EnableDuplicateProductStatus>
<active>true</active>
<codePool>local</codePool>
</MagePal_EnableDuplicateProductStatus>
</modules>
</config>
Then clear cache and try duplicating a product.
read more # :
http://magento4u.wordpress.com/2009/06/08/create-new-module-helloworld-in-magento/
http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method
make a new product active by default in magento
I found error on this code and find out the solution below:
On app/code/local/MagePal/EnableDuplicateProductStatus/etc/config.xml change
<method> duplicateProduct </method>
TO
<method>productDuplicate</method>

How do i override the "addAction()" funciton under app\code\core\Mage\Checkout\controllers

I am trying to add some data to a session variable when a product is added to the shopping cart...
I tried copying the "CartController.php" under "app\code\core\Mage\Checkout\controllers" to
"local\Mage\Checkout\controllers" but its not actually overriding the controller..
Could anyone give me some hint on this..?
Thanks a lot.
You can create an "Event Oberserver" to trigger an event.
Create the following folders :
app/code/local/[myMagento]/EventTest/etc
app/code/local/[myMagento]/EventTest/Model
create a file under /EventTest/Model directory like myMagento_EventTest.xml and put the following :
<?xml version="1.0"?>
<config>
<modules>
<myMagento_EventTest>
<active>true</active>
<codePool>local</codePool>
</myMagento_EventTest>
</modules>
</config>
create config.xml file under /EventTest/etc folder nad put the following :
<?xml version="1.0"?>
<config>
<modules>
<myMagento_EventTest>
<version>0.1.0</version>
</myMagento_EventTest>
</modules>
<frontend>
<events>
<checkout_cart_product_add_after>
<observers>
<myMagento_EventTest_Model_Observer>
<type>singleton</type>
<class>myMagento_EventTest_Model_Observer</class>
<method>MyTestMethod</method>
</myMagento_EventTest_Model_Observer>
</observers>
</checkout_cart_product_add_after>
</events>
</frontend>
</config>
create model class file observer.php under the /EventTest/Model directory and put following
<?php
class myMagento_EventTest_Model_Observer {
public function MyTestMethod($observer) {
$event = $observer->getEvent(); //Fetches the current event
$product = $event->getProduct();
$eventmsg = "Current Event Triggered : " . $event->getName() . "
Currently Added Product : " . $product->getName();
//Adds Custom message to shopping cart
echo Mage::getSingleton("checkout/session")->addSuccess($eventmsg);
//Your Custom Logic Here
//you can use print_r($product) here to get more details
}
}
I found this , this enables us to override the CartController.php addAction() funciton... This is exaclty i needed.
Hope this would help someone in need.
Balan

Resources