This code implements the hreflang alternate tag wich is perfect if you have a magento multistore with different languages and want to push your store in Google.

I looked for a extension but i didn’t want to spend any money on it. Another reason why i didn’t took a magento plugin was because i have a multistore with 3 languages and 5 storeviews.

storeviews magento multistore

So i programmed a little hreflang alternate generator for all cases.

It’s perfect for a Magento multistore with multiple magento websites and multiple stores. It’s working with both, categories and products.

My hreflang extension features

  • It works with different domains
  • It detects products and categories
  • It detects if a product is set to invisible in a storeview
  • It detects if a category is active in a storeview
  • It detects the homepage
  • It does not work with CMS-pages, cart, wishlist and so on, but that’s okay because Google doesn’t care about that. If you have CMS pages, you can use static blocks and assign them to a category

Hreflang alternate tag for magento products

The language tags should look like that.

<link rel="alternate" hreflang="de-DE" href="http://www.yoursite.de/fahrrad/"/>
<link rel="alternate" hreflang="de-AT" href="http://www.yoursite.at/fahrrad/"/>
<link rel="alternate" hreflang="de-CH" href="http://www.yoursite.ch/fahrrad/"/>
<link rel="alternate" hreflang="en" href="http://www.yoursite.com/bicycle/"/>
<link rel="alternate" hreflang="nl" href="http://www.yoursite.nl/fiets/"/>

Implement hreflang alternate in Magento

Just copy the following php code and paste it into that file:

  • app/design/frontend/default/THEME/template/page/html/head.php

After that change the store id’s in the code and upload the file.

/*
 * Generate hreflang alternate tags for all languages
 * @autor: Andreas Grundner
 */
function getAlternateUrl($iStoreCode) {

 $store = Mage::app()-&gt;getStore();
 $prod = Mage::registry('current_product');
 $categ = Mage::registry('current_category');
 $store_url   = Mage::app()-&gt;getStore($iStoreCode)-&gt;getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
    $categId = $categ ? $categ-&gt;getId() : null;

 # product
 if($prod) {
 $iProduct_id = $prod-&gt;getId();

 /*
   check if product is visible
 VISIBILITY_BOTH = 4
 VISIBILITY_IN_CATALOG = 2
 VISIBILITY_IN_SEARCH = 3
 VISIBILITY_NOT_VISIBLE = 1
 */
 if (Mage::getModel('catalog/product')-&gt;setStoreId($iStoreCode)-&gt;load($iProduct_id)-&gt;getVisibility() != 1) {

 $sProductStoreUrl = Mage::getModel('catalog/product')-&gt;setStoreId($iStoreCode)-&gt;load($iProduct_id)-&gt;getProductUrl();

 if (strpos($sProductStoreUrl, '?')) {
 if($sOutput = substr($sProductStoreUrl, 0, strpos($sProductStoreUrl, "?"))) {
 return $sOutput;
 }
 }
 else
 return $sProductStoreUrl;
 }
 return false;
 }
 # category
 elseif($categ) {

 $categoryId  = $categ-&gt;getId();
 $oCat = Mage::getModel('catalog/category')-&gt;setStoreId($iStoreCode)-&gt;load($categoryId);
 if($oCat-&gt;getIsActive()) {
 $aCaturlkey = parse_url($oCat-&gt;getUrl());
 return rtrim($store_url, "/") . $aCaturlkey["path"];
 }
 else
 return false;

 }
 elseif(Mage::getBlockSingleton('page/html_header')-&gt;getIsHomePage()) {
     return rtrim($store_url, "/");
 }
 else {
 return false;
 }
}

# Fill in the store_id from the database table core_store
if(getAlternateUrl(1))
 echo '&lt;link rel="alternate" hreflang="de-DE" href="' . getAlternateUrl(1) . '/" /&gt;' . "\n";

if(getAlternateUrl(3))
 echo '&lt;link rel="alternate" hreflang="de-AT" href="' . getAlternateUrl(3) . '/" /&gt;'  . "\n";

if(getAlternateUrl(4))
 echo '&lt;link rel="alternate" hreflang="de-CH" href="' . getAlternateUrl(4) . '/" /&gt;'  . "\n";

if(getAlternateUrl(9))
 echo '&lt;link rel="alternate" hreflang="en" href="' . getAlternateUrl(9) . '/" /&gt;'  . "\n";

if(getAlternateUrl(8))
 echo '&lt;link rel="alternate" hreflang="nl" href="' . getAlternateUrl(8) . '/" /&gt;'  . "\n";