Question

for designing purposes I need to retrieve the product category path of each single product. I was able to retrieve the category path and explode the first level using the following code in catalog/product/list.phtml:

<?php $currentCat = Mage::registry('current_category'); ?>
<?php $exp = explode("/", $currentCat->getPath());?>

on the other hand I tried to retrieve category path of each product display in search results in order to display different colors for each product based on their main category but with no good results.

any help will be appreciated. brgds.

Was it helpful?

Solution

The following code will give you an array starting with the root category (id: 1), then the stores root category and then the path down to the category your product is in.

$category = Mage::getModel('catalog/product')->load([id])->getCategory();
$path = explode('/', $category->getPath());

How ever, this is quite a 'heavy' piece of code just for design purposes. You might be better of extending the product collection in the Mage_Catalog_Block_Product_List class, method _getProductCollection joining the catalog_category_entity table to the collection query and adding the path field to the select fields

OTHER TIPS

To get path from Root catalog

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

I used following code to find product categories for breadcrumbs when landing to product page outside site. (when if ($category = $this->getCategory()) fails)

$cate is last category product was found in and you can use category functions to get path.

magento/app/code/core/Mage/Catalog/Helper/Data.php

Add where product is found if coming from somewhere else than category

Needs FIX: TODO: check if product has category before looping!

if(empty($path)) {
    if ($this->getProduct()) {
        $collection =  Mage::getModel('catalog/category')->getCollection()
            ->setStoreId(Mage::app()->getStore()->getId())
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('is_active')
            ->addFieldToFilter('entity_id', array('in' => $this->getProduct()->getCategoryIds()));
        $categories = array();
        $cate = "";
        foreach ($collection as $category) {
            $cate = $category;
            $pathInStore = $category->getPathInStore();
            $pathIds = array_reverse(explode(',', $pathInStore));
        }
        $categories = $cate->getParentCategories();
        if($categories) {
        foreach($pathIds as $categoryId) {
                $path['category'.$categoryId] = array(
                    'label' => $categories[$categoryId]->getName(),
                    'link' => $this->_isCategoryLink($categoryId) ? $categories[$categoryId]->getURL() : ''
                );
        }}
    }
}

Thank you for your answers that helped me to accomplish this.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top