2

I have a store using Magento 1.7 and I need to set a login/registration page as the first page that is shown before buying anything, so that it is mandatory to log in.

I edited cms.xml, under app/design/frontend/base/default/layout/cms.xml, and managed to show login/registration in the homepage:

<cms_page translate="label">
    <label>CMS Pages (All)</label>
    <reference name="content">
        <block type="core/template" name="page_content_heading" template="cms/content_heading.phtml"/>
        <block type="page/html_wrapper" name="cms.wrapper" translate="label">
            <label>CMS Content Wrapper</label>
            <action method="setElementClass"><value>std</value></action>
            <block type="customer/form_login" name="customer_form_login" template="customer/form/login.phtml"/>
            <block type="cms/page" name="cms_page"/>
        </block>
    </reference>
</cms_page>

I also added a line in customer.xml under design/frontend/default/custom_theme/layout/customer.xml for removing the login/registration boxes after customer login, so that it looks like:

<customer_logged_in>
    <reference name="top.links">
        <remove name="customer_form_login"></remove>
(...)

But I still can navigate through all the contents of my store without loging in; how should I do in order that the first page only contains login/registration? I think that is something related to layouts and phtml, but I am a bit confused and don't know which file I should edit or if I should include some other files.

Any help is much appreciated :-)

5

For this you can redirect your customer (not logged in ) to login page. you can achieve this by event observer.

Follow below steps to create custom module

Create File with name Namespace_Module.xml at location app/etc/modules/ with code

<config>
  <modules>
    <Namespace_Module>
      <active>true</active>
        <codePool>local</codePool>
    </Namespace_Module>
  </modules>
</config>

create config.xml at app/code/local/Namespace/Module/etc directory and add the following xml code:

<?xml version="1.0" encoding="utf-8"?>
<config>
    <modules>
        <Namespace_Module>
            <version>0.1.0</version>
        </Namespace_Module>
    </modules>
    <global>
        <helpers>
            <namespace_module>
                <class>Namespace_Module_Helper</class>
            </namespace_module>
        </helpers>
        <models>
            <namespace_module>
                <class>Namespace_Module_Model</class>
            </namespace_module>
        </models>
    </global>
    <frontend>
        <events>
            <controller_action_predispatch>
                <observers>
                    <namespace_module>
                        <class>Namespace_Module_Model_Observer</class>
                        <method>checkForLogin</method>
                    </namespace_module>
                </observers>
            </controller_action_predispatch>
        </events>
    </frontend>
</config>

Create a Observer.php in your Module’s Model/ directory with below code

class Namespace_Module_Model_Observer extends Varien_Object{

public function checkForLogin(Varien_Event_Observer $observer)
{
  $allow = array('customer_account_login','customer_account_createpost','customer_account_create','customer_account_forgotpassword','customer_account_resetpassword','customer_account_loginpost','customer_account_forgotpasswordpost','customer_account_resetpasswordpost');
   if(!Mage::getSingleton('customer/session')->isLoggedIn() && !in_array(strtolower($observer->getControllerAction()->getFullActionName()),$allow) ) {
        Mage::app()->getResponse()->setRedirect(Mage::getUrl('customer/account/login'))->sendResponse();
   exit;
   }
 }
}

Create file Data.php at app/code/local/Namespace/Module/Helper/ with code

class Namespace_Module_Helper_Data extends Mage_Core_Helper_Abstract
{
}
  • Where exactly should I add this code? I mean I guess there is a module for login in Magento, but I am not sure. I see that there is customer in the core code. Sorry I quite new to Magento and still don't know about it enough – LeCodeur Dec 20 '16 at 15:58
  • Do not edit core files, create a new custom module – Piyush Dec 20 '16 at 16:31
  • Yes, actually that's what I tried. I created a new module with the two files you say. I also added the corresponding xml in app/etc/modules, and I can see from admin->System->Advanced->Advanced that the module is active. But I can't see any effects, it doesn't seem to work. Is this the proper way to proceed? – LeCodeur Dec 21 '16 at 11:12
  • 1
    please check my updated answer , you can create whole custom module with that – Piyush Dec 21 '16 at 11:32
1

We have a similar situation here...we need people to sign in before they buy, but instead of blocking their access to the website, we just hide the price if the customer is not logged in yet.

This was a better solution for us because customer can see which products we have, what promotions are going on, etc...and then when they want to buy, they need to sign in and just after that the "Add To Cart" button shows up.

We use this extension, but you could use any other option: https://amasty.com/magento-hide-price.html

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.