简体   繁体   中英

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.

Now, I want to set its value, everytime I add/update product with 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.

I have upgraded my Magento to 1.6.2.0 and the problem disappeared and additional attributes works perfectly.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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