Getting Magento Category Paths for a specific product

Updated . Posted . Visible to the public.
public function getCategoryPath($sku)
    {
        $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
        
        $pathArray = array();
        $collection1 = $product->getCategoryCollection()
            ->setStoreId(Mage::app()->getStore()->getId())
            ->addAttributeToSelect('path')
            ->addAttributeToSelect('is_active');
            

        foreach($collection1 as $cat1){            
            $pathIds = explode('/', $cat1->getPath());            
            $collection = Mage::getModel('catalog/category')->getCollection()
                ->setStoreId(Mage::app()->getStore()->getId())
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('is_active')
                ->addFieldToFilter('entity_id', array('in' => $pathIds));

            $pahtByName = '';
            foreach($collection as $cat){                
                $pahtByName .= '/' . $cat->getName();
            }
            
            $pathArray[] = $pahtByName;

        }
        
        return $pathArray;
    }
vasan
Last edit
vasan
Keywords
Magento, Category
Posted by vasan to vasan's deck (2014-09-15 07:57)