skip to Main Content

Using code from a previous discussion (link:https://magento.stackexchange.com/questions/12504/how-to-add-hreflang-tags-or-other-meta-tags-to-pages-in-magento), I was able to implement the hreflang links into our Magento site.

Here is the code that worked for me:

    <?php foreach (Mage::app()->getWebsites() as $website) {
    foreach ($website->getGroups() as $group) {
        $stores = $group->getStores();
        foreach ($stores as $store) {
            $storeId = $store->getId();
            $storeCode = substr(Mage::getStoreConfig('general/locale/code', $storeId),0,2);
            if (Mage::registry('product')) {
                $productId  = Mage::registry('product')->getId();
                $base_url = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
                $url = Mage::getModel('catalog/product')->setStoreId($storeId)->load($productId)->getProductUrl();
                $url = preg_replace('/?.*/', '', $url); 
                echo '<link rel="alternate" hreflang="' . $storeCode . '" href="' . $url . '"/>';}
            elseif(Mage::registry('current_category')) { 
                $categoryId = Mage::registry('current_category')->getId();
                $base_url = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
                $url = Mage::getModel('catalog/category')->setStoreId($storeId)->load($categoryId)->getUrlPath();
                echo '<link rel="alternate" hreflang="' . $storeCode . '" href="' . $base_url . $url . '"/>' . "n";
                }}}} 
?>
<?php 
            $storeId = 1;
            if (Mage::registry('product')) {
                $productId  = Mage::registry('product')->getId();
                $base_url = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
                $url = Mage::getModel('catalog/product')->setStoreId($storeId)->load($productId)->getProductUrl();
                $url = preg_replace('/?.*/', '', $url); 
                echo '<link rel="alternate" hreflang="x-default" href="' . $url . '"/>';
}
            elseif(Mage::registry('current_category')) { 
                $categoryId = Mage::registry('current_category')->getId();
                $base_url = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
                $url = Mage::getModel('catalog/category')->setStoreId($storeId)->load($categoryId)->getUrlPath();
                echo '<link rel="alternate" hreflang="x-default" href="' . $base_url . $url . '"/>' . "n";
            }
?> 

I’m having an issue with layered navigation URL’s and canonical links disappearing on category pages.

Is there something I can add to this code to make sure that layered navigation URL’s that contain “?” after .html get written as shown in the browsers address bar?

Also, on these types of category pages the canonical link does not show.

The code works perfectly on product pages.

Any help would be greatly appreciated!

Thanks in advance.

2

Answers


  1. To solve this I need to know:

    • Where have you put this code? Is executed in categories?
    • Is the current_category empty?

    Regards.

    Login or Signup to reply.
  2. I missunderstood the question. Could you show an example?
    The code only shows the category base URLs. When applying filters the module change the URL adding parameters filtered but your code doesn’t have those parameters.

    Usually people do not position the filters because it gives many problems of duplicate content. Take a look at this link:
    https://amasty.com/blog/magento-layered-navigation-best-settings-for-seo/

    In my opinion, I would left canonical URLs in categories using its base URL and disallow to index anything layered because could give duplicated content issues.

    Kind regards

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search