Adam B Adam B - 3 months ago 36
PHP Question

Magento: Switch between websites using Mage::run($mageRunCode, $mageRunType)

I am currently using GeoIP in

index.php
to send customers to two different Magento websites (exactly the same store view except that one is CAD and one is USD). It works great. What I want to do now is to allow the customer to switch websites by a dropdown menu. I'm using code for a "website-switcher"

if (count($websites) > 1): ?>
<div class="website-switcher">
<label for="select-website"><?php echo $this->__('Select Store:') ?></label>
<select id="select-website" title="<?php echo $this->__('Select Store') ?>" onchange="location.href=this.value">
<?php foreach ($websites as $website): // print out each website name and code as a dropdown box item ?>
<?php $_selected = $website->getCode() == Mage::app()->getWebsite()->getCode() ? ' selected="selected"' : '' ?>
<option value="<?php echo $website->getDefaultStore()->getCurrentUrl() ?>"<?php echo $_selected ?>><?php echo $this->htmlEscape($website->getName()) ?></option>
<?php endforeach; ?>
</select>
</div>
<?php endif; ?>


Except that this only changes the website for the current page - as soon as you click another link it reverts back to the website chosen by
index.php
.

I want the dropdown to actually change the website (so it would actually clear the cart if the person was in the CAD site with items in the cart and then show them an empty USD cart and USD pricing in the USD site).

How does Magento set the website? It doesn't look like it is setting a cookie and I guess this is done at a deeper level by
Mage::run($mageRunCode,$mageRunType)
? I do not want "Add Store Code To Urls" enabled either, even though this provides a workaround to have this functionality, because it's pretty ugly for the URLs and SEO.

Thanks for any help!

Answer

So I figured this out by adding a cookie to the site when you change your selection on the store switcher. For anyone else wondering how to do this:

The code for your website switcher (stored in app/design/frontend/YOUR THEME/template/page/switch/stores.phtml):

if (count($websites) > 1): ?>
<div class="website-switcher">
<label for="select-website"><?php echo $this->__('Select Store:') ?></label>
<select id="select-website" title="<?php echo $this->__('Select Store') ?>" onchange="setCookie('redirect',this.value)">
<?php foreach ($websites as $website): // print out each website name and code as a dropdown box item ?>
    <?php $_selected = $website->getCode() == Mage::app()->getWebsite()->getCode() ? ' selected="selected"' : '' ?>
    <option value="<?php echo $website->getCode() ?>"<?php echo $_selected ?>><?php echo $this->htmlEscape($website->getName()) ?></option>
<?php endforeach; ?>
</select>

You'll notice that the select box has an onchange event that triggers the js code. It feeds in a name for the cookie ('redirect') and a value. The value is just the store code represented by a call to $website->getCode()

Next create a custom javascript file for the cookie creation in your js folder. Add it to the head of your document with a local.xml action method (google this if you don't know how). In the js file put:

function setCookie(name, value, expires, path, domain, secure)
{
var cookieStr = name + "=" + escape(value) + "; ";
if (expires) {
    var expiresDate = new Date(new Date().getTime() + expires * 24 * 60 * 60 * 1000);
    cookieStr += "expires=" + expiresDate.toGMTString() + "; ";
}


cookieStr += "path=/" + "; ";

if (domain) {
    cookieStr += "domain=" + domain + "; ";
}
if (secure) {
    cookieStr += "secure; ";
}

document.cookie = cookieStr;
document.location.reload();

}

setCookie allows you to create a cookie, feeding in values for name, value etc. In this case I've hardcoded it to have path="/" and expires will just be set to Session.

Then in your index.php put this code:

if (isset($_COOKIE['redirect'])) {
if ($_COOKIE['redirect'] == 'base') {

$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
Mage::run($mageRunCode, $mageRunType);
}
elseif ($_COOKIE['redirect'] == 'ca') {

$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : 'ca';
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
Mage::run($mageRunCode, $mageRunType);
}
else {

$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
Mage::run($mageRunCode, $mageRunType);

}

}

This code checks to see if there is a cookie called "redirect" set. If there is and the value of the cookie is 'base' (the default store) then load the default store. If it's 'ca' load the Canadian store. Then I just put a 'catch' term at the end in case the cookie becomes undefined for some reason then it just loads the default store.

Now I have geolocation by default but if the customer wants to change the store then they can from a dropdown menu and it'll override the geolocation. Hope it helps someone!