0

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.

Stark Doe
  • 3
  • 2

2 Answers2

0

To solve this I need to know:

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

Regards.

  • This code is in the head.phtml file. The current_category isn't empty, but the part of the URL after "?" is being stripped out. Hoping you can help me modify this code to make sure the hreflang links include the full URL of the page being viewed. -- Thanks in advance, Cesar! – Stark Doe Aug 29 '18 at 14:49
  • Hello @StarkDoe – César Hernández Aug 30 '18 at 08:41
  • Hello, The part after "URL" does not pertain to the current_category object, that's why you only get url_key without any parameter. I don't understand well your issue. Note that a canonical URL shouldn't have parameters. Also, my recomendation is to avoid index layered categories because it could give you problems about duplicated content. – César Hernández Sep 04 '18 at 13:49
  • I would like to include the parameter in the URL for my hreflang links. – Stark Doe Sep 04 '18 at 19:20
  • Ok, so if you do this you can't get the URL parameters like this. You must go deep into Amasty layered navigation to do it in the right way trying to get the parameters filter (usually is a object) or you can use the Magento function to get current url and then find out the parameters `Mage::helper('core/url')->getCurrentUrl();` – César Hernández Sep 05 '18 at 07:00
  • Cesar- would you be so kind to adjust the code I'm currently using to include the fix? I'm new to php and wouldn't know where to begin. Thanks so much! – Stark Doe Sep 05 '18 at 12:33
  • @StarkDoe prior to do anything: why are you using `$storeId = 1` ? Do you have more than 1 store? You are printing the same canonical URL for each store. – César Hernández Sep 05 '18 at 14:25
  • Yes, Cesar- we have 10 store views. Again, hreflang links are my concern, not the canonicals. – Stark Doe Sep 05 '18 at 14:55
0

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

  • We optimize our layered results. If possible, please continue to help. Example case: URL: https://www.example.com/cat1/cat2/cat.html?some_attribute=some URL showing in hreflang links: https://www.example.com/cat1/cat2/cat.html Thanks, Cesar! – Stark Doe Sep 04 '18 at 13:26