【问题标题】:Creating categories and sub-categories in Magento Extension在 Magento Extension 中创建类别和子类别
【发布时间】:2013-01-16 19:36:29
【问题描述】:

我是 Magento 扩展开发的新手,想知道从扩展中创建类别和子类别的最佳方式是什么。我正在研究的扩展是同步来自 ERP 系统的产品数据。该扩展使用系统->配置对话框运行,该对话框保存连接到服务器的数据(用户/密码/等)。现在我想知道,是通过 Ajax 请求连接还是使用 Soap 调用更好。在这种情况下,对于大约 700 个产品,Ajax 似乎非常慢。那你有什么建议呢?

此外,我在创建类别和子类别方面有点卡住。有没有简单的方法来做到这一点。我发现了一些关于创建类别的东西,然后使用 ->move() 函数。此外,我想知道类别的“路径”对于创建子类别是否必不可少。

【问题讨论】:

    标签: magento magento-1.7 categories


    【解决方案1】:

    您应该使用 magento 模型:

    创建带有子类别的类别:

    /**
     * After installation system has two categories: root one with ID:1 and Default category with ID:2
     */
    /** @var $category1 Mage_Catalog_Model_Category */
    $category1 = Mage::getModel('catalog/category');
    $category1->setName('Category 1')
        ->setParentId(2)
        ->setLevel(2)
        ->setAvailableSortBy('name')
        ->setDefaultSortBy('name')
        ->setIsActive(true)
        ->setPosition(1)
        ->save();
    
    /** @var $category2 Mage_Catalog_Model_Category */
    $category2 = Mage::getModel('catalog/category');
    $category2->setName('Category 1.1')
        ->setParentId($category1->getId()) // set parent category which was created above
        ->setLevel(3)
        ->setAvailableSortBy('name')
        ->setDefaultSortBy('name')
        ->setIsActive(true)
        ->setIsAnchor(true)
        ->setPosition(1)
        ->save();
    

    【讨论】:

    • 感谢您的回复。这实际上很有帮助。检查类别是否已经存在呢?你对此有什么建议吗?目前我正在使用 getTreeModel() 方法来读取所有的 ids/names。
    • 还有一件事,我假设在传递 ParentId() 时会自动设置路径?
    • 路径将在 Mage_Catalog_Model_Resource_Category::_afterSave() 中设置
    • 检查类别是否存在:$category = Mage::getModel('catalog/category'); $category->load($name, 'name'); if ($category->getId()) { // category exists }
    • 抱歉,这根本不起作用。我设法使用以下代码创建了所有类别:请参阅答案。
    【解决方案2】:
    public static function addCatalogCategory($item, $id, $storeId = 0) {
        /*
         * resource for checking category exists
         * http://fishpig.co.uk/blog/load-a-category-or-product-by-an-attribute.html
         */
        $categories = Mage::getResourceModel('catalog/category_collection');
        // Select which fields to load into the category
        // * will load all fields but it is possible to pass an array of
        // select fields to load
        $categories->addAttributeToSelect('*');
        // Ensure the category is active
        $categories->addAttributeToFilter('is_active', 1);
        // Add Name filter
        $categories->addAttributeToFilter('name', $item->GROUP_NAME);
        // Limit the collection to 1 result
        $categories->setCurPage(1)->setPageSize(1);
        // Load the collection
        $categories->load();
    
        if ($categories->getFirstItem()->getId()) {
            $category = $categories->getFirstItem();
            return $category->getId();
        }
    
        /* get category object model */
        $category = Mage::getModel('catalog/category');
        $category->setStoreId($storeId);
        $data = array();
        /* if the node is root */
        if (Heliumv_Synchronization_Helper_Data::xml_attribute($item, 'type') == 'root') {
            $data['category']['parent'] = 2; // 2 top level id
        } else {
            /* is node/leaf */
            $data['category']['parent'] = $id;
        }
    
        $data['general']['path'] = $item->PARENT_ID;
        $data['general']['name'] = $item->GROUP_NAME;
        $data['general']['meta_title'] = "";
        $data['general']['meta_description'] = "";
        $data['general']['is_active'] = "1";
        $data['general']['url_key'] = "";
        $data['general']['display_mode'] = "PRODUCTS";
        $data['general']['is_anchor'] = 0;
    
        /* add data to category model */
        $category->addData($data['general']);
    
        if (!$category->getId()) {
            $parentId = $data['category']['parent'];
            if (!$parentId) {
                if ($storeId) {
                    $parentId = Mage::app()->getStore($storeId)->getRootCategoryId();
                } else {
                    $parentId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
                }
            }
            $parentCategory = Mage::getModel('catalog/category')->load($parentId);
            $category->setPath($parentCategory->getPath());
        }
    
        $category->setAttributeSetId($category->getDefaultAttributeSetId());
    
        try {
            $category->save();
    
            return $category->getId();
        } catch (Exception $e) {
            echo Mage::log($e->getMessage());
        }
    }
    

    我希望这对某人有所帮助。干杯

    【讨论】:

      猜你喜欢
      • 2011-07-22
      • 1970-01-01
      • 1970-01-01
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      相关资源
      最近更新 更多