Magento:如何获得不同商店视图的类别页面URL键


Magento : How to get category page URL keys for different store views?

我试图获得不同商店视图的类别页面的页面URL键。基本上我有3个商店设置在我的Magento安装。现在我想在我的分类页面中实现三个标签。但是当我在默认存储中时,我无法访问其他存储视图的类别URL键,反之亦然。

我有一个类别对象,它来自

$category = Mage::registry('current_category');

任何想法?

似乎在不同的商店下获得类别url的最好方法是使用Magento的Mage_Core_Model_App_Emulation。下面是一个示例:

/**
 * @var $categoryId - The numeric category ID you want to get the URL of.
 * @var $altStoreId - The numeric ID of the other store view to get the URL from.
 */
$env = Mage::getSingleton('core/app_emulation')->startEnvironmentEmulation($altStoreId);
$category = Mage::getModel('catalog/category')->load($categoryId);
$altUrl = $category->getUrl();
Mage::getSingleton('core/app_emulation')->stopEnvironmentEmulation($env);

My Solution, works well

/**
 * @var $store_id  - The numeric ID of the store view to get the URL from.
 * @var $store_url - Base URL of the store
 */
 $store_url   = Mage::app()->getStore($store_id)
                ->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
 $objcategory = Mage::registry('current_category');
 $categoryId  = $objcategory->getId();
 $caturlkey   = Mage::getModel('catalog/category')
                    ->setStoreId($store_id)->load($categoryId)->getUrlKey();
 $altUrl      = $store_url.$caturlkey;

您可以这样执行(在环境仿真时应该成本更低):

$currentStore = Mage::app()->getStore();
Mage::app()->setCurrentStore(Mage::app()->getStore($yourAltStoreId));
$categoryLink = Mage::getModel('catalog/category')->load($yourCategoryId)->getUrl();
Mage::app()->setCurrentStore($currentStore);