Thursday, April 18, 2013

Magento for Designers: Multi-Store Functionality

Magento for Designers: Multi-Store Functionality

Magento ships with a number of very powerful features under the hood that go mostly unnoticed by the folks that install and use it on a daily basis. The feature we're looking at today, the multi store functionality, happens to be one of those; and arguably, it's one of the most advanced ones. There isn't a general vernacular for this feature so I'll be just calling it multi store from now on.

Using this multi store functionality, we can use a single Magento installation and use it to power any number [within technical limits, of course] of stores or websites. This is definitely a godsend for stores that would like to sell products on different domains but would like to have a unified administrative centre. If you need even more granular control, you can set up different, separate stores under the same domain to differentiate between items even better.

The Terminologies Involved

Magento's documentation regarding this topic has a pretty decent explanation of the terminologies so I'll make a concise introduction here:

  • Website: Container for stores and their related views.
  • Store
  • Exactly what you'd expect but in the abstract sense. You can control your catalog through your store but nothing will be rendered on screen.
  • Store View

These enable the actual rendering of your store. If you want to have a multi-lingual store, you'd be modifying the store view, not the store itself.
All these terminologies will start making more sense once you see them in action. For now, we'll move on.

Step 1: Initial Preparations

First up, if you're on your local server, you probably entered nothing for the URL during the installation and thus Magento has the default value stored. We'll need to change to point it to a concrete location. We’ll do it quickly right now.

Magento for Designers: Multi-Store Functionality

Go to System -> Configuration and under the Web category, change the base URL value to reflect your local set up. I have mine working under a predictably named Magento directory thus my URL. Please remember to add the trailing slash; otherwise, Magento will spaz out.

With that out of the way, we can get to the next step: creating a root category for the new store. You may ask why. My reasoning is that with the additional stores, the number of prospective categories will increase as well. With organizational structure in mind, it makes sense to keep each store's categories in a separate root category. We'll be creating one now.

Magento for Designers: Multi-Store Functionality

Just give a name to the new category and make sure it's set to active.

Magento for Designers: Multi-Store Functionality

Finally, set the is anchor value to yes as well. This is important.

Step 2: Setting up the New Site

First, we'll set up the new site in Magento's back end.

Magento for Designers: Multi-Store Functionality

Magento for Designers: Multi-Store Functionality

In the resulting screen, key in a name for the site as well as a code for it. Neither is really important, other than that both need to be unique.

Step 3: Setting up the New Store

Now we'll move on creating a new store.

Magento for Designers: Multi-Store Functionality

Magento for Designers: Multi-Store Functionality

This should be self-explanatory. We set the website and category to the ones we created a few steps back. The name of the store is just for human readability, so feel free to name it as you like. I know having the same name for the store and category is a little confusing here. Feel free to name it as you like in your installations. There's no concrete naming scheme here for you to follow.

Step 4: Setting up the New Store View

Magento for Designers: Multi-Store Functionality

Magento for Designers: Multi-Store Functionality

Magento for Designers: Multi-Store Functionality

Again, these steps should be fairly self-explanatory. We select the appropriate website and store for the view along with keying in a name and code for it. Additionally, remember to set its state to enabled. It may seem obvious, but we tend to forget it, so I thought a quick "heads up" was in order.

With these, most of the back end work in Magento comes to an end. We'll need to revisit this later so for now we'll focus on preparing the new domain.

Step 5: Prepping the New Domain

Ok, to be honest, this is the easiest step there is. Just FTP into the server with the working Magento installation and copy the index.php file as well as the .htaccess file over to the new domain.

Magento for Designers: Multi-Store Functionality

Open up index.php and look for the following code at around line 45,

$mageFilename = '$mageFilename = 'app/Mage.php';';

Change it to the following.

$mageFilename = '../magento/app/Mage.php';

Remember to point it to the Mage.php file of the working installation. Both my sites run under sub folders in my server so I just ask it to go a directory up, go into the magento folder, into the app folder and then access the required file. Depending on your server set up, this may vary, for example, your main installation may be in your account root while the other site may be under a parked domain. In that case, the following code will do.

Step 6: Making it all Work – the HTACCESS file

Now open up the copied htaccess file and append the following to it.

SetEnvIf Host .*base.* MAGE_RUN_CODE="base";  
SetEnvIf Host .*magento_site_2.* MAGE_RUN_TYPE="magento_site_2";

Simple as that! Note that we've used the website code we keyed in earlier. If you don't remember it, just go back in and copy it over. This is very important so make sure typos are avoided.

Step 7: Final Steps

We're pretty much done now. Just the final few steps before we get everything working.

Go to System -> Configuration and choose the web tab.

Magento for Designers: Multi-Store Functionality

As shown in the picture above, please change the redirect to base URL to no.

Magento for Designers: Multi-Store Functionality

If you've noticed that the select element on the left has extra options now, you get a cookie! Now that we have different stores, we can now change their settings and store policies on a per store basis.

Access our second store's view and choose the web tab.

Magento for Designers: Multi-Store Functionality

Uncheck the use website checkbox and change the base URL to the URL of your second domain. For this article, I did everything on my local server so I have a sub-folder imaginatively named magento-2 as a container for my second store front.

Click on save. And that's about it. Go to your second domain and start browsing through your catalog!