I have three websites under single magento installation and would like to generate hreflang tags for each websites manually.

I used the code mentioned in this question

<?php foreach (Mage::app()->getWebsites() as $website) {
    foreach ($website->getGroups() as $group) {
        $stores = $group->getStores();
        foreach ($stores as $store) {
            $storeCode = substr(Mage::getStoreConfig('general/locale/code', $store->getId()),0,2);
            echo '<link rel="alternate" hreflang="' . $storeCode . '" href="' . $store->getCurrentUrl(false) . '"/>' . "\n";

However, this only outputs hreflang available as storeview language. Also no coutntry is specified. I would like to use more hreflang tags as fi,de, en-ca etc.

How can I manually write hreflang tag?

--- UPDATE ---

For now, I could manually write hreflang by creating head.phtml for each websites. Then to add, for example,

<link rel="alternate" href="http://www.mywebsite.com<?php $urlString = Mage::helper('core/url')->getCurrentUrl(); $url = Mage::getSingleton('core/url')->parseUrl($urlString); $path = $url->getPath();echo $path ?>" hreflang="en-gb" />

This works. And so far Google Webmastertool is not giving any errors. However, this method obviously doesn't list all the available languages in my shop. But if I don't create each head.phtml and just have one head.phtml with the code above, it will add extra store code when you are on different website. Any good solution for this?


Try replacing

$storeCode = substr(Mage::getStoreConfig('general/locale/code', $store->getId()),0,2);

with this:

$storeCode = strtolower(str_replace('_', '_', Mage::getStoreConfig('general/locale/code', $store->getId())));
|improve this answer|||||
  • I tried your code. It adds country code. That's good. But still I don't want magneto to automatically output hreflang tag. I want to manually write for each hreflang version. I tried the code <link rel="alternate" href="example.com/ca<?php $urlString = Mage::helper('core/url')->getCurrentUrl(); $url = Mage::getSingleton('core/url')->parseUrl($urlString); $path = $url->getPath();echo $path ?>" hreflang="en-ca" /> – rtk0407 Mar 18 '15 at 8:45
  • but then it adds extra store code. <link rel="alternate" href="example.com/ca/ca/sample-product.html" hreflang="en-ca" /> So I am not sure how to do this. – rtk0407 Mar 18 '15 at 8:45
  • I don;t know where that extra store code comes from. Do you have "use store codes in url" set to Yes in config? – Marius Mar 18 '15 at 8:47
  • No. In this page magento.stackexchange.com/questions/12504/… some suggestions were given. I tried it also. Extra code doesn't appear. Also with the code posted in my question above. But still this is automatically generated. I want to write manually for each language. So that I can get results like <link rel="alternate" href="example.com/en/sample-product.html"; hreflang="en-ca" /> <link rel="alternate" href="example.com/eu/sample-product.html"; hreflang="en_gb" /> – rtk0407 Mar 18 '15 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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