简体   繁体   English

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

[英]Magento API v2 and C# - set custom attributes whilst adding product

I have added custom attribute with the code "my_price" with "Catalog Input Type for Store Owner" set to "Price" and assigned it to the "Default" (only) attribute set. 我添加了自定义属性,代码为“my_price”,“商店所有者的目录输入类型”设置为“价格”,并将其分配给“默认”(仅限)属性集。

Now, I want to set its value, everytime I add/update product with API v2 (C#). 现在,我想在每次使用API​​ v2(C#)添加/更新产品时设置其值。 Here is the code which does not work (the value is not being set): 这是不起作用的代码(未设置值):

// 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");

What am I doing wrong? 我究竟做错了什么?

Magento 1.6.1.0 has a bug which results with additional attributes error. Magento 1.6.1.0有一个错误导致其他属性错误。

I have upgraded my Magento to 1.6.2.0 and the problem disappeared and additional attributes works perfectly. 我已将Magento升级到1.6.2.0,问题消失了,其他属性也完美无缺。

Quick example of how it works: 它的工作原理的简单示例:

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;

Hope it helps someone. 希望它可以帮助某人。

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

AdditionalAttributes.key = "myPrice";
handler.catalogProductCreate(session_id, "simple", attributeset_id, sku, mageProduct, "default");

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM