Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento: Disable module for any particular store

Suppose, I have 3 stores.

I want to disable a module in Store 2. I only want it to be enabled in Store 1 and Store 3.

I see that I can do it by:-

  • Going to System -> Configuration -> Advanced

  • Selecting desired store from Current Configuration Scope dropdown list.

But this does not work fully.

And, I also don't want to check store in the module code itself or create system configuration field for the module to check/uncheck store to enable/disable.

What I am expecting is by adding some code in app/etc/modules/MyNamespace_MyModule.xml. Can we do it this way?

like image 800
Mukesh Chapagain Avatar asked May 11 '11 09:05

Mukesh Chapagain


People also ask

How do you completely disable a module in Magento?

Although the title might sound silly and so easy, there are lot of people having difficulties with this. At first turning Magento module can be easy as going trough System Configuration > Current Configuration Scope > Advanced > Advanced > Disable Module Output.

How do I disable a store in Magento 2?

Steps to Enable/Disable Demo Store Notice in Magento 2Log in to Magento 2 admin panel. Navigate to Content -> Design -> Configuration. In the grid, find the store view that you want to configure and click Edit in the Action column. Expand the HTML Head section, under Other Settings.


2 Answers

To disable a module on the store scope, I've found it's possible to do it like this:

Move app/code/core/Mage/Core/Model/Config.php to app/code/local/Mage/Core/Model/Config.php

Inside Config.php find the method "loadModulesConfiguration" Don't change anything, but add the following code to make the method look like this.

public function loadModulesConfiguration($fileName, $mergeToObject = null, $mergeModel=null)
{
    $disableLocalModules = !$this->_canUseLocalModules();

    if ($mergeToObject === null) {
        $mergeToObject = clone $this->_prototype;
        $mergeToObject->loadString('<config/>');
    }
    if ($mergeModel === null) {
        $mergeModel = clone $this->_prototype;
    }
    $modules = $this->getNode('modules')->children();
    foreach ($modules as $modName=>$module) {
        if ($module->is('active')) {
            // Begin additional code
            if((bool)$module->restricted) {
                $restricted = explode(',', (string)$module->restricted);
                $runCode = (isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : 'default');
                if(in_array($runCode, $restricted)) {
                    continue;
                }
            }
            // End additional code
            if ($disableLocalModules && ('local' === (string)$module->codePool)) {
                continue;
            }
            if (!is_array($fileName)) {
                $fileName = array($fileName);
            }

            foreach ($fileName as $configFile) {
                $configFile = $this->getModuleDir('etc', $modName).DS.$configFile;
                if ($mergeModel->loadFile($configFile)) {
                    $mergeToObject->extend($mergeModel, true);
                }
            }
        }
    }
    return $mergeToObject;
}

The new code will cause the method to also check for a new node in the module xml file, <restricted>. If the node exists, the value would be a comma separated list of store codes that you do NOT want the module to load on. If you have multiple stores, the $_SERVER variable "MAGE_RUN_CODE" should be set with the current store code. If it's not set, the script will fallback to assuming the store code is "default" which is what it is by default unless for some bizarre reason you decide to change that in the backend.

A modules xml file could then look like this:

<?xml version="1.0"?>
<config>
    <modules>
        <MyPackage_MyModule>
            <active>false</active>
            <restricted>mystore1,mystore4,mystore5</restricted>
            <codePool>local</codePool>
        </MyPackage_MyModule>
    </modules>
</config>

With this, the module will not even load while on the stores with a store code of mystore1, mystore4, or mystore5. The <restricted> tag is entirely optional, if you omit it the module will load as it normally would.

like image 176
Eric Hainer Avatar answered Sep 18 '22 15:09

Eric Hainer


This configuration just disables module output in layout for frontend, but module controllers, event observers, admin pages, etc still working.

Also don't forget to specify your module name in layout files definition, otherwise all the layout file content will be loaded for a particular store:

<config>
   <layout>
       <module_alias module="Module_Name">
           <file>yourlayoutfile.xml</file>
       </module_alias>
   </layout>
</config>

If you are developing a module and want to disable full its functionality on the frontent for a particular store, then you should create a configuration field of "Yes/No" type and check its value via Mage::getStoreConfigFlag('config/field/path') in your module code.

like image 23
Ivan Chepurnyi Avatar answered Sep 17 '22 15:09

Ivan Chepurnyi