如何使用API​​ SOAP在Magento中创建子类别?

时间:2013-10-03 21:27:41

标签: api magento soap categories

我正在使用Magento标准API SOAP来创建类别和子类别。

以下是我用来插入类别的代码:

$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但没有结果。

你们中的任何人都经历过这招?谢谢

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分: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健康与化妆品 头发护理健康和化妆品头发护理

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

谢谢 Dhananjay