1

we have a multistore with many languages. Now the situation exists where 1 language can have a category that the other does not. So store1/en has store1/en/blueshoes and store2/fr does not sell blue shoes. The category is disabled (greyed out) in the category config.

So in short: you click the flag in the header and that store changes language. Only to see that the result is a 404.

I think this can be called a small bug in Magento. Or at least a choice should be give how to solve this in the backend. I can think of three options.

  • don't show the flag / language switcher in this situation
  • change the link to link to the main page
  • keep showing 404 (current behavior)

All these solutions can be created, but only if we can find out that $_lang->getCurrentUrl() cannot find an active CurrentUrl for the current language shop in the foreach statement. So it returns false or we can detect the existance of the URL via another method: does it exist and/or is it activated. In the case that is does not exist or it is not actived: then revert to options 1,2 and 3 above.

Question: how can we find out if a certain $_lang->getCurrentUrl() is not activated/does not exist so we can change the behavior and circumvent the 404.

thanks, Sean

<?php
/**
 * Language switcher template
 *
 * @see Mage_Page_Block_Switch
 */
?>
<?php if(count($this->getStores())>1): ?>
<div class="form-language">
    <label for="select-language"><?php echo $this->__('Your Language:') ?></label>
    <select id="select-language" title="<?php echo $this->__('Your Language') ?>" onchange="window.location.href=this.value">
    <?php foreach ($this->getStores() as $_lang): ?>
        <?php $_selected = ($_lang->getId() == $this->getCurrentStoreId()) ? ' selected="selected"' : '' ?>
        <option value="<?php echo $_lang->getCurrentUrl() ?>"<?php echo $_selected ?>><?php echo $this->escapeHtml($_lang->getName()) ?></option>
    <?php endforeach; ?>
    </select>
</div>
<?php endif; ?>

2 Answers 2

2

I guess it is not possible to check wether $_lang->getCurrentUrl() has active status or not, since this is not something you store in the URL, it is a category option.

First of all, I would fix this using .htaccess or something, to just redirect to homepage on 404 pages, or put meaningful 404 page on your website. That would save you the overhead of fixing this in your template file.

But, if you want to fix this from your language switcher template, the workaround is to get the current category, and then per store get the status for the category.

To get the current category I think you can use something like this:

<?php 
 $catid = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); 
?>

Then, in your loop where your looping through your store you would need to get the category information, using something like this:

$categoryStore = Mage::getModel('catalog/category')
  ->setStoreId($_lang->getId())
  ->load($catid );

Please note that I use $_lang->getId() since that is the way to get the ID of your store inside the loop.

So now you have the category information, for your specific store. Next, just call:

if ($categoryStore->getIsActive()) { //return another url now }

So, when the if return true, instead of putting the $_lang->getCurrentUrl() into the options value, you want to put a specific page, or your sites homepage for that store into the url.

Hope this helps.

2
  • Thanks! Helpfull ... only remark would be that it solves category and not also product pages ...
    – snh_nl
    Commented May 6, 2015 at 10:01
  • I agree, that would mean that you need to add another check to see on what type of page they are, see stackoverflow.com/questions/3503842/… Getting a little more complex then but you could figure that out. I would still go for the meaningful 404, or redirect of 404 to homepage anyways but you probably have your own reasons. Commented May 6, 2015 at 10:06
1

A possible answer is comprised in this gist

https://gist.github.com/seansan/fe1945722c7b257ab55f

also a bug report was made: http://www.magentocommerce.com/bug-tracking/issue/index/id/754

The currect code uses $_lang->GetCurrenUrl() this is not specific enough.

Either GetCurrenUrl() should have a new method GetIsActive() or IsActive() or code should be updated like in the gist link with logic (detect router/get id/check if active/in specific store_id).

@programmers, please feel free to improve this code gist (https://gist.github.com/seansan/fe1945722c7b257ab55f)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.