Magento News

 

How do I create a product with additional attributes in Magento via Soap/Java

Good day!

I would like to use the magenta’s soap api to manage the product catalog, attributes etc. I running following configuration

Magento 1.6
Soap API Ws-I compliance
Mac OSX Lion
Mamp 2.0.5

In case someone want to create a new product it is necessary to set a few properties of the product object.
Following code will demonstrate my approach to do this :

    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;
    }

I realized that I get an error written to the system.log of magento.

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

I could localize the error in the V2.php file on line 265.
According to the php.net documentation the property_exists() method only can check for fields in objects.
As a matter of fact the $productData variable holds a property called additional_attributes which is of the type array.
Therefore the execution of this code will lead to an error.

Moreover I don’t know to reproduce the object the structure of the $productData object through the use of Magento’s soap api v2.

If I examine this code (foreach loop) in line 270 it indicates that there is an object (productData) holding an array (additional_attributes) which again shall encapsulates a set of key, value pairs (if I am right)

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 ...

This seems to be a bug. So here is my question.

Am I going right to call this an programming issue which shall be posted in the bug base?
Is there a way to get over this issue?
Shall I rewrite parts of the php.code from above to satisfy my need to handle product information to create a product properly ?

Thanks in advance

How do I create a product with additional attributes in Magento via Soap/Java

Possibly Related Posts:


 

Leave a Reply