Magento rel alternate hreflang

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()->getStore();
	$prod = Mage::registry('current_product');
	$categ = Mage::registry('current_category');
	$store_url   = Mage::app()->getStore($iStoreCode)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
    $categId = $categ ? $categ->getId() : null;


	# product
	if($prod) {
		$iProduct_id = $prod->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')->setStoreId($iStoreCode)->load($iProduct_id)->getVisibility() != 1) {

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

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

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


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

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

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

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

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

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


Sei sozial und teile

Über den Autor Andreas Grundner

Ich bin Wordpress, Joomla, Typo3 Integrator, Unix Rootserver Administrator und Webanwendungsprogrammierer. Mittlerweile habe ich über 70 Webprojekte weitgehend selbstständig mit enger Kundenabsprache realisiert. Ich bin auf One Page Websites auf Wordpress-Basis spezialisiert, biete aber auch Suchmaschinenoptimierung (SEO), Blogs, Facebook, Youtube und Google+ Seiten an. Meine Verpflichtung gilt dem Datenschutz, meine Leidenschaft dem Finden und Aufzeigen von Sicherheitslücken in Webanwendungen.

Meinung sagen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.