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, <sku>, 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 :)

10 thoughts on “Retrieving Product Attributes from Magento's V2 API

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

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

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

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

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

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>