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



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

你们中有人经历过这个技巧吗?谢谢<</p>

div class="one_answers">
$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();
}
我知道

这是一个 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("rn", "", $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。这对我有用。但是,我认为这可以提高效率。

谢谢达南杰

相关内容

  • 没有找到相关文章

最新更新