Reputation: 2501
I'm trying to get the page URL Key for category pages for different store views. Basically I have 3 stores set up in my Magento installation. Now I want to implement hrefhang tags in my category pages. But I cannot access category URL keys of other store views when I'm in default store and vice versa.
I have category object which I get from,
$category = Mage::registry('current_category');
Any ideas?
Upvotes: 3
Views: 6386
Reputation: 103
You can perform like this (should be less costly when an environment emulation):
$currentStore = Mage::app()->getStore();
Mage::app()->setCurrentStore(Mage::app()->getStore($yourAltStoreId));
$categoryLink = Mage::getModel('catalog/category')->load($yourCategoryId)->getUrl();
Mage::app()->setCurrentStore($currentStore);
Upvotes: 0
Reputation: 2501
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;
Upvotes: 3
Reputation: 1621
It seems like the best way to get category URLs under a different store than the current one is to make use of Magento’s Mage_Core_Model_App_Emulation
. Here’s an example of how you could do that:
/**
* @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);
Upvotes: 6