添加rel =“alternate”的Magento $ category-> getURL()问题

时间:2014-05-16 22:51:03

标签: magento

尝试制定解决方案以合并rel =" alternate"标签到多语言Mage商店。注意:类别和产品具有每个商店的自定义URL密钥

以下代码正常运行并为我提供了产品的正确完整网址,格式为http://www.storename.com/storecode/product.phtml

$url = Mage::getModel('catalog/product')->setStoreId($storeId)->load($product->getId())->getProductUrl();

但以下内容不适用于类别。它为每个URL提供相同的商店代码,而不是foreach中每个商店的正确商店代码:

$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";
        }
    }
}
?>

任何建议或帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

这是我找到的最快的解决方案:

$url = $store->getCurrentUrl(false) . Mage::getModel('catalog/category')->setStoreId($storeId)->load($category->getId())->getUrlKey() . '.html';