Manual activation of the Magento mobile theme

By default switching to magento mobile theme being happened automatically basing on customer user agent (i.e. browser). You can find the details about this in my previous article.

However, in case if you would like to test your mobile theme before publishing, this approach will not work because theme will be visible for all customers with mobile devices. In this article I will describe how to make mobile theme available for testing only for you using special link, for example:

yourstore.com?use_mobile_theme=1

Important: we assume that your mobile theme is located in the same package as the desktop theme, and the name of the theme is "mobile".

First of all, you need to create a new custom module, lets call it Turnkeye_Switcher, there are a lot of tutorials how to create a custom module, so lets describe only module functionality itself.

Step 1. We need to create a module config.

File: app/code/community/Turnkeye/Switcher/etc/config.xml

< ?xml version="1.0"?>
<config>
    <modules>
        <Turnkeye_Switcher>
            <version>0.0.1</version>
        </Turnkeye_Switcher>
    </modules>
    <global>
        <models>
            <core>
                <rewrite>
                    <design_package>Turnkeye_Switcher_RW_Core_Model_Design_Package</design_package>
                </rewrite>
            </core>
        </models>
    </global>
</config>

Step 2. As you see above we rewrite a model responsible for theming, now lets create this class.

File: app/code/community/Turnkeye/Switcher/RW/Core/Model/Design/Package.php

< ?php

class Turnkeye_Switcher_RW_Core_Model_Design_Package extends Mage_Core_Model_Design_Package
{

    protected function _checkUserAgentAgainstRegexps($regexpsConfigPath)
    {
        /**
         * @var $cookie Mage_Core_Model_Cookie
         */
        $cookie = Mage::getSingleton('core/cookie');
        if (Mage::app()->getRequest()) {
            $use_mobile = Mage::app()->getRequest()->getParam('use_mobile_version', null);
            if (!is_null($use_mobile)) {
                $cookie->set('use_mobile_version', $use_mobile, true);
                if (!$use_mobile) {
                    return false;
                }

                return 'mobile';
            }

            if (!$cookie->get('use_mobile_version')) {
                return false;
            }

            return mage::getStoreConfig('design/mobile/theme');
        }

        return parent::_checkUserAgentAgainstRegexps($regexpsConfigPath);
    }

    /**
     * check is it mobile
     *
     * @param $regexpsConfigPath
     * @return mixed
     */

    public function checkUserAgentAgainstRegexps()
    {
        if (Mage::app()->getRequest()) {
            $use_mobile = Mage::app()->getRequest()->getParam('use_mobile_version', null);
            if (!is_null($use_mobile)) {
                return (bool)$use_mobile;
            }
        }

        $cookie = Mage::getSingleton('core/cookie');
        if ($cookie->get('use_mobile_version')) {
            return true;
        }

        return false;
    }

}

That is all, after that you can see your mobile theme using this link:
http://yourstore.com?use_mobile_theme=1

And vise versa, the mobile theme view can be disabled using this command:
http://yourstore.com?use_mobile_theme=0

Important: As you see we rewrite a core class, it is not very good. I recommend to remove the module after the mobile theme testing.