How to override a magento core module

Posted On //
Yesterday I have to extend a magento core module, and I decided to share the way with everyone. In my project, the products and categories come from the company's own system. I have written a module that synchronize the magento database and the company's database, with magento product api. Now I have to make a new attribute, that control the product is salable or not. These product have to show on frontend, but the customer can not put it into his or her cart. I have looked after what control the visibility of the cart link. In my case, I have a myTheme named them so the path is: app/design/frontend/default/myTheme/template/catalog/product/list.phtml Somewhere in the template there are lines like these:
isSaleable()): ?>
  

.
.
.
You can see, we have to override product.isSalable() method. In my case this method is in app/code/core/Mage/Catalog/Model/Product/Type/Abstract.php. In an ideal way we should to override this class, but in my experience magento does not allow to override an abstract class. My products are simple products, so I have to find descendant, that extends this abstract class, and override. This was Simple.php in the same directory. You can see it is an almost empty class, just extends abstract class:
class Mage_Catalog_Model_Product_Type_Simple extends Mage_Catalog_Model_Product_Type_Abstract
{
}
Ok. We have to create my own module, that override only this class. I have created app/code/local/Chromy/Catalog directory. In this directory we have two another directory: etc and Model. In etc directory we have an xml: config.xml. In my case it looks like:

    
        
        0.1.0
        
    
    
        
            
                
                    Chromy_Catalog_Model_Product_Type_Simple
                
            
        
    
    
        
    
 

After this I have created a php file in the Model directory: Product/Type/Simple.php whit this content:
class Vision_Catalog_Model_Product_Type_Simple extends Mage_Catalog_Model_Product_Type_Simple {
public function isSalable($product = null)
{
$salable = parent::isSalable($product);

if ($salable && $this->getProduct($product)->hasData('is_under_pricing')) {
$salable = !$this->getProduct($product)->getData('is_under_pricing');
}

return $salable;
}

}
As you see I have created a new attribute in magento backend named "is_under_princing". I don't want to do it in this post, you can find a lot of article on net about this. So the next and last thing we have to do, is config the magento system to use this module instead of core. In app/etc/modules directory I have created a file named: Chromy_Catalog.xml:

            
        
            true
            local
        
    

And after some cache clean voila. The new module is working. Good luck!
[Read more]