How to get list of products
Magento SOAP API contains catalogProductList method for downloading list of products. Basically it returns ALL of products at once. In production environment it could mean thousands of products returned in single API call. Mobile devices are not capable of handling such big response. There is another inconvenience – lack of pagination. Simply, there are no page and limit parameters in Magento SOAP API. But there are two solutions: first, ask your Magento developer to add needed parameters to SOAP API or use REST API (OAuth authentication is required). However, in this article I will show how to download all products and set up filter to skip products that customer should not see.
Download list of products
There are response models defined in documentation of catalogProductList:
A few words of explanation: I developed code generator which creates Java classes from WSDL (Web Services Description Language) file of Magento SOAP API. WSDL is an XML file, defines messages, types and operations. There are code generation tools for Web Services but result code is not fully compatible with Android (many differences between the Java API and the Android API, and Android does not use a Java Virtual Machine but Dalvik or Android Runtime (ART)). It’s a good idea to write an automated code generator, instead of manually creating each class. By the way it’s a pleasure to generate 676 Java files just like that ;)
CatalogProductEntity is meager product model which contains only name and sku, which are useful for UI. It should also be expanded by Magento developer to meet the demands of mobile applications (add photos, price, currency, short description, etc.).
Request class with filters field:
SessionId you can get from Login opeartion; for details see Part II of this series. There is also example of API call to achieve it, so I omit that snippet of code in this post. But remember to run test calls on staging machine with at most tens of products because it can overload the server. To reduce number of products in response or find products by name we should use Filters.
Filters
There are two types of filters: simple and complex. Simple filter contains list of key, value objects and selects products that exactly matches these parameters. Complex filter lets use operators like:
- gt – greater than
- lt – less than
- in – matches one of values divided by commas
- and more
Products in Magento have 4 states of visibility:
If we are interested in products visible for users then we should search for visibility value greater than 1. I will not go into the details of implementation of filters. But I would like to say that it’s good idea to use Builder Pattern. It allows you to define filters in simple way and makes them easy to read: