Magento snippet for switching between websites
8 Comments 1st MAR 2010 | Posted by Tomas Novoselic in E-Commerce, Magento

Assuming you have set unique base URL foreach website running on the same Magento installation all you need to switch between those websites is this little snippet.
Just paste it somewhere in your templates.
However, keep in mind that I only tested this to work for my own configuration.
Basicly, what this snippet does is redirect to website’s default store base url.
<!-- BEGIN website switcher -->
<select id="website-changer" onChange="document.location=this.options[selectedIndex].value">
< ?php
$websites = Mage::getModel('core/website')->getCollection();
foreach($websites as $website)
{
$default_store = $website->getDefaultStore();
$url_obj = new Mage_Core_Model_Url();
$default_store_path = $url_obj->getBaseUrl(array('_store'=> $default_store->getCode()));
?>
<option <?php if(strstr($this->helper('core/url')->getCurrentUrl(), $default_store_path)):?>selected="selected"< ?php endif; ?> value="< ?php echo $default_store_path ?>">< ?php echo $website->getName()?></option>
< ?php
}
?>
</select>
<!-- END website switcher -->
This code comes with apsolutely no warranty, however I wish somebody will find it usable. ![]()
If you have any questions, you can ask here.
EDIT: As always WP has some code formating issues, I hope that’s not a problem
To post code in comments, place your code inside [code] and [/code] tags.



















March 1st, 2010 at 14:06
WHERE WERE YOU ON FRIDAY WHEN I WAS DOING THIS???
March 1st, 2010 at 14:13
@Ben,
We waited for you to solve this and then used our spy in your company to steal the code and publish it here
March 2nd, 2010 at 23:00
Is there a way to switch in code between Store Views in the same website?
March 3rd, 2010 at 7:32
@Robert
You can get array of store codes
(ones you enter at “Store View Information” menu in admin panel) using $website->getStoreCodes();
All other is pretty much the same. You will need loop or two, adjust this snippet a little, but in general I think thats the way
July 8th, 2010 at 22:44
Hi, I must say that the Inchoo site makes understanding Magento easier.
When you say to put the above snippet “somewhere in your templates,” could you please be a little more specific?
Thank you so much!
July 9th, 2010 at 13:52
I ended up creating landing pages for my two stores and using text on those pages to switch between stores. Could somebody tell me how to change the link for the header logo to revert to the main index instead of to the current store? I tried using a hard coded link in header.phml, but it didn’t change anything, maybe because of the .htaccess file?
July 9th, 2010 at 13:56
Apologies, I have found that information. I hard-coded the main link into the header.phtml for the h1 logo class and that worked.
August 27th, 2010 at 8:35
hai let me know how to set multiple web store in localhost