Android WooCommerce REST API:创建变体产品订单

Android WooCommerce REST API:创建变体产品订单,android,post,ionic-framework,hybrid-mobile-app,woocommerce-rest-api,Android,Post,Ionic Framework,Hybrid Mobile App,Woocommerce Rest Api,我正在使用RESTAPI V3,并通过跟踪和发布变体创建订单。尽管我在order对象的行项目中正确发送了变体id。这是我正在发送的请求对象- URL: http://mywebsite.com/wc-api/v3/orders?............. Request Method: POST Status Code: 201 Created 订单对象- {"order": { "payment_method": "cod", "payment_method_title": "Cash O

我正在使用RESTAPI V3,并通过跟踪和发布变体创建订单。尽管我在order对象的行项目中正确发送了变体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之后返回的响应在第行\u items对象中没有变体\u 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端已成功收到订单,但这是针对主要产品而非变体的订单

如果有人看到我做错了什么,请帮助我。
谢谢

您确定您的请求格式设置正确吗

我看了官方文件,得到了类似于你工作要求的东西,假设我取下了“订单”键,它就这样回来了

请求:

{
  "payment_method": "bacs",
  "payment_method_title": "Direct Bank Transfer",
  "set_paid": true,
  "billing": {
    "first_name": "John",
    "last_name": "Doe",
    "address_1": "969 Market",
    "address_2": "",
    "city": "San Francisco",
    "state": "CA",
    "postcode": "94103",
    "country": "US",
    "email": "john.doe@example.com",
    "phone": "(555) 555-5555"
  },
  "shipping": {
    "first_name": "John",
    "last_name": "Doe",
    "address_1": "969 Market",
    "address_2": "",
    "city": "San Francisco",
    "state": "CA",
    "postcode": "94103",
    "country": "US"
  },
  "line_items": [
    {
      "product_id": 93,
      "quantity": 2
    },
    {
      "product_id": 843,
      "variation_id": 1540,
      "quantity": 1
    }
  ],
  "shipping_lines": [
    {
      "method_id": "flat_rate",
      "method_title": "Flat Rate",
      "total": "10"
    }
  ]
}
答复:

{
    "id": 1555,
    "parent_id": 0,
    "number": "1555",
    "order_key": "wc_order_5c33a7a124cc0",
    "created_via": "rest-api",
    "version": "3.5.1",
    "status": "processing",
    "currency": "USD",
    "date_created": "2019-01-07T14:25:22",
    "date_created_gmt": "2019-01-07T19:25:22",
    "date_modified": "2019-01-07T14:25:22",
    "date_modified_gmt": "2019-01-07T19:25:22",
    "discount_total": "0.00",
    "discount_tax": "0.00",
    "shipping_total": "10.00",
    "shipping_tax": "0.00",
    "cart_tax": "0.00",
    "total": "24.97",
    "total_tax": "0.00",
    "prices_include_tax": false,
    "customer_id": 0,
    "customer_ip_address": "",
    "customer_user_agent": "",
    "customer_note": "",
    "billing": {
        "first_name": "John",
        "last_name": "Doe",
        "company": "",
        "address_1": "969 Market",
        "address_2": "",
        "city": "San Francisco",
        "state": "CA",
        "postcode": "94103",
        "country": "US",
        "email": "john.doe@example.com",
        "phone": "(555) 555-5555"
    },
    "shipping": {
        "first_name": "John",
        "last_name": "Doe",
        "company": "",
        "address_1": "969 Market",
        "address_2": "",
        "city": "San Francisco",
        "state": "CA",
        "postcode": "94103",
        "country": "US"
    },
    "payment_method": "bacs",
    "payment_method_title": "Direct Bank Transfer",
    "transaction_id": "",
    "date_paid": "2019-01-07T14:25:22",
    "date_paid_gmt": "2019-01-07T19:25:22",
    "date_completed": null,
    "date_completed_gmt": null,
    "cart_hash": "",
    "meta_data": [],
    "line_items": [
        {
            "id": 588,
            "name": "Chocolate Chip Cookie",
            "product_id": 93,
            "variation_id": 0,
            "quantity": 2,
            "tax_class": "",
            "subtotal": "5.98",
            "subtotal_tax": "0.00",
            "total": "5.98",
            "total_tax": "0.00",
            "taxes": [],
            "meta_data": [],
            "sku": "",
            "price": 2.99
        },
        {
            "id": 589,
            "name": "Salad (Copy) - Pint (Serves 2-3)",
            "product_id": 1537,
            "variation_id": 1540,
            "quantity": 1,
            "tax_class": "",
            "subtotal": "8.99",
            "subtotal_tax": "0.00",
            "total": "8.99",
            "total_tax": "0.00",
            "taxes": [],
            "meta_data": [
                {
                    "id": 5622,
                    "key": "size",
                    "value": "Pint (Serves 2-3)"
                }
            ],
            "sku": "",
            "price": 8.99
        }
    ],
    "tax_lines": [],
    "shipping_lines": [
        {
            "id": 590,
            "method_title": "Flat Rate",
            "method_id": "flat_rate",
            "instance_id": "",
            "total": "10.00",
            "total_tax": "0.00",
            "taxes": [],
            "meta_data": []
        }
    ],
    "fee_lines": [],
    "coupon_lines": [],
    "refunds": [],
    "_links": {
        "self": [
            {
                "href": "https://acme.local/wp-json/wc/v3/orders/1555"
            }
        ],
        "collection": [
            {
                "href": "https://acme.local/wp-json/wc/v3/orders"
            }
        ]
    }
}
如果上述方法不适用于您(即,重新格式化订单数据以匹配上述内容),您可以通过设置XDebug并在此处设置断点,始终在woocommerce端调试问题:


wp content/plugins/woocommerce/includes/api/class wc rest orders controller.php
,特别是“prepare_object_for_database”方法,将是我首先查看的地方,查看$request变量中的数据是否符合您的预期,如果不检查错误日志,请确保您的内容类型设置为json,您的json在请求体中,而不是POST数据中

我用这个。这对我很有用:

{"payment_method": "cod",   "payment_method_title": "Cash on delivery",   "set_paid": false,   "billing": {     "first_name": "rocky",     "last_name": "",     "address_1": "bangladesh ",     "address_2": "",     "city": "dhaka",     "state": "Chittagong ",     "postcode": "4590",     "country": "",     "email": "mdroky360@gmail.com",     "phone": "01683946462"   },   "shipping": {     "first_name": "md roky",     "last_name": "",     "address_1": "bangladesh ",     "address_2": "",     "city": "rangamati ",     "state": "Chittagong ",     "postcode": "4590",     "country": "US"   },   "line_items": [     {       "product_id": 33, "variation_id":324,      "quantity": 2     }],   "shipping_lines": [     {       "method_id": "flat_rate",       "method_title": "Flat Rate",       "total": "80"     }   ] }

任何人请回答我的问题。我会再次检查这些变化是启用和购买。文件称应返回一个变体id: