我知道这是一个 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。这对我有用。但是,我认为这可以提高效率。
谢谢丹南杰