Magento 2 - REST API - searchCriteria in nested json file
Asked Answered
M

2

6

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.

Misdate answered 1/2, 2018 at 22:21 Comment(2)
Did you get the solution?Yeseniayeshiva
@RafiqulHasan no unfortunately no solution found for thisMisdate
P
3

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>
Phatic answered 25/4, 2019 at 11:56 Comment(0)
B
-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
    ]
}
}
Baptiste answered 1/4, 2018 at 11:37 Comment(1)
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.Cynthiacynthie

© 2022 - 2024 — McMap. All rights reserved.