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.....
source share