Snippets

Steve Cordle MAGENTO: Get Categories from a product in a specific store

Created by Steve Cordle last modified
$storeId = Mage::getModel('core/store')->load('en_us3', 'code')->getId();
$rootCategoryId = Mage::app()->getStore($storeId)->getRootCategoryId();

$product = Mage::getModel('catalog/product')
    ->setStoreId($storeId)
    ->loadByAttribute('sku',$sku);

// Create a category blank model just in-case.
$result = Mage::getModel('catalog/category');

if ($product) {
    $product_categories = $product->getCategoryCollection()
        ->setStoreId($storeId)
        ->addFieldToFilter('is_active', 1)
        ->addAttributeToFilter('path', array('like' => "1/{$rootCategoryId}/%"))
        ->addAttributeToSelect("*");

foreach ($product_categories as $category) {
    $topCat = $this->_getTopCat($category);
    $result = $result->load($topCat->getId());
    if ($result->getIsPrimary()) {
        break;
    }
}

//Primary category
return $result;

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.