1

I am using the following code to generate the hreflang on my website:

<?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";
}
?>

This gives me the links exactly the way I want them but with the exception of 'default' store that appears as www.example.com/default/.

I would like to replace 'default' with 'en'. Is there a way to do it? I tried looking for the code but can't find it any where as most developers tend to use getWebsite instead of getstore. Would really appreciate if someone can help with the appropriate code in php that can be pasted in head.phtml.

1 Answer 1

0

How about:

foreach ($collection as $store) {
    $storeCode = $store->getCode() == 'default' ? 'en' : $store->getCode();
    echo '<link rel="alternate" href="' . preg_replace('/\?.*/', '', $store->getCurrentUrl()) . '" hreflang="' . $storeCode . '"/>' . "\n";
}

Evaluate if $store->getCode() gives you default. If it does, set $storeCode = 'en'.

2
  • Thank you Vic, this works perfectly. The final code if someone is interested is: <?php $groupId = Mage::app()->getStore()->getGroupId(); $collection = Mage::getModel('core/store')->getCollection()->addFieldToFilter('group_id', $groupId); foreach ($collection as $store) { $storeCode = $store->getCode() == 'default' ? 'en' : $store->getCode(); echo '<link rel="alternate" href="' . preg_replace('/\?.*/', '', $store->getCurrentUrl()) . '" hreflang="' . $storeCode . '"/>' . "\n"; } ?>
    – Ameeds
    Aug 27, 2015 at 0:06
  • Glad I could help. Please mark my answer as accepted.
    – Vic
    Aug 27, 2015 at 0:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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