在Magento Extension中创建类别和子类别

时间:2013-01-16 19:36:30

标签: magento magento-1.7 categories

我是Magento Extension Development的新手,并想知道哪种是在扩展中创建类别和子类别的最佳方式。我正在研究的扩展是同步来自ERP系统的产品数据。扩展程序使用System->配置对话框进行操作,该对话框保存与服务器连接的数据(user / pwd / etc.)现在我想知道,如果通过Ajax请求连接或使用Soap调用更好。对于大约700种产品,Ajax在这种情况下似乎非常缓慢。所以你有什么建议?

此外,我通过创建类别和子类别而陷入困境。有没有简单的方法来做到这一点。我在创建类别时发现了一些内容,然后使用 - > move()函数。此外,我想知道该类别的“路径”是否对创建子类别至关重要。

2 个答案:

答案 0 :(得分:0)

你应该使用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();

答案 1 :(得分:0)

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

我希望这有助于某人。欢呼声