2013-10-03 23 views
0

我使用Magento标准API SOAP来创建类别和子类别。如何使用API​​ SOAP在Magento中创建子类别?

这里是我使用插入类代码:

$client = new SoapClient('http://magentohost/api/soap/?wsdl'); 

$session = $client->login('apiUser', 'apiKey'); 
$result = $client->call($session, 'catalog_category.create', array(2, array(
'name' => 'Category name', 
'is_active' => 1, 
'position' => 1, 

'available_sort_by' => 'position', 
'custom_design' => null, 
'custom_apply_to_products' => null, 
'custom_design_from' => null, 
'custom_design_to' => null, 
'custom_layout_update' => null, 
'default_sort_by' => 'position', 
'description' => 'Category description', 
'display_mode' => null, 
'is_anchor' => 0, 
'landing_page' => null, 
'meta_description' => 'Category meta description', 
'meta_keywords' => 'Category meta keywords', 
'meta_title' => 'Category meta title', 
'page_layout' => 'two_columns_left', 
'url_key' => 'url-key', 
'include_in_menu' => 1, 
))); 

胡麻这里一切正常,并取得成功,但我真的很迷惑怎样把子类别的我已经创建的类别。我试过使用catalog_category.move但没有结果。

你们有没有经历过这个诡计?谢谢

+0

必须设置为子cateogory路径像' '路径'=> 1/2 /'。 – Zaheerabbas

+0

好的,这是一个好主意,但它不让我首先声明entity_id,在这种情况下magento创建不是我,我试图插入实体id像'entity_id'=> $ some_value_comes_form_DB和它不允许我这样做,如果可能的话,我可以声明我的路径为'path'=>'1/2/$ somevalue_from_DB/$ other_value'。问题是我从另一个数据库调用创建这些类别,并且他们有自己的Keys和id,这些必须声明。任何想法? – DanielDake

+0

是的,当然你可以设置你自己的实体ID。我最近在这种情况下工作,并获得成功与我自己的id创建类别。我只是把我的代码作为答案below.Please检查它可能会给一些想法创建类别根据您的要求。 – Zaheerabbas

回答

2
$category = Mage::getModel('catalog/category'); 
$category->setStoreId(0); // 0 = default/all store view. If you want to save data for a specific store view, replace 0 by Mage::app()->getStore()->getId(). 

//if update 
if ($id) { 
    $category->load($id); 
} 

$general['name'] = "My Category"; 
$general['path'] = "1/3/"; // catalog path here you can add your own ID 
$general['description'] = "Great My Category"; 
$general['meta_title'] = "My Category"; //Page title 
$general['meta_keywords'] = "My , Category"; 
$general['meta_description'] = "Some description to be found by meta search robots. 2"; 
$general['landing_page'] = ""; //has to be created in advance, here comes id 
$general['display_mode'] = "PRODUCTS"; //static block and the products are shown on the page 
$general['is_active'] = 1; 
$general['is_anchor'] = 0; 
$general['page_layout'] = 'two_columns_left'; 

//$general['url_key'] = "cars";//url to be used for this category's page by magento. 
//$general['image'] = "cars.jpg"; 


$category->addData($general); 

try { 
    $category->setId(255); // Here you cant set your own entity id 
    $category->save(); 
    echo "Success! Id: ".$category->getId(); 
} 
catch (Exception $e){ 
    echo $e->getMessage(); 
} 
+0

感谢您的回复,我有一个问题,你把这段代码放在肥皂下?你的回答很有道理非常感谢你 – DanielDake

+0

你为什么要使用soap api?它也可以使用脚本。 – Zaheerabbas

+0

但是,这只是在magento里面做的,用肥皂我可以直接从我的本地机器上做,比如创建,更新,删除。但也与SOAP我可以创建产品,属性,价格规则等..这就是为什么我想使用Magento SOAP – DanielDake

0

我知道这是一个2岁的线程。但我仍然发布我的答案。它可能有助于某人。

我是Magento的新手,这是我的第一个项目。所以,不确定下面的代码是否真的非常有效。我用SOAP catalog_category.create:

<?php 
/** 
*      ---NOTE---NOTE---NOTE--- 
* Upload file must contain only those fields where the value is neither null nor blank. 
* Default values are already set for all the fields. 
* @var unknown 
*/ 

$target_dir = "uploads/"; 
$filename = "category_tree_create_1443881125.csv"; 
$target_file = $target_dir . $filename; 

//Web service sessoin initialization 
$proxy = new SoapClient('http://localhost/magento-dhana/api/v2_soap/?wsdl'); // TODO : change url 
$sessionId = $proxy->login('yourapiuser', 'apiuserpassword'); // TODO : change login and pwd if necessary 

    $categoryIds = array(); 

    //prepare the initial attributes array required to upload 
    $attrbutesFinal = array(

      'name' => '', 
      'is_active' => 1, 
      'position' => 1, 
      'available_sort_by' => array('position'), 
      'custom_design' => null, 
      'custom_apply_to_products' => null, 
      'custom_design_from' => null, 
      'custom_design_to' => null, 
      'custom_layout_update' => null, 
      'default_sort_by' => 'position', 
      'description' => '', 
      'display_mode' => null, 
      'is_anchor' => 0, 
      'landing_page' => null, 
      'meta_description' => ' ', 
      'meta_keywords' => ' ', 
      'meta_title' => ' ', 
      'page_layout' => '', 
      'url_key' => '', 
      'include_in_menu' => 1, 
    ); 


     $fileURL = $target_file; 
     //$content = file_get_contents($fileURL, false); 
     $content = file($fileURL); 

     $rowCount = 0; 
     foreach ($content as $row){ 

      $row = str_replace("\r\n", "", $row); 
      $rowAsArray = explode(",", $row); 

      if($rowCount === 0){ 
       //we collect the header row here and keep it separate 
       $headerRow = $rowAsArray; 
       $columns = count($headerRow); 
      }else{ 

       for ($i = 0; $i < $columns; $i++) { 
        //an associative array is created with header row columns as keys and 
        //columns of subsequent rows as values 
        //This is the format required by the web service as well 
        if ($headerRow[$i] === 'available_sort_by'){ 
         //THIS COLUMN NEEDS AN ARRAY 
         $attrbutesFinal[$headerRow[$i]] = array($rowAsArray[$i]); 
        }else { 

         $attrbutesFinal[$headerRow[$i]] = $rowAsArray[$i]; 
        } 

       } 
       //create category ---> SOAP call 
       //if parent calumn has the ID (checked if it is number or not by calling intval()) then 
       //call soap directly using it as 'parent' 
       if (intval($attrbutesFinal['parent']) > 1){ 

        $result = $proxy->catalogCategoryCreate($sessionId, intval($attrbutesFinal['parent']), $attrbutesFinal); 
       }else { 

        foreach ($categoryIds as $key => $val){ 

         if ($attrbutesFinal['parent'] === $key){ 
          $result = $proxy->catalogCategoryCreate($sessionId, intval($val), $attrbutesFinal); 
         } 
        } 
       } 
       if ($result > 0){ 

        //File contains category name as parent but the web service needs the 
        //ID. while uploading new categories, we do not know the category ids yet 
        //So, once the category is created, we collect its ID here 
        $categoryIds[$attrbutesFinal['name']] = $result; 
       } 
      } 

      //var_dump($rowAsArray); 

      $rowCount += 1; 
     } 

     echo "<strong>Categories created ===> </strong></br>"; 
     var_dump($categoryIds); 
?> 

在这里,一旦创建了父类,我收集ID创建的实体,并用它来创建它的孩子。

我用下面的数据CSV文件:

名家长说明 健康和化妆品63健康和化妆品 头发医疗卫生和化妆品头发护理

它假定我知道有一类的实体ID ,根或根目录下的某个类别。 这适用于我。但是,我认为这可以提高效率。

谢谢 Dhananjay

相关问题