Magento themes and extensions

Implement rel alternate links in Magento

Share

With the rise of E-commerce, selling goods to customers around the globe became easier than ever before. Internet has no borders, and anyone can ‘walk into your store’ whilst being in comfort of his own home.

An increasing need for multilingual stores has appeared. You might want your customers to be able to browse your store in their own language.

When you’re setting up a multilingual store, Google has some directives and recommendations as to what should be done for your website to be indexed correctly, and rank higher in their search.

One of those recommendations is implementation of , if you have pages translated to another language.

Syntax we should be using looks like this:

1
<link rel="alternate" hreflang="es" href="http://es.example.com/" />

If we adapt this syntax to a Magento store, this would look like:

1
<link rel="alternate" hreflang="first-two-letters-of-a-store-locale" href="url-of-the-page-in-other-language" />

Now that we have an idea what we should do, let’s get started by registering our module.

app/etc/modules/Inchoo_Alternate.xml

1
2
3
4
5
6
7
8
9
<?xml version="1.0"?>
<config>
<modules>
<Inchoo_Alternate>
<active>true</active>
<codePool>community</codePool>
</Inchoo_Alternate>
</modules>
</config>

Let’s talk our module’s configuration file a bit. What we need to do is insert alternate links in every pages’ head. One way of doing this would be creating logic needed in head.phtml file.

The better way would be using an observer to listen to some event, and insert links to head using a method Magento already has – addLinkRel().

In this example, we’ll use controller_action_layout_generate_blocks_after event. There probably is some other event that is more suitable, but this one will do the trick.

app/code/community/Inchoo/Alternate/etc/config.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0"?>
<config>
<modules>
<Inchoo_Alternate>
<version>1.0.0</version>
</Inchoo_Alternate>
</modules>
<global>
<events>
<controller_action_layout_generate_blocks_after>
<observers>
<inchoo_alternate>
<type>singleton</type>
<class>Inchoo_Alternate_Model_Observer</class>
<method>alternateLinks</method>
</inchoo_alternate>
</observers>
</controller_action_layout_generate_blocks_after>
</events>
</global>
</config>

We just defined what will be the name our observer and it’s method, let’s create them now.

app/code/community/Inchoo/Alternate/Model/Observer.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
class Inchoo_Alternate_Model_Observer
{
public function alternateLinks()
{
$headBlock = Mage::app()->getLayout()->getBlock('head');
$stores = Mage::app()->getStores();
foreach ($stores as $store)
{
$url = $store->getCurrentUrl(false);
$storeCode = substr(Mage::getStoreConfig('general/locale/code', $store->getId()),0,2);
$headBlock->addLinkRel('alternate"' . ' hreflang="' . $storeCode, $url);
}
return $this;
}
}

As you can see, we’re looping through all the stores. With getCurrentUrl() method, we’ll get current URL in different store.

E.g. if our current url is www.example.com/shirts.html, in our German store, it will bewww.example.com/shirts.html?___store=german or even www.example.com/de/shirts.html, depending on your Magento configuration.

Next up, we’ll get first two letters our store’s locale code. This might not be perfect if you have e.g. two english stores, one for Great Britain, and one for US, but I’ll let you figure this one out.

After we have gotten all the information we needed, we’ll call the head block and use it’s addLinkRel()method to add the links to the head.

That’s pretty much it. Inspect your website to see the links

Comments are closed.