2

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

main category/sub 
main category/sub/sub

它始终是名称,而不是 url 或东西。是否可以通过此路径获取类别 ID?

4

2 回答 2

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

}
于 2013-10-08T12:54:33.637 回答
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]
于 2013-10-09T02:30:34.750 回答