Magento:如何通过API的HTTP GET请求获取销售订单的所有发票?
解决方案:通过销售订单ID获取所有发票信息
Magento 2 REST API确实没有提供直接通过订单ID获取发票的单一GET端点,但可以通过发票搜索接口实现需求,具体方法如下:
使用发票搜索接口,通过
order_id过滤结果:
发送GET请求到/V1/invoices,带上搜索条件参数,指定过滤字段为order_id,值为目标订单ID,匹配条件为eq(等于)。完整请求示例(curl):
curl -X GET "https://your-magento-domain/rest/V1/invoices?searchCriteria[filter_groups][0][filters][0][field]=order_id&searchCriteria[filter_groups][0][filters][0][value]=YOUR_ORDER_ID&searchCriteria[filter_groups][0][filters][0][condition_type]=eq" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN"定制返回字段(可选):
如果只需要特定字段(比如发票ID、总金额),可以添加searchCriteria[fields]参数缩小返回范围:curl -X GET "https://your-magento-domain/rest/V1/invoices?searchCriteria[filter_groups][0][filters][0][field]=order_id&searchCriteria[filter_groups][0][filters][0][value]=YOUR_ORDER_ID&searchCriteria[filter_groups][0][filters][0][condition_type]=eq&searchCriteria[fields]=items[invoice_id,grand_total]" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN"
关于订单接口不返回发票ID的说明
默认的/V1/orders/{order_id}接口响应中确实不包含发票关联数据,这是因为Magento 2默认的订单API响应字段未包含发票集合。如果需要在订单接口中直接返回发票信息,需要自定义扩展API,通过插件或自定义模块扩展订单数据的响应结构。
内容的提问来源于stack exchange,提问作者Luke
相关产品推荐
相关产品推荐

