3

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.

3

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

  • @sander-manguel thank you.. interesting approach. i will do that. brgds – s_h Sep 15 '13 at 2:44
  • @s_h : Could u pls explain, how you did this. – Gem Oct 4 '16 at 11:28
  • @JeevaRathinam it only works in category / product page. you put that code in the top of the file, and then you use $category in your code. thats it. – s_h Oct 5 '16 at 15:12
  • how can i get the ids from the category path? – Mujahidh Mar 21 at 11:51
  • please help me in this question. magento.stackexchange.com/questions/266825/… – Mujahidh Mar 21 at 11:52
2

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;
}
  • How can i run above code? – user51361 May 24 '17 at 9:57
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.