Magento$类别->;用于添加rel=";的getURL()问题;候补;
正在尝试制定一个解决方案,将rel=“alternate”标记合并到多语言法师存储中。注意:类别和产品为每个商店定制了URL键 下面的代码工作正常,并以 但以下内容不适用于类别。它为每个URL提供相同的存储代码,而不是foreach中每个存储的正确存储代码:Magento$类别->;用于添加rel=";的getURL()问题;候补;,magento,Magento,正在尝试制定一个解决方案,将rel=“alternate”标记合并到多语言法师存储中。注意:类别和产品为每个商店定制了URL键 下面的代码工作正常,并以 但以下内容不适用于类别。它为每个URL提供相同的存储代码,而不是foreach中每个存储的正确存储代码: $url = Mage::getModel('catalog/category')->setStoreId($storeId)->load($category->getId())->getUrl(); 在head.
$url = Mage::getModel('catalog/category')->setStoreId($storeId)->load($category->getId())->getUrl();
在head.phtml中,我得到了:
<?php
$url = '';
$pageType = Mage::app()->getFrontController()->getRequest()->getControllerName();
foreach (Mage::app()->getWebsites() as $website) {
foreach ($website->getGroups() as $group) {
$stores = $group->getStores();
foreach ($stores as $store) {
$storeId = $store->getStoreId();
switch ($pageType) {
case 'product':
$product = Mage::registry('current_product');
$url = Mage::getModel('catalog/product')->setStoreId($storeId)->load($product->getId())->getProductUrl();
break;
case 'category':
$category = Mage::registry('current_category');
/* Below is the code that isn't working properly */
$url = Mage::getModel('catalog/category')->setStoreId($storeId)->load($category->getId())->getUrl();
break;
default:
$url = 'def';
break;
}
echo '<link rel="alternate" href="' . $url . '" hreflang="' . $store->getConfig('general/locale/code') . '"/>' . "\n";
}
}
}
?>
以下是我找到的最快的解决方案:
$url = $store->getCurrentUrl(false) . Mage::getModel('catalog/category')->setStoreId($storeId)->load($category->getId())->getUrlKey() . '.html';
更新:我将代码封装在一个模拟的存储环境中,并为每个不同的语言版本获得了完全相同的类别URL——甚至比第一次尝试更糟糕:$appEmulation=Mage::getSingleton('core/app_仿真')$initialEnvironmentInfo=$appEmulation->startEnvironmentEmulation($storeId)$url=Mage::getModel('catalog/category')->setStoreId($storeId)->load($category->getId())->getUrl()$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);原始代码的奇怪之处在于,它在每个URL中以正确的语言给出了类别名称,但商店代码保持不变——当前商店正在查看。如果更改存储语言,输出URL中的存储代码将更改为该存储代码。我不明白为什么它会在URL中为产品代码中的每种语言输出正确的存储代码,但它对类别代码不起作用。
$url = $store->getCurrentUrl(false) . Mage::getModel('catalog/category')->setStoreId($storeId)->load($category->getId())->getUrlKey() . '.html';