Magento rewriting block, helper and models plus override issues

Posted by Damodar Bashyal on January 06, 2014

 

Magento is an awesome ecommerce web application but sometimes it gives a real headache. As I am creating mobile theme, the requirement was, it should load desktop version theme on everything other than mobile devices. But the real challenge was when user decides to load desktop version even on mobile devices it should load desktop version not mobile template.

This project doesn't have separate links for mobile and desktop. So it was a little challenging for me.

After googling around found one solution on stackexchange site (http://magento.stackexchange.com/a/9795) which suggested to override function _checkUserAgentAgainstRegexps in core/design/package file (app/code/core/Mage/Core/Model/Design/Package.php) and use cookie to check if user wants to load desktop or mobile theme.

I found one obstacle overriding that file. Another extension (OpsWay_Cloudfront) was already overriding it. So, Magento was ignoring my override. Then, I added that extension as dependency for my extension.

file: app/etc/modules/DesignSwitcher.xml

<?xml version="1.0"?>
<config>
    <modules>
        <DesignSwitcher>
            <active>true</active>
            <codePool>community</codePool>
            <depends>
                <Mage_Core/>
                <Mage_Catalog/>
                <OpsWay_Cloudfront/> <!-- depends on this extension, so that extension is loaded first and this one -->
            </depends>
        </DesignSwitcher>
    </modules>
</config>

Awesome, after that update my override was working perfectly. But there was another issue after my override. OpsWay_Cloudfront was loaded first and mine second, so it ignored override of OpsWay_Cloudfront extension. So to fix this issue, instead of extending my class to Mage_Core_Model_Design_Package I extended to OpsWay_Cloudfront_Model_Design_Package as it was extending to core already.

So previously it was:

class DesignSwitcher_Model_Core_Design_Package extends Mage_Core_Model_Design_Package

I updated it to:

class DesignSwitcher_Model_Core_Design_Package extends OpsWay_Cloudfront_Model_Design_Package

Now Magento was loading my Model overrides plus that extension's override too.

 
not published on website


QR Code: Magento rewriting block, helper and models plus override issues