Magento API and C# – Part I Extended – AND/OR Filters

Posted by on

Leave a comment

I already identified some of the shortcomings of the Magento SOAP API in my previous article including a lack of pagination. Since then I’ve realised another interesting feature that I think is missing (or at least I can’t seem to find) and that is the ability to do an OR query via the API. By default, Magento treats all filters as AND queries. For example if I want to search for myself amongst the customers I could use the following filter:

Which would create a SQL where clause like this:

But what if I wanted to find myself or a different customer? How do we construct our filter? To answer that question we are going to further enhance our module by creating a data helper that allows for a combination of AND and OR queries.

Create the helper

Helpers are stored in the Helper directory. Create that directory and the following file:

Notice our class extends the Mage_Api_Helper_Data class. For the full implementation refer to the code on Github.

Update the configuration

The same way we told the module about our models in the previous article, we need let the module know about the new helper. Add this entry to the config.xml in the ‘global’ section.

Putting it to use

We can now call the helper from our API code like this:

For a better example take a look on Github where I have updated our existing ‘count’ and ‘pagedList’ methods to use the new helper.

Wrap Up

Before we made our changes, filters where simply an array of complex filters that were interpreted as an AND query by Magento. Our helper has extended our methods to expect an array of arrays of complex filters. The inner arrays are the AND queries and the outer array is the OR query.

Now we can search like this:

Which creates a SQL where clause like this:

Ok, next we really are going to look at the C# portion of this series!

Tags: , , , ,

Related posts

No Comments Reply

Leave a Reply

© 2019 Meza Information Technology | Design by Vlad Carp

Back to top