Adding Pagination to Magento API Calls
When working with Magento’s SOAP API to retrieve product data, you might encounter situations where the result set is too large to handle in one go. In such cases, applying pagination filters becomes essential. This guide will walk you through the process of limiting the number of records returned by a single API call.
Understanding Magento API Pagination
Magento uses filter parameters to apply limitations on the returned data sets. The primary filters that control pagination are limit and offset.
-
Limit: This parameter defines the maximum number of items to be included in the result set.
-
Offset: Specifies the starting point for the data retrieval. It allows you to skip a certain number of records before beginning the actual fetch.
Adding Pagination Filters to Your API Call
Let’s enhance your existing code by adding pagination filters to limit the product list to 25 items per page:
// create soap object
$proxy = new SoapClient('<a href="http://localhsot/magento/api/soap/?wsdl" rel="nofollow noreferrer">http://localhsot/magento/api/soap/?wsdl</a>');
// create authorized session id using api user name and api key
// $sessionId = $proxy->login('apiUser', 'apiKey');
$sessionId = $proxy->login('test_admin', '12345678');
$filters = array(
'limit' => 25, // Number of items per page
'offset' => 0 // Starting point (0 for first page)
);
// Get list of product
$productlist = $proxy->call($sessionId, 'product.list', array($filters));
print_r($productlist);
In the updated code:
-
We define an associative array
$filtersto hold our pagination parameters. -
The
limitkey is set to 25, which means each API call will return a maximum of 25 product records. -
The
offsetkey is initially set to 0, indicating that the first page should be retrieved. -
These filters are passed as an array argument to the
product.listmethod in the$proxy->call()function.
Navigating Between Pages
To retrieve multiple pages of data, you can increment the offset value by the limit. For example:
$currentOffset = 0;
$itemsPerPage = 25;
while ($currentOffset < 100) {
$filters['offset'] = $currentOffset; // Set new offset for each page
$productlist = $proxy->call($sessionId, 'product.list', array($filters));
print_r($productlist);
$currentOffset += $itemsPerPage;
}
In this loop example:
-
We start with an
offsetof 0 and alimitof 25 items per page. -
After each iteration, we increment the offset by the number of items per page (25).
-
The loop continues until all pages are retrieved (
currentOffset < totalNumberOfItems).
Conclusion
Adding pagination to Magento API calls allows you to manage large data sets more efficiently. By setting appropriate limit and offset values, you can retrieve product information in a controlled and predictable manner.
Remember that the pagination filters are applied within the $filters array passed to your desired method (product.list, in this case). You can adjust these filter parameters to fit the specific requirements of your application.
For more advanced use cases and additional API functionalities, I recommend exploring Laravel Company’s guide on Magento API integrations. This resource provides a comprehensive overview of different Magento API types and their usage scenarios.