سؤال

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 :-)

هل كانت مفيدة؟

المحلول

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
{
}

نصائح أخرى

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top