Rewriting app/code/core/Mage/Catalog/controllers/Product/CompareController.php in Magento 1

controllersmoduleoverrides

I am trying to rewrite app/code/core/Mage/Catalog/controllers/Product/CompareController.php. In my config.xml I have the following:

<frontend>
  <routers>
    <catalog>
      <args>
        <modules>
          <Slice_CustomCompare before="Mage_Catalog">Slice_CustomCompare_Catalog_Product_Compare</Slice_CustomCompare>
        </modules>
      </args>
    </catalog>
  </routers>
</frontend>

and in app/code/local/Slice/CustomCompare/controllers/Product/CompareController.php I have:

<?php
require_once(Mage::getModuleDir('controllers','Mage_Catalog').DS.'CompareController.php');
class Slice_CustomCompare_Product_CompareController extends Mage_Catalog_Product_CompareController {

  public function removeAction() {
    Mage::log('Slice_CustomCompare_Product_CompareController:removeAction');
  }
}
?>

When I try to remove a product from the compare list I don't get Slice_CustomCompare_Product_CompareController:removeAction in my logs. Instead I get output that I added to the method in the core code. What am I missing? Why is my rewrite not working?

Best Answer

Your config section should look like this:

<frontend>
  <routers>
    <catalog>
      <args>
        <modules>
          <Caupo_CustomCompare before="Mage_Catalog">Caupo_CustomCompare</Caupo_CustomCompare>
        </modules>
      </args>
    </catalog>
  </routers>
</frontend>

This means that Magento looks for a controller in Caupo/CustomCompare/controllers before looking in Mage/Catalog/controllers.
The controller path must follow the same file path as the original controller (but looks like you already have that:)

Mage /Catalog      /controllers/Product/CompareController.php - original controller
Caupo/CustomCompare/controllers/Product/CompareController.php - your controller
\____/\____________/\________________________________________/
   |       |                          |
   |       |                          |---Common path to controller
   |       |--- Module
   |---- Namespace

And your require statement should be like this:

require_once('Mage/Catalog/controllers/Product/CompareController.php');

I think it works with this also but I'm not sure:

require_once(Mage::getModuleDir('controllers','Mage_Catalog').DS.'controllers'.DS.'Product'.DS.'CompareController.php');