Sharing some useful tips, solutions and notes for Geeks.

Wednesday, December 6, 2017

First parameter must either be an object or the name of an existing class Magento api soap

Yesterday when i was working in Magento API V2, i met with an issue.

I was able to create product using WSDL xml in Magento. However, i was struggling to insert the additional custom attributes i have created in Magento. Whatever i do, the custom attribute values were not inserting in the fields for product.

Creating xml looks like this:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
        <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none" /> </s:Header>
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <catalogProductCreateRequestParam xmlns="urn:Magento">
            <sessionId xmlns="">**************</sessionId>
            <type xmlns="">simple</type>
            <set xmlns="">***</set>
            <sku xmlns="">**********</sku>
            <productData xmlns="">
                <websites>
                    <complexObjectArray>1</complexObjectArray>
                </websites>
                <name>Test</name>
                <description>Test</description>
                <short_description>Test</short_description>
                <weight>0.00</weight>
                <status>2</status>
                <price>0.0000</price>
                <tax_class_id>2</tax_class_id>
                <meta_description>Test</meta_description>
                <additional_attributes>
                    <complexObjectArray>
                        <key>colour</key>
                        <value>red</value>
                    </complexObjectArray>                   
                </additional_attributes>
            </productData>
        </catalogProductCreateRequestParam>
    </s:Body>
</s:Envelope>


Here whatever i do, colour was not setting for that product.

So i started debugging the code and found this error:

"First parameter must either be an object or the name of an existing class /app/code/core/Mage/Catalog/Model/Product/Api/V2.php"

Then i started investigating the above V2.php file. I understood that in this method: _prepareDataForSave, the additional attributes are getting inserted/updated.

Under if (property_exists($productData, 'additional_attributes')) { , it checks for single_data and multi_data. I have amended another if condition just below the other two if conditions.

if (gettype($productData->additional_attributes) == 'array') {
                foreach ($productData->additional_attributes as $k => $v) {
                    $_attrCode = $k;
                    $productData->$_attrCode = $v;
                }
            }

This checks whether the type receiving is in array. So the whole code will looks like this:



 And Voila, everything started importing. For dropdown attributes, i used index instead of values. For text and other attribute types, used values itself.

Just try it out and not worked, get me back via contact form.







No comments: