Magento snippet for switching between websites

Magento snippet for switching between websites

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. :D
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 ;)

9
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

Tomas Novoselic

Team Leader / Senior Developer

At Inchoo, Tomas is a Team leader and Senior PHP developer. He handles Magento modifications at any level. He also works closely with clients on Magento projects of any size and difficulty.

Other posts from this author

Discussion 9 Comments

Add Comment
  1. Ben

    WHERE WERE YOU ON FRIDAY WHEN I WAS DOING THIS??? :-P

  2. Toni Anicic

    @Ben,

    We waited for you to solve this and then used our spy in your company to steal the code and publish it here :D

  3. Is there a way to switch in code between Store Views in the same website?

  4. @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 :)

  5. Linda

    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!

  6. Linda

    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?

  7. Linda

    Apologies, I have found that information. I hard-coded the main link into the header.phtml for the h1 logo class and that worked.

  8. Mohan

    hai let me know how to set multiple web store in localhost

  9. Is there anything else I need to do to get this working besides copying and pasting the code into my theme file? I added it to the header.phtml file of my template, and it makes the entire page into a blank white screen.

    I’m thinking that my multiple websites might not be set up correctly. Any suggestions?

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top