4

I want to restrict the visitors for a single store in multistore website. If they are not logged in, redirect them to login page. They can also create account and login after they visit the website. If anyone have idea please let me know.

5

You can create a module for that.

First you need to declare an observer on the controller_action_predispatch event: app/code/local/Vendor/Module/etc/config.xml

<config>
    <modules>
        <Vendor_Module>
            <version>0.0.1</version>
        </Vendor_Module>
    </modules>
    <global>
        <models>
            <module>Vendor_Module_Model</module>
        </models>
    </global>
    <frontend>
        <events>
            <controller_action_predispatch>
                <observers>
                    <vendor_module_controller_action_predispatch>
                        <type>singleton</type>
                        <class>module/observer</class>
                        <method>preDispatch</method>
                    </vendor_module_controller_action_predispatch>
                </observers>
            </controller_action_predispatch>
        </events>
    </frontend>
</config>

Then in app/code/local/Vendor/Module/Model/Observer.php please note that you need to replace STORE_ID with the id of the store you want to implement that feature :

<?php

class Vendor_Module_Model_Observer {

    public function preDispatch(Varien_Event_Observer $observer)
    {
        $controller = $observer->getControllerAction();

        if (Mage::app()->getStore()->getId() == STORE_ID) {

            if ('customer' === $controller->getRouteName()) {
                return $this;
            }

            if (!Mage::getSingleton('customer/session')->authenticate($controller)) {
                $controller->setFlag('', 'no-dispatch', true);
            }
        }
    }

}

And app/etc/modules/Vendor_Module.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Vendor_Module>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Magento_Customer/>
            </depends>
        </Vendor_Module>
    </modules>
</config>
  • 1
    Thanks It works but according to my question we have to modify the observer function like Maurius Posted – Jack Jun 16 '16 at 13:16
2

You can create an observer for the event controller_action_predispatch.
The observer method can look like this:

public function redirectNotLogged(Varien_Event_Observer $observer)
{
    $storeId = Mage::app()->getStore()->getId(); 
    $restrictedStoreId = your restricted store id here. It can even come from a config setting.
    if ($storeId == $restrictedStoreId) {
    $action = strtolower(Mage::app()->getRequest()->getActionName());
        $controller = strtolower(Mage::app()->getRequest()->getControllerName());
        $openActions = array(
            'create',
            'createpost',
            'login',
            'loginpost',
            'logoutsuccess',
            'forgotpassword',
            'forgotpasswordpost',
            'resetpassword',
            'resetpasswordpost',
            'confirm',
            'confirmation'
        );
        if ($controller == 'account' && in_array($action, $openActions)) {
            return $this; //if in allowed actions do nothing.
        }
        if(! Mage::helper('customer')->isLoggedIn()){
            Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account/login'));
        }
    }
}
  • Your observer function working thanks for help – Jack Jun 16 '16 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.