× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Resolve Class Rewrite Conflicts in Magento

Ryan Street
Dec 01, 2015
<p>No two modules can rewrite the same class. The last one in always wins. What I mean by “in” is that the last one to have their configuration loaded is the one who will rewrite the class. All previous entries will be overwritten. This is known as a rewrite conflict.</p> <p>In order to fix this issue, you can declare a dependency on another module. This forces Magento to merge your configuration after the module you depend on. This allows you to perform a rewrite.</p> <p>Now, instead of rewriting the original class, rewrite the module’s class you specified as a dependency. Normal inheritance should work from that point.</p> <p>Here is an example. Module1 and Module2 are both attempting to rewrite <code>Mage_Catalog_Model_Product</code> . Module1, specifies a dependency on Module2 like this:</p> <pre><code class="language-xml">&lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;config&gt; &lt;modules&gt; &lt;Namespace_Module1&gt; &lt;active&gt;true&lt;/active&gt; &lt;codePool&gt;local&lt;/codePool&gt; &lt;depends&gt; &lt;Namespace_Module2 /&gt; &lt;/depends&gt; &lt;/Namespace_Module1&gt; &lt;/modules&gt; &lt;/config&gt;</code></pre> <p>Now specify the configuration for the rewrite in Module1.</p> <pre><code class="language-xml">&lt;?xml version="1.0"?&gt; &lt;config&gt; &lt;global&gt; &lt;models&gt; &lt;catalog&gt; &lt;rewrite&gt; &lt;product&gt;Namespace_Module2_Model_Catalog_Product&lt;/product&gt; &lt;/rewrite&gt; &lt;/catalog&gt; &lt;/models&gt; &lt;/global&gt; &lt;/config&gt; </code></pre> <p>Now, when creating your rewrite for Module1, extend Module2’s class instead of the original Product class.</p> <pre><code class="language-php">&lt;?php class Namespace_Module2_Model_Catalog_Product extends Namespace_Module1_Model_Catalog_Product // this class extends Mage_Catalog_Model_Product { } </code></pre> <p>Of course, this is not a full proof solution. You may have to do some tweaking to ensure both modules’ customizations are getting through.</p>
comments powered by Disqus