Magento snippet for switching between websites

8 Comments 1st MAR 2010 | Posted by Tomas Novoselic in E-Commerce, Magento

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

If you like what you read, please share it.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Yahoo! Bookmarks
  • Reddit
  • Technorati
  • Twitter
  • StumbleUpon
  • LinkedIn
  • Netvibes
  • NewsVine
  • Sphinn
  • Tumblr
  • Posterous

To post code in comments, place your code inside [code] and [/code] tags.

There are 8 comments (Add Yours +)

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

  • Toni Anicic Says

    @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

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

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

  • 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!

  • 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?

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

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

Leave a Comment

Please wrap all source codes with [code][/code] tags.
Magento Design and Development | Magento SEO | iPhone Application Development Web Application Development with ZEND | WordPress Ecommerce | WordPress development
Sitemap

Inchoo - webappsolutions | 2009