I'm trying to generate the href links for our multilingual Magento 2 site, and as the category slug changes, I cannot simply replace the store code in the URL.
I've code is given below :
$category = $this->_registry->registry('current_category');
if (!$category->getId()) {
throw new LocalizedException(__('Failed to initialize category'));
}
$categoryId = $category->getId();
foreach ($this->_storeManager->getStores() as $store) {
$locale = $this->_scopeConfig->getValue('general/locale/code', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store->getStoreId());
$category->setStoreId($store->getStoreId());
$category->load($categoryId);
if($category->getIsActive())
{
$output[$locale] = $category->getUrl();
}
}
It seems to work except that the $category->getUrl() always return the current store URL, even if I loaded the category from another store. Is it a bug or should I do something else?
So how get multiple store category URL ?