2013-01-02 58 views
4

我想根据magento中的产品ID获取类别路径中的类别名称。Magento:类别名称,而不是类别路径中的类别标识。

假设我的产品Id = 1,并且我定义了category5(id = 5),并且我得到类似2/3/5的路径。而不是这种类型的路径,我需要类别路径,如category2/category3/category5。这意味着我需要路径中的类别名称而不是类别ID。我通过使用下面的代码得到了这个,但它花费了很多时间。我需要缩短处理时间。

请给我如何减少处理时间的建议。

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

感谢.....

+0

它是你的问题解决? – Rathinam

回答

7

你应该使用类集合,而不是负荷为每个类别减少数据库查询。

编辑: 我写过你的代码示例。我假设你有类别的类别ID其中,你想要得到的路径$的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().'/'; 
} 
+0

请建议我如何使用类别收集和地点?因为我是新来的magento @mpaepper – user123

+0

感谢您的建议,我会努力,但如果您有时间指导我更多 – user123

+0

@ user123我添加了一个代码示例,以显示如何使用类别集合,如果您有一个类别标识检索路径名称 – mpaepper

3

我想你应该看看它处理这个问题对你的Magento目录URL重写.. 。在您的Magento管理应当在目录>> URL重写管理

编辑:

因为你想要做一些其他的页面,您可以简单地做:

$store = Mage::app()->getStore(); 

$product_url = $store->getBaseUrl().$product->getUrlPath(); 

$category_url = $store->getBaseUrl().$category->getUrlPath(); 
+0

感谢您的答复,但我需要我的一些PHP页面上所需的输出,所以请告诉我我怎么做这个@nasaralla – user123

+0

检查我编辑的答案 – Nasaralla

0

这是比@mpapper建议更快速的解决方案。我只加载一次该集合,然后不询问数据库。

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;