Magento: category name instead of category identifiers in the category path.

I am trying to get category names in a category path based on a product id in magento.

Suppose My Product Id = 1 and In that I define category5 (id = 5), and I get the path to the category as 2/3/5. Instead of this type of category path, I need a category path, for example category2 / category3 / category5. This means that I need category names in the path instead of category identifiers. I got this using the following code, but it takes so long. I need to reduce processing time.

Please give me tips on how to reduce the process time.

$category_model = Mage::getModel('catalog/category'); $product_model = Mage::getModel('catalog/product'); $all_cats = array(); $product_model->reset(); $_product = $product_model->load($entityId); $all_cats = $product_model->getCategoryIds($_product); $main_cnt = count($all_cats); $cat_str_main = ''; $j = 0; foreach($all_cats as $ac) { $root_category = $category_model->load($ac); $cat_path = $root_category->getPath(); $cat_arr = explode("/",$cat_path); $cnt = count($cat_arr); $cat_str = ''; $main_str = ''; $i=0; foreach($cat_arr as $ids) { $root_category = $category_model->load($ids); //load root catalog if($i == 2) { $cat_str = $category_model->getName(); } else if($i > 2) { $cat_str = $cat_str."/".$category_model->getName(); } $i = $i+1; } if($j < 1) { $cat_str_main = $cat_str; } else { $cat_str_main = $cat_str_main .",".$cat_str; } $j = $j+1; } 

Thanks.....

+4
source share
4 answers

You should use a collection of categories instead of a load for each category to reduce database queries.

Edit: I wrote you a sample code. I assume that you have the category identifier from which you want to get the path names as $ categoryId:

 $category = Mage::getModel('catalog/category')->load($categoryId); $collection = $category->getResourceCollection(); $pathIds = $category->getPathIds(); $collection->addAttributeToSelect('name'); $collection->addAttributeToFilter('entity_id', array('in' => $pathIds)); $result = ''; foreach ($collection as $cat) { $result .= $cat->getName().'/'; } 
+7
source

I think you should take a look at renaming the magento directory that handles this problem for you ... in your magento admin, it should be in the directory -> Transition Control URL

Edit:

Since you want to do this on another page, you can simply do:

 $store = Mage::app()->getStore(); $product_url = $store->getBaseUrl().$product->getUrlPath(); $category_url = $store->getBaseUrl().$category->getUrlPath(); 
+3
source

Here is a solution that works faster than the @mpaepper suggestion. My downloads the collection only once and does not query the database.

 echo "################################", PHP_EOL; $collection = Mage::getResourceModel('catalog/category_collection'); /* @var $collection Mage_Catalog_Model_Resource_Category_Collection */ $pattern = '1/2/'; $collection->addNameToResult() ->addPathFilter($pattern . '.+'); $categories = array(); foreach ($collection as $id => $_cat) { /* @var $_cat Mage_Catalog_Model_Category */ $path = explode('/', $_cat->getPath()); $namedPath = []; foreach ($path as $_id) { $subcat = $collection->getItemById($_id); if ($subcat) { $namedPath[] = $subcat->getName(); } } $categories[implode('/', $namedPath)] = $id; } print_r($categories); echo $collection->count(), $collection->getSelect()->assemble(), PHP_EOL; 
0
source

Here is the Magento code: get the category name with the category track. Please, try. Hope this helps you.

 <?php set_time_limit(0); require_once '../app/Mage.php'; Mage::app(); ?> <table class="category-data" border="1" align="center"> <tr> <td><h2>Category Path</h2></td> <td><h2>Category Id</h2></td> </tr> <?php $category = Mage::getModel('catalog/category'); $tree = $category->getTreeModel(); $tree->load(); $ids = $tree->getCollection()->getAllIds(); $categories = array(); if ($ids) { foreach ($ids as $id) { $category->load($id); $root = 'Root Catalog'; $isRoot = strtolower($root); $categoryName = strtolower($category->getName()); if($categoryName == $isRoot){ continue; } $categories[$id]['name'] = $category->getName(); $categories[$id]['path'] = $category->getPath(); } foreach ($ids as $id) { $path = explode('/', $categories[$id]['path']); $len = count($path); $string = ''; if($id > 2){ foreach ($path as $k=>$pathId) { $separator = ''; if($pathId > 2){ if($k != $len-1){ $separator = ' || ';} $string.= $categories[$pathId]['name'] . $separator; } $cnt++; } ?> <tr> <td><?php echo $string; ?></td> <td><?php echo $id; ?></td> </tr> <?php } ?> <?php } } ?> </table> 
0
source

All Articles