【问题标题】:Magento 2 - REST API - searchCriteria in nested json fileMagento 2 - REST API - 嵌套 json 文件中的 searchCriteria
【发布时间】:2018-07-12 08:48:30
【问题描述】:

我想过滤掉所有带有特定country_id 的订单。

这里是来自json 响应的示例:

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

这是我尝试过的:

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

它不起作用,因为country_id 在嵌套实体中。

所以我尝试用billing_address[country_id] 替换country_id,但它也不起作用。

【问题讨论】:

  • 您找到解决方案了吗?
  • @RafiqulHasan 不幸的是没有找到解决方案

标签: rest api magento magento2


【解决方案1】:

用于搜索的默认 REST API 仅搜索 sales_order 表中可用的列。

你不能用country_id 搜索,你会得到一个类似 column not found 的错误。要使其工作,您必须自定义方法getList

magento/module-sales/Model/OrderRepository.php 通过创建自定义模块,或者您可以构建自定义 API 以根据 country_id 获取订单详细信息

正确的网址

 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

方法:GET

标题:

Content-Type : application/json
Authorization : Bearer <TOKEN>

【讨论】:

    【解决方案2】:

    这是搜索条件的 json 结构..

    {
    "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
        ]
    }
    }
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关 如何 和/或 为什么 解决问题的附加上下文将改善答案的长期性价值。
    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 2018-05-19
    • 1970-01-01
    • 2017-04-29
    • 2020-09-24
    相关资源
    最近更新 更多