0

In my Magento2 website i have installed an extension for generate links for hreflang.

This is the class that generates links for hreflang:

use Magento\Framework\View\Element\Template;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\Group;
use Magento\Store\Model\Store;
use Magento\Store\Model\Website;

class HrefLang extends Template
{
    /**
     * @var \BrunoCanada\HrefLang\Service\HrefLang\AlternativeUrlService
     */
    private $alternativeUrlService;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \BrunoCanada\HrefLang\Service\HrefLang\AlternativeUrlService $alternativeUrlService,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->alternativeUrlService = $alternativeUrlService;
    }

    /**
     * @return array in format [en_us => $url] or [en => $url]
     */
    public function getAlternatives()
    {
        $data = [];
        foreach ($this->getStores() as $store) {
            $url = $this->getStoreUrl($store);
            if ($url) {
                $data[$this->getLocaleCode($store)] = $url;
            }
        }
        return $data;
    }

    /**
     * @param Store $store
     * @return string
     */
    private function getStoreUrl($store)
    {
        return $this->alternativeUrlService->getAlternativeUrl($store);
    }

    /**
     * @param StoreInterface $store
     * @return bool
     */
    private function isCurrentStore($store)
    {
        return $store->getId() == $this->_storeManager->getStore()->getId();
    }

    /**
     * @param StoreInterface $store
     * @return string
     */
    private function getLocaleCode($store)
    {
        
        $localeCode = $this->_scopeConfig->getValue('general/locale/code', 'stores', $store->getId());
        return str_replace('_', '-', strtolower($localeCode));
    }

    /**
     * @return Store[]
     */
    private function getStores()
    {
        /*if ($this->_scopeConfig->isSetFlag('seo/hreflang/same_website_only')) {
            return $this->getSameWebsiteStores();
        }*/
        return $this->_storeManager->getStores();
    }

    /**
     * @return Store[]
     */
    private function getSameWebsiteStores()
    {
        $stores = [];
        /** @var Website $website */
        $website = $this->_storeManager->getWebsite();
        foreach ($website->getGroups() as $group) {
            /** @var Group $group */
            foreach ($group->getStores() as $store) {
                $stores[] = $store;
            }
        }
        return $stores;
    }
}

And this is the file phtml:

<?php
$alternatives = $block->getAlternatives();
?>
<?php if ($alternatives): ?>
    <?php foreach ($alternatives as $hrefLang => $url): ?>
        
<link rel="alternate" hreflang="<?php echo $hrefLang ?>" href="<?php echo $url ?>" />
    
    <?php endforeach; ?>
    
    
    
<?php endif ?>

The module works but all links generated are all referring to the default store. This issue happen only on category page and not for product page:

Current result:

www.mysite.com/categoria1 ---> store 1
www.mysite.com/categoria1 ---> store 2
www.mysite.com/categoria1 ---> store 3

Expected result:

www.mysite.com/categoria1 ---> store 1
www.mysite.com/category1 ---> store 2
www.mysite.com/Kategorie ---> store 3

I checked all my backend links in url rewrites section and they are all correct!

How can I do to fix this error?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.