Magento按路径获取分类

时间:2013-10-08 12:43:16

标签: php magento

对于新的导入工具,我需要按路径分类,这是我所拥有的路径的示例,例如:

main category/sub 
main category/sub/sub

它始终是名称,而不是网址或内容。可以通过此路径获取类别ID吗?

2 个答案:

答案 0 :(得分:5)

我最近创建了与此相同的内容,基本上模仿了Mage_ImportExport_Model_Import_Entity_Product的功能。

用法

查看系统中的所有路径:

Mage::getModel('your_module/category_finder')->getAllPaths()

搜索路径,返回类别ID:

Mage::getModel('your_module/category_finder')->getIdFromPath('main category/sub/sub');

查看要点: https://gist.github.com/jzahedieh/6884204

<?php
/**
 * Functionality taken from Mage_ImportExport_Model_Import_Entity_Product
 */
class Your_Module_Model_Category_Finder
{
    /**
     * Categories text-path to ID hash.
     *
     * @var array
     */
    protected $_categories = array();

    /**
     * Categories text-path to ID hash with roots checking.
     *
     * @var array
     */
    protected $_categoriesWithRoots = array();

    /**
     * Populates the models properties with category information.
     */
    public function __construct()
    {
        $this->_initCategories();
    }


    /**
     * Finds a subcategory id from a path string
     *
     * @param $string
     * @return bool
     */
    public function getIdFromPath($string)
    {
        if (in_array($string, array_keys($this->_categories))) {
            return $this->_categories[$string];
        }

        return false;
    }

    /**
     * Returns all valid path strings
     *
     * @return array
     */
    public function getAllPaths()
    {
        return array_keys($this->_categories);
    }

    /**
     * Initialize categories text-path to ID hash.
     *
     * @return Mage_ImportExport_Model_Import_Entity_Product
     */
    protected function _initCategories()
    {
        $collection = Mage::getResourceModel('catalog/category_collection')->addNameToResult();
        /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
        foreach ($collection as $category) {
            $structure = explode('/', $category->getPath());
            $pathSize = count($structure);
            if ($pathSize > 1) {
                $path = array();
                for ($i = 1; $i < $pathSize; $i++) {
                    $path[] = $collection->getItemById($structure[$i])->getName();
                }
                $rootCategoryName = array_shift($path);
                if (!isset($this->_categoriesWithRoots[$rootCategoryName])) {
                    $this->_categoriesWithRoots[$rootCategoryName] = array();
                }
                $index = implode('/', $path);
                $this->_categoriesWithRoots[$rootCategoryName][$index] = $category->getId();
                if ($pathSize > 2) {
                    $this->_categories[$index] = $category->getId();
                }
            }
        }
        return $this;
    }

}

答案 1 :(得分:5)

一个简短的便携式片段:

// load all category paths
$all_category_paths = array();
$categories = Mage::getResourceModel('catalog/category_collection')->addAttributeToSelect('name')->getItems();


foreach( $categories as $_category){
    $path = array_slice(explode('/', $_category->getPath()),2);//remove array_slice if you want to include root category in path
    foreach($path as $_k => $_v){
        $path[$_k]=str_replace('/','\/', $categories[$_v]->getName());
    }
    $all_category_paths[$_category->getId()]= strtolower(join('/',$path));
}

然后得到你的路径:

$all_category_paths[$category_id]