2013-01-16 5 views
1

Magento Extension Development를 처음 사용하고 Extension 내에서 카테고리 및 하위 카테고리를 만드는 가장 좋은 방법은 궁금합니다. 현재 진행중인 Extension은 ERP 시스템의 제품 데이터를 동기화하는 것입니다. 확장은 서버 (사용자/pwd/etc.)에 대한 연결을위한 데이터를 보유하는 시스템 -> 구성 대화 상자에서 작동합니다. 이제 Ajax 요청을 통해 연결하거나 Soap 호출을 사용하는 것이 더 나은지 궁금합니다. Ajax는이 경우 약 700 개의 제품에 대해 매우 느린 것으로 보입니다. 그래서 무엇을 제안합니까?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

당신은 마 젠토 모델을 사용한다 :

이 하위 범주와 범주를 만듭니다

/** 
* 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

답장을 보내 주셔서 감사합니다. 이것은 실제로 도움이됩니다. 카테고리가 이미 존재하는지 확인하는 것은 어떻습니까? 이것에 대한 조언이 있습니까? 현재 모든 ID/이름을 읽으려면 getTreeModel() 메서드를 사용하고 있습니다. –

+0

한가지 더, ParentId()가 전달 될 때 경로가 자동으로 설정된다고 가정합니다. –

+0

경로가 Mage_Catalog_Model_Resource_Category :: _ afterSave()에 설정됩니다. – Zyava

관련 문제