Magento2 Rest API Search Criteria Issue – Search Criteria Not Working Properly

magento2restsearch-criteria

I am trying to get the sales orders from magento2 REST API. I am using the below query to get the complete orders within entity_id range:

$ch = curl_init( Helper::getURL() . "orders?searchCriteria[filter_groups][0][filters][0][field]=status" .
         "&searchCriteria[filter_groups][0][filters][0][value]=" . $status .
         "&searchCriteria[filter_groups][0][filters][0][condition_type]=like" .
         "&searchCriteria[filter_groups][0][filters][0][field]=entity_id" .
         "&searchCriteria[filter_groups][0][filters][0][value]=" . $idMin .
         "&searchCriteria[filter_groups][0][filters][0][condition_type]=gteq" .
         "&searchCriteria[filter_groups][0][filters][0][field]=entity_id" .
         "&searchCriteria[filter_groups][0][filters][0][value]=" . $idMax .
         "&searchCriteria[filter_groups][0][filters][0][condition_type]=lteq" .
         "&searchCriteria[pageSize]=500&fields=items[entity_id,customer_email,customer_firstname,customer_lastname,customer_is_guest," .
         "increment_id,status,created_at,subtotal,total_paid,total_due,grand_total,shipping_amount,payment[method,amount_paid]," .
         "billing_address[city,email,firstname,lastname,postcode,street,telephone,company,region],items[item_id,name,original_price,price," .
         "product_id,qty_canceled,qty_invoiced,qty_ordered,qty_refunded,qty_returned,qty_shipped,row_total,sku,discount_amount]]" );

Status filter and idMin filter are not working. Only idMax filer is working. The search returns all sales orders having entity_id less than idMax with different status and entity_id starting from zero.

What is the problem with the search criteria?

Best Answer

Problem Solved. The issue is related to filter index. Each filter must have different index:

$ch = curl_init( Helper::getURL() . "orders?searchCriteria[filter_groups][0][filters][0][field]=status" .
     "&searchCriteria[filter_groups][0][filters][0][value]=" . $status .
     "&searchCriteria[filter_groups][0][filters][0][condition_type]=like" .
     "&searchCriteria[filter_groups][1][filters][0][field]=entity_id" .
     "&searchCriteria[filter_groups][1][filters][0][value]=" . $idMin .
     "&searchCriteria[filter_groups][1][filters][0][condition_type]=gteq" .
     "&searchCriteria[filter_groups][2][filters][0][field]=entity_id" .
     "&searchCriteria[filter_groups][2][filters][0][value]=" . $idMax .
     "&searchCriteria[filter_groups][2][filters][0][condition_type]=lteq" .
     "&searchCriteria[pageSize]=500&fields=items[entity_id,customer_email,customer_firstname,customer_lastname,customer_is_guest," .
     "increment_id,status,created_at,subtotal,total_paid,total_due,grand_total,shipping_amount,payment[method,amount_paid]," .
     "billing_address[city,email,firstname,lastname,postcode,street,telephone,company,region],items[item_id,name,original_price,price," .
     "product_id,qty_canceled,qty_invoiced,qty_ordered,qty_refunded,qty_returned,qty_shipped,row_total,sku,discount_amount]]" );

As mentioned in Magento Docs

Logical OR search

The following example searches for all products whose names contain the string Leggings or Parachute. The instances of %25 in the example are converted into the SQL wildcard character %.

GET <host>/rest/<store_code>/V1/products?
searchCriteria[filter_groups][0][filters][0][field]=name&
searchCriteria[filter_groups][0][filters][0][value]=%25Leggings%25&
searchCriteria[filter_groups][0][filters][0][condition_type]=like&
searchCriteria[filter_groups][0][filters][1][field]=name&
searchCriteria[filter_groups][0][filters][1][value]=%25Parachute%25&
searchCriteria[filter_groups][0][filters][1][condition_type]=like

Logical AND search

This sample searches for women’s shorts that are size 31 and costs less than $30. In the CE sample data, women’s shorts have a sku value that begins with WSH. The sku also contains the size and color, such as WSH02-31-Yellow.

GET <host>/rest/<store_code>/V1/products?
searchCriteria[filter_groups][0][filters][0][field]=sku&
searchCriteria[filter_groups][0][filters][0][value]=WSH%2531%25&
searchCriteria[filter_groups][0][filters][0][condition_type]=like&
searchCriteria[filter_groups][1][filters][0][field]=price&
searchCriteria[filter_groups][1][filters][0][value]=30&
searchCriteria[filter_groups][1][filters][0][condition_type]=lt
Related Topic