0

我正在使用 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 但没有结果。

你们中有人经历过这个技巧吗?谢谢

4

2 回答 2

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();
}
于 2013-10-07T04:02:23.263 回答
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。这对我有用。但是,我认为这可以提高效率。

谢谢丹南杰

于 2015-10-04T06:59:27.100 回答