Magento:类别名称而不是类别路径中的类别ID。

时间:2013-01-02 07:13:56

标签: php performance magento magento-1.7

我正在尝试根据magento中的产品ID获取类别路径中的类别名称。

假设我的产品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;
}

感谢.....

4 个答案:

答案 0 :(得分: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().'/';
}

答案 1 :(得分:3)

我认为您应该查看处理此问题的magento目录网址重写...在您的magento管理员中,它应该在目录&gt;&gt; URL重写管理

编辑:

因为您想在其他页面中执行此操作,您只需执行以下操作:

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

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

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

答案 2 :(得分:0)

这是比@mpaepper建议更快的解决方案。我只加载了一次收集,然后就不会问数据库了。

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;

答案 3 :(得分:0)

这是代码Magento:获取带有类别路径的类别名称。请试一试。我希望它会对你有所帮助。

<?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>
相关问题