Magento: Only registered users can view store

Here is a nice simple way to make your store only viewable by registered users. And if someone is not registered and tried to view any page they will be taken to the registration form. This can be changes so a guest is shown any page you want.

this magento modification goes well with my previous post of restricting user registration to only certain email domains. So check that our after this.

in your design package you have in your page folder ( Magento Folder/app/design/frontend/*yourdesignpackage*/default/template/page ) different template files for each kind of page.

Example: for a page with one column on the left and the main column on the right you have the file “2columns-left.phtml”

remember that if you don’t have them all in your design package folder to copy over the default ones from the base package folder: Magento Folder/app/design/frontend/base/default/template/page

The default files will be something like:

1column.phtml
2columns-left.phtml
2columns-right.phtml
3columns.phtml

if you have more it may be worth doing them too.

Now we want to have these type of pages restricted for only loged in registered users.
so at the beginning of each file add this php code:

what this is doing is telling magento that if the current visitor is not logged in, redirect them to the customer login page.

but if you view your site right now and are not logged in you will be in an endless redirect. That is because the customer login page has this redirect code at the top too. So what we need to do is add a new template file the guest with no redirect code.

So again in your Magento Folder/app/design/frontend/*yourdesignpackage*/default/template/page Folder duplicate the file 1column.phtml. The reason i am doing this file is because by default this is the template file used for the register,login and logout page.

Rename your new file something like 1column-guest.phtml to identify it easily.
IMPORTANT: Open it up and remove ( if there ) the code we entered above at the beginning. As this is for non logged in visitors who wont be redirected for certain pages.

So now we need to assign that template file to some pages, which non logged in viewers can see.
In your customer layout folder ( Magento Folder/app/design/frontend/*yourdesignpackage*/default/layout/customer.xml ) look for:

or

and under that find “page/1column.phtml” and replace with “page/1column-guest.phtml”

do the same under the following sections:

if non of those are there look into your base default layout folder and copy them over.

all done. now all guests have to login or register to view your magento store.
You can even get fancy and strip out all the un-needed design from the guest template file so it is just the login fields and button.

But what if you want to assign that template file to a CMS Page you have. Maybe your about page.

Simple… in your local file: Magento Folder/app/etc/local.xml

find the following line:

and add just before it:

and now you will see it added to the dropdowm menu under the design tab when editing a CMS page.

remember to refresh you magento cache if you have it running.