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;
|