7

I am trying to call the Magento SOAP API and get a list of orders within a certain time period. I can connect to API and get a list of all the orders just fine but I can't (for the life of me) figure out how to filter the results... Any ideas? My Code that return all the orders is below...

$proxy = new SoapClient('http://lalala.freelunchlabs.com/api/v2_soap/?wsdl');

// create authorized session id using api user name and api key
$sessionId = $proxy->login('myusername', 'mypassword');

$filters = array(
    'created_at' => array( '>' => '2011-04-21 02:13:00'),
    'created_at' => array( '<' => '2011-04-21 02:22:00')
);

 // Get order list
$orderinfo = $proxy->salesOrderList($sessionId,array($filters));

print_r($orderinfo);

Thanks in advance!

Chuck

Chuck D
  • 1,718
  • 4
  • 19
  • 26

4 Answers4

9

In v2 of the Magento API you need to adjust your 'filters' array like so:

$params = array('complex_filter'=>
    array(
        array('key'=>'created_at','value'=>array('key' =>'from','value' => '2012-07-05 01:01:01'))
    )
);

While their API shows an example of the API v2 here:

http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/introduction#api_version_v2

this documentation doesn't indicate (as far as I can tell) that you need to replace 'filter' with 'complex_filter' when using conditional statements.

For example, you could replace

'key'=>'from"

with

'key'=>'to'

or

'key'=>'eq'

A more complete list of conditionals that you can use is here:

http://100101.kurodust.net/2008/10/24/magento-api-calls-filter-parameters/

David
  • 101
  • 1
  • 2
  • 2
    I actually was able to find a reference to the 'complex_filter' value here: http://www.magentocommerce.com/api/soap/sales/salesOrder/sales_order.list.html – David Jul 13 '12 at 20:15
  • The mentioned link about a complete list of conditionals can still be found at http://web.archive.org/web/20140902022328/http://100101.kurodust.net/2008/10/24/magento-api-calls-filter-parameters/ – user2094178 Dec 30 '17 at 05:46
9

I got no experience with the Magento SOAP 2 Api, but if the filters in V2 work the same way as with V1, you could try this:

$filters = array(
    'created_at' => array(
        'from' => '2011-04-21 02:13:00',
        'to' => '2011-04-21 02:22:00'
    )
);
Jürgen Thelen
  • 12,745
  • 7
  • 52
  • 71
8

I think it's unfortunate that only PHP code is shown here. One of the benefits of using SOAP and web services in general is language/implementation independence.

In order to steer others in the right direction I'm providing the XML that works for me. Whatever language you're using to make your SOAP calls, the following XML format in your request should work.

For example, using Java and Apache Axis 2, this is the contents of my SOAPEnvelope object. Get a list of all orders with status 'processing':

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <salesOrderList xmlns="urn:Magento">
            <sessionId xmlns="">12345asdf</sessionId>
            <filters xmlns="">
                <filter>
                    <associativeEntity>
                        <key>status</key>
                        <value>processing</value>
                    </associativeEntity>
                </filter>
            </filters>
        </salesOrderList>
    </soapenv:Body>
</soapenv:Envelope>

Note, this is the SOAP v2 format for Magento.

etech
  • 2,548
  • 1
  • 27
  • 24
  • 1
    My thoughts exactly @etech, I'm using a Ruby Soap gem and all this PHP is another layer of abstraction I don't need. One question: Where does `associativeEntity` come from? – davetapley Sep 16 '14 at 21:37
  • 1
    @dukedave Everything within the body of the above XML is an element defined in the Magento WSDL. So, according to the WSDL, an `` element is the top level item within a `` element. – etech Oct 06 '14 at 04:37
  • +1 for showing the XML. I'm just using SOAP UI to experiment with the API, and so I need to write the XML myself. – psyklopz Jan 02 '17 at 18:26
1

I used this and it worked in SOAP API v2:

$params = array('complex_filter'=>
    array(
        array('key'=>'created_at','value'=>array('key' =>'from','value' => '2013-05-03 01:01:01')),
        array('key'=>'customer_id','value'=>array('key' =>'eq','value' => 3)),

    ),

);
Ren
  • 1,111
  • 5
  • 15
  • 24