Close Magento store for maintenance

Sometimes we need to close Magento store for maintenance (for example, during store transfer to another hosting or performing Magento upgrade), and in this case, the web store will be closed for several hours.

As you may know, Magento already has a useful default way to close the store for maintenance. You only need to add an empty file into the root Magento directory and call the file “maintenance.flag”. After that, your Magento store will be closed for all users, customers and even admins. Take a look at index.php:

$maintenanceFile = 'maintenance.flag';

...

if (file_exists($maintenanceFile)) {
    include_once dirname(__FILE__) . '/errors/503.php';
    exit;
}

As a result, all users will see the default Magento maintenance page. You may want to customize this page to make it more user-friendly.

maintenance_flag_page

Pay attention that the web store will be closed for you too – you will not be able to get the access to the website. It is quite inconvenient for admins.

Nevertheless, you can allow the access to the web store for some IP addresses. And for this you just need to modify some parts of the code in index.php.

// add this code snippets
$ip = $_SERVER['REMOTE_ADDR']; 
$allowed = array("127.0.0.1","10.10.10.10","100.100.100.100"); //allowed IPs

//and change check code part

if (!in_array($ip, $allowed) && file_exists($maintenanceFile)) {
    include_once dirname(__FILE__) . '/errors/503.php';
    exit;
}

After these code changes you will get a page with “503 Service Unavailable” error, and it will be output with the maintenance.flag file and only for users whose IP addresses are not added to our allowed array with the IPs. But if your store needs such modifications quite often, this solution will be quite inconvenient.

That is why, in this article, we create a small extension to close Magento store for maintenance. The extension file structure:

maintains-tree

Below we are describing each file, so then it will be easy for you to use this extension on your web store. Let’s start from the main config file. This code enables extension and specifies a code pool, path: app/etc/modules.

<?xml version="1.0"?>
<config>
    <modules>
        <Atwix_Maintenance>
            <active>true</active>
            <codePool>local</codePool>
        </Atwix_Maintenance>
    </modules>
</config>

Then, add config.xml to the extension, path: app/code/local/Atwix/Maintenance/etc/config.xml.

<?xml version="1.0"?>
<config>
    <modules>
        <Atwix_Maintenance>
            <version>1.0.0</version>
        </Atwix_Maintenance>
    </modules>
    <global>
        <helpers>
            <atwix_maintenance>
                <class>Atwix_Maintenance_Helper</class>
            </atwix_maintenance>
        </helpers>
        <models>
            <atwix_maintenance>
                <class>Atwix_Maintenance_Model</class>
            </atwix_maintenance>
        </models>
        <events>
            <controller_front_init_before>
                <observers>
                    <atwix_maintenance>
                        <type>singleton</type>
                        <class>Atwix_Maintenance_Model_Observer</class>
                        <method>switchStoreToMaintenance</method>
                    </atwix_maintenance>
                </observers>
            </controller_front_init_before>
        </events>
    </global>
    <adminhtml>
        <acl>
            <resources>
                <admin>
                    <children>
                        <system>
                            <children>
                                <config>
                                    <children>
                                        <atwix>
                                            <title>Atwix Extensions</title>
                                        </atwix>
                                    </children>
                                </config>
                            </children>
                        </system>
                    </children>
                </admin>
            </resources>
        </acl>
    </adminhtml>
    <default>
        <atwix>
            <maintenance_settings>
                <maintenance_enable>0</maintenance_enable>
            </maintenance_settings>
        </atwix>
    </default>
</config>

And add the config tabs to the extension, create system.xml file, path: app/code/local/Atwix/Maintenance/etc/system.xml.

<?xml version="1.0"?>
<config>
    <tabs>
        <atwix translate="label" module="atwix_maintenance">
            <label>Atwix Extensions</label>
            <sort_order>900</sort_order>
        </atwix>
    </tabs>
    <sections>
        <atwix translate="label" module="atwix_maintenance">
            <label>Maintenance</label>
            <tab>atwix</tab>
            <frontend_type>text</frontend_type>
            <sort_order>999</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <maintenance_settings translate="label">
                    <label>Maintenance Settings</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>1</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <maintenance_enable translate="label">
                            <label>Enabled</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>1</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </maintenance_enable>
                        <allow_ips translate="label,comment">
                            <label>Allow IPs</label>
                            <comment><![CDATA[add allow IPs comma separated values. ex: 127.0.0.1,10.10.10.10]]></comment>
                            <frontend_type>textarea</frontend_type>
                            <sort_order>2</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </allow_ips>
                    </fields>
                </maintenance_settings>
            </groups>
        </atwix>
    </sections>
</config>

In this extension we are using a helper to get the config file (path to the helper is app/code/local/Atwix/Maintenance/Helper/Data.php). Here you can see the code of the helper:

<?php
class Atwix_Maintenance_Helper_Data extends Mage_Core_Helper_Abstract
{
    /**
     * get store config data 'atwix/maintenance_settings/maintenance_enable'
     *
     * @return bool
     */
    public function isEnabeled ()
    {
        $enabel = Mage::getStoreConfig('atwix/maintenance_settings/maintenance_enable');
        return $enabel == 1 ? true : false;
    }

    /**
     * get store config data 'atwix/maintenance_settings/allow_ips'
     * and prepare allow IPs array
     *
     * @return array
     */
    public function getAllowIpsArray()
    {
        $ipArray = array();
        $string = Mage::getStoreConfig('atwix/maintenance_settings/allow_ips');
        if (isset($string)) {
            $ipArray = explode(',', $string);
        }
        return $ipArray;
    }
}

Finally, add an observer (path: app/code/local/Atwix/Maintenance/Model/Observer.php).

<?php
class Atwix_Maintenance_Model_Observer extends Varien_Event_Observer
{
    public function switchStoreToMaintenance()
    {
        $myIp = $_SERVER['REMOTE_ADDR'];

        $maintenanceOff = MAGENTO_ROOT . '/maintenance.off';

        $isEnable = Mage::helper('atwix_maintenance')->isEnabeled();
        $allowedIps = Mage::helper('atwix_maintenance')->getAllowIpsArray();

        if ($isEnable === true && !in_array($myIp, $allowedIps) && !file_exists($maintenanceOff)) {

        echo '<div style="margin: auto; width: 800px;">
                 <div style="margin: auto; width: 400px; font-size: 25px" >Service Temporarily Unavailable</div>
                 <div style="font-size: 14px">The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.</div>
              </div>';

        exit;
        }
    }
}

After clearing the cache you will see the config of the extension – go to Magento admin -> System -> Config -> Atwix Extensions -> Maintenance:

maintance-admin

Well, there are two config fields: the first “Enabled” field is to close or open Magento store for users, and the second field “Allow IPs” – there you can add the needed IPs for which the Magento store will be opened.

After enabling the maintenance, all customers (which are not in the list of allowed IPs) will see the maintenance page:

maintains-on

It is important to note that when you set the maintenance extension you should be careful, because if you do not add your IP or add the wrong IP – the web store will be closed for you as well. By the way, you can solve this issue by adding the empty file to the root Magento folder with the name maintenance.off, and after that you can set the access correctly.

Thank you for reading us!