2013-01-16 84 views
1

我是Magento Extension Development的新手,想知道從擴展內部創建類別和子類別的最佳方法。我正在開發的擴展是同步ERP系統的產品數據。該擴展使用系統 - >配置對話框來保存連接到服務器的數據(user/pwd/etc)。現在我想知道,如果最好通過Ajax請求連接或使用Soap調用。對於約700種產品,Ajax在這種情況下似乎非常緩慢。所以你有什麼建議?在Magento Extension中創建類別和子類別

此外,我有點卡住創建類別和子類別。有沒有簡單的方法來做到這一點。我發現了一些創建類別的東西,然後使用 - > move()函數。此外,我想知道該類別的「路徑」是否對創建子類別至關重要。

回答

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

我希望這可以幫助別人。歡呼聲

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(); 
+0

謝謝您的回覆。這實際上是有幫助的。如何檢查類別是否已經存在?你有什麼建議嗎?目前我正在使用getTreeModel()方法來讀取所有ID /名稱。 –

+0

還有一件事,我認爲當ParentId()被傳遞時,路徑會自動設置? –

+0

路徑將在Mage_Catalog_Model_Resource_Category :: _ afterSave() – Zyava