Solving Magento 2 REST API Search Criteria for Order Date Range
I’ve encountered this issue while working on an e-commerce project using the Magento 2 REST API to retrieve orders based on a specific date range. The search criteria provided in your question, though detailed, is not correctly formatted to filter orders by created_at between two dates.
Understanding the Magento 2 REST Search Criteria Format
The Magento 2 REST API uses a standard format for search criteria, which includes filter_groups, filters, and condition_type. However, the way you’ve combined these elements in your URL is causing the date filter to fail.
Here’s how the correct structure should look:
/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=created_at&searchCriteria[filter_groups][0][filters][0][value]=2022-10-13 00:00:00&searchCriteria[filter_groups][0][filters][0][condition_type]=gteq
Fixing the Date Filter for Created At
Let’s break down the issues in your original URL and correct them:
Incorrect Date Format
The date format 2016-10-26T04:00:00.0000000Z is not accepted by Magento 2 as a valid timestamp for the created_at field. You should use the standard YYYY-MM-DD HH:MM:SS format.
Incorrect Condition Type
The condition types you’re using (from and to) are not recognized by the Magento API for date filtering. The correct conditions are:
gteq: Greater than or equal tolteq: Less than or equal togt: Greater thanlt: Less than
Missing Timezone Information
The date you’re passing should include timezone information, which Magento 2 expects. Using Z at the end of your timestamp indicates UTC timezone.
The Correct Approach for Filtering by Created At
To retrieve orders created between October 12th and October 13th, use the following URL:
/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=created_at&searchCriteria[filter_groups][0][filters][0][value]=2022-10-12 23:59:59&searchCriteria[filter_groups][0][filters][0][condition_type]=lteq
This will fetch all orders created on or before October 12th, effectively capturing orders from that day.
Remember to replace 2022-10-12 with the start date of your desired range and adjust the condition accordingly for different ranges. For example, to get orders from October 13th to a later date, use gteq.
Alternative: Using Date Range Filters
An alternative approach is to use separate filters for the start and end dates:
/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=created_at&searchCriteria[filter_groups][0][filters][0][value]=2022-10-12 00:00:00&searchCriteria[filter_groups][0][filters][0][condition_type]=gteq
&searchCriteria[filter_groups][0][filters][1][field]=created_at&searchCriteria[filter_groups][0][filters][1][value]=2022-10-13 23:59:59&searchCriteria[filter_groups][0][filters][1][condition_type]=lteq
This method combines two filtersâone for the start date with gteq and one for the end date with lteq.
Conclusion
When using the Magento 2 REST API to filter orders by created_at, ensure you follow the correct format for search criteria. Use the standard timestamp format with timezone information, apply the appropriate condition type (like gteq or lteq), and maintain consistent formatting across all parameters.
Remember that the API is case-sensitive and requires precise matching of field names and conditions. Always double-check your URL structure before making requests to avoid unnecessary errors.
For more detailed examples and best practices on Magento 2 REST API usage, check out our guide at Laravel Company.