skip to Main Content

I would like to filter out all orders with a specific country_id.

Here a sample from the json response:

{  
    "items": [{  
        "total_qty_ordered": 3,
        "updated_at": "2018-01-10 15:59:05",
        "weight": 0,
        "billing_address": {  
            "city": "Schaffhausen",
            "country_id": "CH"
        }
    }]
}

Here is what I’ve tried:

url = (
    "http://<host>/rest/V1/orders"
    "?searchCriteria[filter_groups][0][filters][0][field]=country_id"
    "&searchCriteria[filter_groups][0][filters][0][value]=CH" 
)

It’s not working because country_id is in a nested entity.

So I’ve tried to replace country_id by billing_address[country_id] but it’s not working either.

2

Answers


  1. This is the json structure for the search criteria..

    {
    "search_criteria": {
        "filter_groups": [
            {
                "filters": [
                    {
                        "field": "attribute_name",
                        "value": [string|int|float],
                        "condition_type": [string]; optional
                    }
                    more entries
                ]
            }
            more entries
        ],
        "current_page": [int] page number; optional
        "page_size": [int] number of items on a page; optional
        "sort_orders": [ optional
            {
                "field": "attribute_name",
                "direction": [int] -1 or 1
            }
            more entries
        ]
    }
    }
    
    Login or Signup to reply.
  2. The default REST API for search is only searching for the column which is available in sales_order table.

    You can not search with country_id, you will get an error like column not found. To make it work you have to customize the method getList

    magento/module-sales/Model/OrderRepository.php by creating custom module OR you can build your custom API to fetch order details based on country_id

    The proper URL

     http:/host/index.php/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=country_id&searchCriteria[filter_groups][0][filters][0][value]=US&searchCriteria[filter_groups][0][filters][0][conditionType]=eq
    

    Method: GET

    Header :

    Content-Type : application/json
    Authorization : Bearer <TOKEN>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search