Retrieving Product Attributes from Magento’s V2 API
I’ve been working with the API for Magento in recent weeks and I had a bit of a struggle explaining to the V2 API which attributes of a product I wanted to retrieve. Actually I had issues talking to the V2 API at all, but that’s a different post so I’ll skate over those for now. Instead I thought I’d share (or rather, record for the next time I have the same problem!) how to specify which attributes about a product to retrieve.
It actually wasn’t complicated but without V2 API documentation, it wasn’t at all clear what to feed in to get the result I was looking for. It turns out you can just pass an array of desired attributes, shown here with the info method from the product_catalog:
// connect to soap server
$client = new SoapClient('http://magentoinstall.local/api/v2_soap?wsdl=1');
// log in
$session = $client->login('user', 'pass');
// product info
$attributes = new stdclass();
$attributes->attributes = array('product_title', 'description', 'short_description', 'price');
$list = $client->catalogProductInfo($session, , NULL, $attributes);
There were two tricks – one was realising that I could pass that final (undocumented) argument, and the other was understanding how to format that. Hopefully anyone doing battle with the same thing will find this post and get over this little challenge much faster than I did :)
Thanks for your help. I’ve been struggling with this one for hours. Once you know how it’s done it’s really easy to get something done with Magento.
Thank you for your help. I’ve been searching for this some hours no.
I used your code, but I ran into an error message: Parse error: syntax error, unexpected ‘<' in C:\wamp\www\magento161\apitest.php on line 15. The line with '’ is the problem, I think.
Your help will be appreciated.
Best regards,
Bert
I ment <sku> …
You need to replace with your actual product SKU – then you can get the attributes for it
There is a bug on Magento if you try to select numeric SKU. If you are trying something like:
[code]print_r($client->catalogProductInfo($session, “12345678”));[/code]
If you get this error:
[code]Product not exists.[/code]
And you are sure this SKU exists, your magento has this bug.
A fast workaround is add for example a space to the string, to have this:
[code]print_r($client->catalogProductInfo($session, “12345678 “));[/code]
Note the space after the 8.
That is a fabulous tip, and what a horrible bug. Thanks for posting!
I’ve seen this elsewhere in Magento code where the assumption is made that skus are non-numeric.
It looks something like this. I don’t remember the exact method.
// $search passed into a method call
if (is_numeric($search)) {
// Assumes $search is an id if numeric
$product = Mage::getModel(‘catalog/product’)->load($search);
} else {
$product = Mage::getModel(‘catalog/product’)->loadByAttribute(‘sku’, $search);
}
Hi Lorna – many thanks for the post. I’ve got this running with the attributes that you used, but if I try and retrieve ‘image’ attribute then it isn’t returned. I’m having a similar problem with custom attributes. Is this a problem that you’ve encountered?
Looking into this further, it looks as though to get to some attributes, e.g. ‘manufacturer’, you have to set up ‘$attributes->additional_attributes’ rather than ‘$attributes->attributes’:
[code]$attributes->additional_attributes = array(‘manufacturer’);[/code]
Not sure what the reasoning is behind this…
Hi Lorna, thank you for this post. It has saved me a LOT of time. Thank you! For those who are interested, here is a python snippet to get the additional_attributes:
adds = {“additional_attributes”:[self_deffined_additional_attribute]}
catalogProductInfo(sessionID,sku, None, adds)
Hello all together! This is very interesting!
We add some custom attributes to our products, like “product_processing_status”.
Now we have a order with some products.
I read the product data from the order table with the SOAP V2 API.
How i can retrieve the custom attribute “product_processing_status” with SOAP V2 and update them with a new value in the order?
Do you have any ideas?
Thank you!
Tommy
Try this
$attributes = new stdclass();
$attributes->additional_attributes = array(‘jew_stone’,’jew_metal’,’jew_metal_weight’);
$result = $proxy->catalogProductInfo((object)array(‘sessionId’ => $sessionId->result, ‘productId’ => 10,’attributes’ => $attributes));
This is not working i create some custom attribute and i try to call it but it always showing basic attributes only. there is no changes in the result
It is working after i make some change in the code as follow
$result = $client->catalogProductInfo((object)array(‘sessionId’ => $session->result, ‘productId’ => $product->result->product_id,’attributes’ => $attributes));
Thanks :)