I need that, when the user goes to the compare list page it will reset so if he navigates to the products again he will have to choose different products for the list.

How can I do this?


You can use the code that Magento uses in the clearAction() of the product compare controller and addapt it a little:

$items = Mage::getResourceModel('catalog/product_compare_item_collection');
    if (Mage::getSingleton('customer/session')->isLoggedIn()) {
    } elseif ($this->_customerId) {
    } else {

/** @var $session Mage_Catalog_Model_Session */
$session = Mage::getSingleton('catalog/session');

try {
catch (Exception $e){
    //do nothing

You can add this code at the end of your compare list template (.phtml) but a clean option would be to either override the list block (Mage_Catalog_Block_Product_Compare_List) and add this code in the _afterToHtml() methods, or create an observer for the event core_block_abstract_to_html_after and check if the block passed to the observer matches your block (instanceof Mage_Catalog_Block_Product_Compare_List) then execute the code above.

I recommend overriding the block and adding the code in the _afterToHtml() method because the event is called for each rendered block and it may add an overhead to page rendering.

  • That class doesn't have _afterToHtml() method, should I simply add it? Apr 23 '13 at 9:40
  • Simply add it. The list block is a child of the class Mage_Core_Block_Abstract that has the method and the method is called inside toHtml()
    – Marius
    Apr 23 '13 at 9:42
  • I'm getting a blank page :S Edit: I get a blank page if I add the method to the class, it works adding it on the phtml Apr 23 '13 at 9:47
  • You should end your _afterToHtml($html) method with return parent::_afterToHtml($html). But if it works the 'quick & dirty' way, when adding it to the phtml file and you don't plan to pack your code as a commercial extension then I guess there is no problem with that.
    – Marius
    Apr 23 '13 at 9:59
  • Yeah, I'll leave it like that, thank you very much! Apr 23 '13 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.