Magento API v2和C# - 在添加产品时设置自定义属性

时间:2012-01-04 20:26:12

标签: c# api magento soap

我添加了自定义属性,代码为“my_price”,“商店所有者的目录输入类型”设置为“价格”,并将其分配给“默认”(仅限)属性集。

现在,我想在每次使用API​​ v2(C#)添加/更新产品时设置其值。这是不起作用的代码(未设置值):

// Connect & Auth:
Mage_Api_Model_Server_V2_HandlerPortTypeClient handler = new Mage_Api_Model_Server_V2_HandlerPortTypeClient();
session_id = handler.login(username, api_key);

// Getting attributes set:
catalogProductAttributeSetEntity[] attributeSets;
attributeSets = handler.catalogProductAttributeSetList(session_id);
attributeSet = attributeSets[0];
string attributeset_id = attributeSet.set_id.ToString();

// Adding product:
catalogProductCreateEntity mageProduct = new catalogProductCreateEntity();
// (...) setting product's name, sku, etc.
associativeEntity AdditionalAttributes = new associativeEntity();
AdditionalAttributes.key = "my_price";
AdditionalAttributes.value = "12,33";
associativeEntity[] AssociativeEntity = new associativeEntity[1];
AssociativeEntity[0] = AdditionalAttributes;
mageProduct.additional_attributes = AssociativeEntity;
handler.catalogProductCreate(session_id, "simple", attributeset_id, sku, mageProduct, "default");

我做错了什么?

3 个答案:

答案 0 :(得分:5)

Magento 1.6.1.0有一个错误导致其他属性错误。

我已将Magento升级到1.6.2.0,问题消失了,其他属性也完美无缺。

它如何运作的快速示例:

associativeEntity[] AdditionalAttributes = new associativeEntity[1];
associativeEntity AdditionalAttribute = new associativeEntity();
AdditionalAttribute.key = "myprice";
AdditionalAttribute.value = getOriginalPrice(prices).ToString();
AdditionalAttributes[0] = AdditionalAttribute;
catalogProductAdditionalAttributesEntity AdditionalAttributesEntity = new catalogProductAdditionalAttributesEntity();
AdditionalAttributesEntity.single_data = AdditionalAttributes;

mageProduct.additional_attributes = AdditionalAttributesEntity;

希望它有所帮助。

答案 1 :(得分:2)

试试这个,让我知道结果。

AdditionalAttributes.key = "myPrice";

答案 2 :(得分:0)

handler.catalogProductCreate(session_id, "simple", attributeset_id, sku, mageProduct, "default");

提供有效的storeview而不是默认,例如试试这个:

handler.catalogProductCreate(session_id, "simple", attributeset_id, sku, mageProduct, "1");