我正在使用REST API V3,并通过跟踪和发布变异创建订单。虽然我在订单对象的行项目中正确发送variation_id。下面是我发送的请求对象-
URL: http://mywebsite.com/wc-api/v3/orders?.............
Request Method: POST
Status Code: 201 Created
订单对象-
{"order": {
"payment_method": "cod",
"payment_method_title": "Cash On Delivery",
"set_paid": false,
"billing_address": {
"first_name": "I ey",
"last_name": "Xhh",
"address_1": "Gdb",
"address_2": "Dvhs",
"city": "Gsg",
"state": "New Delhi",
"postcode": "54619",
"country": "India",
"email": "hdj@bsb.bsn",
"phone": "54646"
},
"shipping_address": {
"first_name": "I ey",
"last_name": "Xhh",
"address_1": "Gdb",
"address_2": "Dvhs",
"city": "Gsg",
"state": "New Delhi",
"postcode": "54619",
"country": "India",
"email": "hdj@bsb.bsn",
"phone": "54646"
},
"line_items": [
{
"product_id": 420,
**"variation_id": 429,**
"quantity": 1
},
{
"product_id": 420,
**"variation_id": 425,**
"quantity": 1
}
]}}
很遗憾,在201之后返回的RESPONSE在line_items对象中没有variation_id-
"line_items": [
{
"id": 101,
"subtotal": "5100.00",
"subtotal_tax": "0.00",
"total": "5100.00",
"total_tax": "0.00",
"price": "5100.00",
"quantity": 1,
"tax_class": "",
"name": "Alpha Cement",
"product_id": 95,
"sku": "",
"meta":[]
},
{
"id": 102,
"subtotal": "20000000.00",
"subtotal_tax": "0.00",
"total": "20000000.00",
"total_tax": "0.00",
"price": "20000000.00",
"quantity": 1,
"tax_class": "",
"name": "Apple MacBook Core i5 7th Gen",
"product_id": 94,
"sku": "",
"meta": [
]
},
{
"id": 103,
"subtotal": "82000.00",
"subtotal_tax": "0.00",
"total": "82000.00",
"total_tax": "0.00",
"price": "82000.00",
"quantity": 1,
"tax_class": "",
"name": "CR SHEET",
"product_id": 420,//correct product id
"sku": "",
"meta": [
]
},
{
"id": 104,
"subtotal": "82000.00",
"subtotal_tax": "0.00",
"total": "82000.00",
"total_tax": "0.00",
"price": "82000.00",
"quantity": 1,
"tax_class": "",
"name": "CR SHEET",
"product_id": 420, //correct product id
"sku": "",
"meta": [
]
}
],
注意:订单是在WooCommerce方面成功收到的,但这是主要产品,而不是变化。
如果有人看到我在这里做错了什么,请帮忙。谢谢!
2条答案
按热度按时间rqenqsqc1#
您确定您的请求格式正确吗?
我看了这里的官方文档,能够得到类似于你的请求工作的东西,假设我删除了“订单”键,它回来了这样,
请求:
回应:
如果上面的方法不适合你(也就是说,重新格式化你的订单数据以匹配上面的方法),你可以通过设置XDebug并在这里设置一个断点来调试woocommerce方面的问题:
wp-content/plugins/woocommerce/includes/api/class-wc-rest-orders-controller.php
,特别是“prepare_object_for_database”方法,将是我首先查看的地方,看看进入$request变量的数据是否是您所期望的,如果不是,请检查错误日志,确保您的内容类型设置为json,并且您的json位于请求主体中,而不是POST数据中。pftdvrlh2#
我用这个。这个对我有用: