Web services API filter operators
23 Jun 2009, by Peter Spiller
2 Comments · Posted in Magento

    Many of the Magento web services API methods facilitate listing certain types of data - customers, products, orders, etc. However, when using these methods we rarely want to retrieve every record. Luckily there are a number of filter operators that can be used to narrow the result set that will be returned.

    The following is a basic example of how to get a list of all customers in PHP:

    
    $server = new SoapClient('http://magentohost/api/soap/?wsdl');
    $sessionID = $server->login('api_user', 'api_key');
    $server->call($sessionID, 'customer.list');
    

    Now, suppose we want to retrieve only those customers updated since 10/06/2009. We can do this by adding a filter operator to the last line:

    
    $server->call($sessionID, 'customer.list', array(array('updated_at' => array('from' => '2009-06-10'))));
    
    Alternatively, we could get only those customers whose email addresses start with 'jim':
    
    $server->call($sessionID, 'customer.list', array(array('email' => array('like' => 'jim%'))));
    

    There are quite a few filter operators available which directly correspond to SQL operators, since that's what they are translated to internally.

    OperatorExplanation
    fromAfter the given timestamp
    toBefore the given timestamp
    likeLike the given text (SQL syntax, using % for wildcards)
    nlikeNot like the given text (SQL syntax)
    eqEqual to the given value
    neqNot equal to the given value
    inIn the given array
    ninNot in the given array
    nullIs null
    notnullIs not null
    gtGreater than
    ltLess than
    gteqGreater than or equal to
    lteqLess than or equal to

    Comments

    This line:
    $server->call($sessionID, 'customer.list', array('updated_at' => array('from' => '2009-06-10')));

    should be like this:
    $server->call($sessionID, 'customer.list', array(array('updated_at' => array('from' => '2009-06-10'))));

    Note the extra array(..)
    Comment by Anonymous - 10 Oct 2009 3:10:05 AM
    Thanks for pointing that out - the post has been updated.
    Comment by Peter Spiller - 20 Oct 2009 10:38:27 AM
    Comments are closed for this post