0

I am adding hreflang-tags to a multi-language Magento store. I found the code below on here and used it in the page header. It does what it's supposed to, but I'm looking for another solution.

<?php
$website = Mage::app()->getWebsite()->getStores();
foreach ( $website as $store) {
$lang = $store->getConfig('general/locale/code');
    if (strtolower($lang) == 'en_us'){
        $lang = 'en-us'; // en-us or other
        }
    if (strtolower($lang) == 'nl_nl'){
        $lang = 'nl-nl'; // nl-nl or other
        }
    echo '<link rel="alternate" href="' . $store->getCurrentUrl() . '" hreflang="' . $lang . '"/>' . "\n";
}
?>

This returns a tag like:

<link rel="alternate" href="https://example.com/category?___store=english&amp;___from_store=english" hreflang="en-us"/>

But I would rather have a code that returns a tag with the actual URL as specified in the backend.

For example:

A product named "shoulder-bag" in category "bags" returns an URL in English as: example.com/bags/shoulder-bag And in Dutch as: example.com/tassen/schouder-tas

I'm also missing the code to specify a "default"

Can anybody help me out? Thanks!

1 Answer 1

0

Here you can find a complete explanation on how to implement that feature : http://inchoo.net/magento/implement-rel-alternate-links-in-magento/


In your case your problem is your not telling Magento that the store is changing in your foreach loop.
You have to tell him with this function :

{foreach}  
Mage::app()->setCurrentStore($store->getId());  
[...]   
{endforeach}

Don't forget to reset to your current store after the loop.

2
  • Thank you for the link. Followed the steps and it works for the product pages now. I've also implemented the "fix" suggested in the comments for non-product pages. It's still not the complete solution I'm looking for, but I'm a bit further than I was.
    – EasyBizz
    Feb 15, 2016 at 7:43
  • Can you update your question with my fix to see if there is no other error with that, and for other people reading the thread. Thank you. Feb 15, 2016 at 15:22

Your Answer

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

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