Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

soap

api

magento

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?

like image 842
Cleankod Avatar asked Jan 04 '12 20:01

Cleankod


2 Answers

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.

like image 147
Cleankod Avatar answered Oct 26 '22 10:10

Cleankod


Try this and let me know the result.

AdditionalAttributes.key = "myPrice";
like image 41
Oğuz Çelikdemir Avatar answered Oct 26 '22 10:10

Oğuz Çelikdemir