Magento Extension Conflicts

In this blog post we will see how to detect magento extension conflicts and how to resolve them.

What is an Extension Conflict

We know we can rewrite models, blocks, helpers in magento. Now suppose there are two different magento extension which rewrite the same model or block or helper. This results in conflict because only one of the two rewrite will be used by magento not both. Here you can see how rewrites work

Detecting Extension Conflict

To detect extension conflict there few free and paid extension available online. But lets see how we can write a small code snippet do this directly

In the previous blog posts we have seen how magento loads magento configuration files. The function is “loadModulesConfiguration()” in Mage_Core_Model_Config class

We will add the code

                      $xml = $mergeModel->getXpath('global/*/*/rewrite');
                        foreach ($xml as $x) {
                            $key = '';
                            $y = $x->getParent()->getParent();
                            $key .= $y->getName() . "_";
                            $key .= $y->children()->getName() . "_";
                            $key .= $x->children()->getName() . "_";
                            if (in_array($key, array_keys($keys))) {
                                echo "$key rewrite conflict in file $configFile and {$keys[$key]}<Br/>";
                            } else {
                                $keys[$key] = $configFile;

just after the line

if ($mergeModel->loadFile($configFile)) {

Here we are basically checking all the global rewrites defined using xpath and keeping a list of it in an array. Whenever a duplication rewrite is encountered we print it.

Fixing Extension Conflicts

Lets see how to fix conflicts. Lets assume there are two rewrites

ClassA overrides Mage_Core_ClassX
ClassB overrides Mage_Core_ClassX

To fix this follow the procedure below

1. Remove the rewrite code in config.xml from any one of the modules (just comment it out). Lets say we remove the rewrite code in config.xml for ClassA
2. Next we will make ClassB extend ClassA
So finally ClassB extends ClassA , ClassA extends Mage_Core_ClassX

This helps to solve the conflict problem. But there is a big catch here, if ClassA and ClassB have defined same function inside their classes then the code written in ClassA will no longer be used. In such cases you need to manually merge the code written in ClassA and ClassB. To explain this better, lets assume
ClassA has a function getCustomerName() in it where some code is written.
ClassB also has the same function getCustomerName(), since ClassB now extends ClassA the code written in ClassA will become redundant. So you will have to copy/merge code from ClassA and put it in ClassB’s getCustomerName() function.

Tip: Above i had mentioned that you need to comment out the rewrite written in config.xml. Another trick to achive this is using in app/etc/modules/ file of the module. You can change the sort of module loading using “depends” and which ever module loads later that rewrite will be taken by magento.
So in above case we could do ModuleB depends ModuleA in the xml file, and we won’t have to comment out anything from the config.xml file. This is especially useful when ModuleA is a 3rd party module and ModuleB is a module which you are developing.