0

We have insert this code in the header.phtml to have on all web page:

    <?php
    $groupId = Mage::app()->getStore()->getGroupId();
    $collection = Mage::getModel('core/store')->getCollection()->addFieldToFilter('group_id', $groupId);
    foreach ($collection as $store) {
            echo '<link rel="alternate" href="' . preg_replace('/\?.*/', '', $store->getCurrentUrl()) . '" hreflang="' . $store->getCode() . '"/>' . "\n";
    }
    ?>

It is working good, the only problem is it list 2 same URL and we have 2 different URL for all page/category/product

What appears:

    <link rel="alternate" href="http://example.com/index.php/parts/battery.html" hreflang="fr_ca"/>
    <link rel="alternate" href="http://example.com/index.php/parts/battery.html" hreflang="en_ca"/>

Whate we want to be displayed:

    <link rel="alternate" href="http://example.com/index.php/pieces/batteries.html" hreflang="fr_ca"/>
    <link rel="alternate" href="http://example.com/index.php/parts/battery.html" hreflang="en_ca"/>

How to do that?

0

1 Answer 1

0

Hi have found the solution here: How to add hreflang tags (or other meta tags) to pages in magento?

It is bit complicated to all understand so there is the good complete code:

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

Just change the en_ca for your language and country and do the same after $lang withe a "-".

If you have more than 2 store just add another:

    if (strtolower($lang) == 'en_us'){
    $lang = 'en-us'; // en-us or other
    }

Before echo and after }. Each time you add store or for each store.

ex: if you have 5 store, you will have 5 if (strtolower($lang)...

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