如何通过Soap / Java在Magento中创建具有其他属性的产品

美好的一天!

我想使用Magento的SOAP API来管理产品目录,属性等。我正在运行以下配置: –

  • Magento 1.6
  • Soap API WS-I合规性
  • Mac OSX Lion
  • Mamp 2.0.5

如果有人想要创建新产品,则需要设置产品对象的一些属性。 以下代码将演示我执行此操作的方法:

public int createProduct(DatabaseProduct product) { ArrayOfString categories = new ArrayOfString(); categories.getComplexObjectArray().add(categoryID); createEntity.setCategoryIds(categories); CatalogProductCreateEntity createEntity = populateCreateOrUpdateEntity(product); CatalogProductCreateRequestParam param = new CatalogProductCreateRequestParam(); param.setSessionId(sessionId); param.setSet(setId); param.setSku(product.getSku()); param.setType("simple"); param.setStore(storeId); param.setProductData(createEntity); CatalogProductCreateResponseParam response = service.catalogProductCreate(param); return response.getResult(); } private CatalogProductCreateEntity populateCreateOrUpdateEntity(DatabaseProduct product) { CatalogProductCreateEntity createEntity = new CatalogProductCreateEntity(); createEntity.setShortDescription(product.getDescription().substring(0, 20) + "..."); createEntity.setDescription(product.getDescription()); createEntity.setName(product.getName()); createEntity.setPrice(String.valueOf(product.getPrice())); createEntity.setStatus("1"); //active createEntity.setVisibility("4"); //visible in search/catalog createEntity.setWeight("70"); //some value createEntity.setTaxClassId("2"); //standard AssociativeArray attributes = new AssociativeArray(); AssociativeEntity attr1 = new AssociativeEntity(); attr1.("attribute1_key"; attr1.("attribute1_value"); attributes.getComplexObjectArray().add(attr1); AssociativeEntity attr2 = new AssociativeEntity(); attr2.("attribute2_key"); attr2.("attribute2_value"); attributes.getComplexObjectArray().add(attr2); createEntity.setAdditionalAttributes(attributes); return createEntity; } 

我意识到我收到了写入Magento的“ system.log ”的错误。

 2012-01-21T09:41:01+00:00 DEBUG (7): First parameter must either be an object or the name of an existing class/opt/website/magento/app/code/core/Mage/Catalog/Model/Product/Api/V2.php 

我可以在第265行的“ V2.php ”文件中本地化错误。根据php.net文档,“ property_exists() ”方法只能检查对象中的字段。 事实上,“ $productData ”变量包含一个名为“ additional_attributes ”的属性,该属性是数组类型。 因此,执行此代码将导致错误。

此外,我不知道通过使用Magento的SOAP API V2来重现对象“ $productData ”对象的结构。

如果我在第270行检查这段代码(“ foreach ”循环),它表示有一个对象(“ $productData ”)持有一个数组(“ additional_attributes ”),它再次封装了一组键/值对(如果我我是对的)

 253 protected function _prepareDataForSave ($product, $productData) 254 { 255 if (property_exists($productData, 'website_ids') && is_array($productData->website_ids)) { 256 $product->setWebsiteIds($productData->website_ids); 257 } 258 259 Mage::log("debug1"); 260 Mage::log(property_exists($productData, 'additional_attributes')); 261 262 Mage::log($productData); 263 264 if (property_exists($productData, 'additional_attributes')) { 265 if (property_exists($productData->additional_attributes, 'single_data')) { 266 267 Mage::log("---> single"); 268 Mage::log($productData->additional_attributes); 269 270 foreach ($productData->additional_attributes->single_data as $_attribute) { 271 $_attrCode = $_attribute->key; 272 $productData->$_attrCode = $_attribute->value; 273 } 274 } 275 if (property_exists($productData->additional_attributes, 'multi_data')) { 276 277 Mage::log("---> multi"); 278 Mage::log($productData->additional_attributes); 279 280 foreach ($productData->additional_attributes->multi_data as $_attribute) { 281 $_attrCode = $_attribute->key; 282 $productData->$_attrCode = $_attribute->value; 283 } 284 } 285 286 Mage::log("debugXXX"); 287 unset($productData->additional_attributes); 288 } 289 290 Mage::log("debug2"); 291 292 foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) { 293 $_attrCode = $attribute->getAttributeCode(); 294 if ($this->_isAllowedAttribute($attribute) && (isset($productData->$_attrCode))) { 295 $product->setData( 296 ... etc ... 

这似乎是一个错误。 所以这是我的问题。

我是否正确地称这是一个编程问题,应该在bug基础上发布? 有没有办法克服这个问题? 我应该从上面重写部分php.code以满足我处理产品信息以正确创建产品的需要吗?

提前致谢

  $productData ( [name] => testname [description] => testdescription [short_description] => shorttestdescription [weight] => 70 [status] => 1 [visibility] => 4 [price] => 359.0 [tax_class_id] => 2 [additional_attributes] => Array ( [attribute1] => 999.0 [attribute2] => testcontent ) ) 

来自SoapUI生成的WSDL的CatalogProductCreate-Call:

     ? ? ? ?     ?     ?   ?  ?  ?  ?  ?  ?  ?  ?    ?     ?   ?  ?  ?  ?  ?  ?  ?      ?  ?  ?  ?    ?  ?  ?  ?  ?  ?     ? ?      ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ?    ?    

对于V2 SOAP API,我们需要将additional_attributes嵌套在multi_data或single_data层中?

看app / code / core / Mage / Catalog / Model / Product / Api / V2.php#256我觉得我们需要用

 $manufacturer = new stdClass(); $manufacturer->key = "manufacturer"; $manufacturer->value = "20"; $additionalAttrs['single_data'][] = $manufacturer; 

要么

 $manufacturer = new stdClass(); $manufacturer->key = "manufacturer"; $manufacturer->value = "20"; $additionalAttrs['multi_data'][] = $manufacturer; 

使用方式如下:

  $productData = new stdClass(); $additionalAttrs = array(); // manufacturer from one of the two above ^ $productData->name = $data['name']; $productData->description = $data['description']; $productData->short_description = $data['short_description']; $productData->weight = 0; $productData->status = 2; // 1 = active $productData->visibility = 4; //visible in search/catalog $productData->category_ids = $data['categories']; $productData->price = $data['price']; $productData->tax_class_id = 2; // 2=standard $productData->additional_attributes = $additionalAttrs; // Create new product try { $proxy->catalogProductCreate($sessionId, 'virtual', 9, $sku, $productData); // 9 is courses } catch (SoapFault $e) { print $e->getMessage(); //Internal Error. Please see log for details. exit(); } 

我实际上必须修补app / code / core / Mage / Catalog / Model / Product / Api / V2.php以使此调用在版本1.6.2.0中工作。

现有代码检查’additional_attributes’属性下的“single_data”或“multi_data”属性,并尝试将这些属性作为关联数组进行迭代:

 if (property_exists($productData, 'additional_attributes')) { if (property_exists($productData->additional_attributes, 'single_data')) { foreach ($productData->additional_attributes->single_data as $_attribute) { $_attrCode = $_attribute->key; $productData->$_attrCode = $_attribute->value; } } if (property_exists($productData->additional_attributes, 'multi_data')) { foreach ($productData->additional_attributes->multi_data as $_attribute) { $_attrCode = $_attribute->key; $productData->$_attrCode = $_attribute->value; } } } 

我认为问题是因为我们传递了关联数组结构:

  ... ...   ... ...  

直接在additional_attribtutes块下,那么它实际上是必须迭代的additional_attributes属性来拉取属性键/值,所以我添加了第三个if块:

  if (gettype($productData->additional_attributes) == 'array') { foreach ($productData->additional_attributes as $k => $v) { $_attrCode = $k; $productData->$_attrCode = $v; } } 

使用该代码,我的自定义属性正在添加/更新。

这是一个示例请求:

      100fe1f9d0518b0fd0ed49cc460c1fa6 1  product 3    feed_id 56920   feed_active 1       

这不是一个错误。 我已经使用这个Magento SOAP API V2一年多了,Magento团队在这里应用的概念是绝对正确的。

首先,每次使用SOAP API V2的任何人都必须完全检查相应Magento的WSDL,以便他可以正确地创建/调用消息。 加载SOAP API V2的WSDL的URL是“ /api/v2_soap/index/wsdl/1 ”。

现在回到你的问题,元素“ additional_attributes ”的类型是“ associativeArray ”,这意味着它的XML应该类似于: –

    attribute_1_code attribute_1_value_as_defined_in_database   attribute_2_code attribute_2_value_as_defined_in_database   

上述XML格式的一个例子是: –

   color  56    manufacturer 87    

因此,您的“ $productData ”变量必须包含如下值: –

 $productData ( [name] => testname [description] => testdescription [short_description] => shorttestdescription [weight] => 70 [status] => 1 [visibility] => 4 [price] => 359.0 [tax_class_id] => 2 [additional_attributes] => Array ( [0] => Array ( [key] => attribute1 [value] => 999.0 ) [1] => Array ( [key] => attribute2 [value] => testcontent ) ) ) 

希望能帮助到你。

斯托伊西几乎是完全正确的。 只需修复一个答案:您添加的第三个条件也将在前两个条件中评估为true (因此,如果您在非WSI兼容模式下使用SOAPv2,则将这些属性添加两次)。

条件链应如下所示:

  // ... if (property_exists($productData->additional_attributes, 'single_data')) { foreach ($productData->additional_attributes->single_data as $_attribute) { $_attrCode = $_attribute->key; $productData->$_attrCode = $_attribute->value; } } if (property_exists($productData->additional_attributes, 'multi_data')) { foreach ($productData->additional_attributes->multi_data as $_attribute) { $_attrCode = $_attribute->key; $productData->$_attrCode = $_attribute->value; } } else if (! property_exists($productData->additional_attributes, 'single_data')) { foreach ($productData->additional_attributes as $key => $value) { $productData->$key = $value; } } // ... 

第三个条件断言'single_data''multi_data' 都不additional_attributes属性。


虽然,在我看来,修复此错误的正确方法是编辑“wsi.xml”文件,使其与wsdl.xml一致(反之亦然)。

我正在使用SOAP API在magento商店中输入产品。 这是完整的代码

在多选自定义属性的情况下。

  $arrProductTime = explode(',', '136,139'); $result = $client->catalogProductCreate($session, 'simple', $attributeSet->set_id, 'product_sku1234', array( 'categories' => array(36), 'websites' => array(1), 'name' => 'my_pdt1008', 'description' => 'my_pdt1', 'short_description' => 'my_pdt1000', 'weight' => '11', 'status' => '1', 'url_key' => 'product-url-key1', 'url_path' => 'product-url-path1', 'visibility' => '4', 'price' => '100', 'tax_class_id' => 1, 'meta_title' => 'Product meta title1', 'meta_keyword' => 'Product meta keyword1', 'meta_description' => 'Product meta description1', 'stock_data' => array('qty'=>'100','is_in_stock'=>1,'manage_stock'=>1), 'additional_attributes' => array('multi_data' => array(array('key' => 'product_time', 'value' => $arrProductTime))) ));