Extending Core Magento Model: A Comprehensive Guide
In this post, we’ll guide you through extending the core Mage_Catalog_Model_Product_Visibility in Magento using a custom extension. This is a common requirement when you need to modify the default product visibility behavior without directly editing core files.
Why Extend Instead of Editing Core Files?
Modifying core files can lead to issues during upgrades or updates. It also violates best practices for maintaining a clean, upgradeable codebase. By creating an extension, we ensure our changes are isolated and don’t interfere with the default Magento functionality.
Creating the Extension: Step-by-Step
1. Configuring the Module Definition
First, create your module definition in app/etc/modules/CustomCode_InvisibleProducts.xml:
<?xml version="1.0"?>
<config>
<modules>
<CustomCode_InvisibleProducts>
<active>true</active>
<codePool>local</codePool>
</CustomCode_InvisibleProducts>
</modules>
</config>
This file configures the module’s activation status and code pool.
2. Configuring Model Rewrite
Next, configure your extension in app/code/local/CustomCode/InvisibleProducts/etc/config.xml:
<?xml version="1.0"?>
<config>
<modules>
<CustomCode_InvisibleProducts>
<version>0.1.0</version>
</CustomCode_InvisibleProducts>
</modules>
<global>
<models>
<customcode_invisibleproducts>
<class>CustomCode_InvisibleProducts_Model</class>
</customcode_invisibleproducts>
<catalog>
<rewrite>
<product_visibility>CustomCode_InvisibleProducts_Model_Catalog_Product_Visibility</product_visibility>
</rewrite>
</catalog>
</models>
</global>
</config>
Here, we define our custom model namespace and rewrite the Mage_Catalog_Model_Product_Visibility class.
3. Implementing the Model Rewrite
In app/code/local/CustomCode/InvisibleProducts/Model/Catalog/Product/Visibility.php, implement your rewritten model:
class CustomCode_InvisibleProducts_Model_Catalog_Product_Visibility extends Mage_Catalog_Model_Product_Visibility
{
// Your customizations go here...
}
4. Implementing the Customization
To achieve the desired functionality of adding self::VISIBILITY_NOT_VISIBLE to the visible site IDs, implement it in your extended model:
public function getVisibleInSiteIds()
{
$visibleIds = parent::getVisibleInSiteIds();
return array_merge($visibleIds, [self::VISIBILITY_NOT_VISIBLE]);
}
This code merges the existing visible site IDs with VISIBILITY_NOT_VISIBLE.
Common Pitfalls and Troubleshooting
If your extension isn’t working as expected, check the following:
- Ensure you’ve flushed all caches in Magento (
System > Cache Management). - Verify that your module is enabled in
System > Configuration > Advanced. - Check if there are any typos or syntax errors in your XML configuration files.
Conclusion: Extending Core Magento Models Cleanly
By following this guide, you should now be able to extend the core Mage_Catalog_Model_Product_Visibility model without directly editing core files. This approach ensures your changes remain isolated and upgrade-safe.
Remember that working with extensions requires a good understanding of Magento’s module structure and XML configuration. If you encounter further issues, feel free to explore our Magento Development Resources for more in-depth tutorials and troubleshooting guides.
Happy coding!