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

时间:2018-12-26 19:39:54

标签: android post ionic-framework hybrid-mobile-app woocommerce-rest-api

我正在使用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 个答案:

答案 0 :(得分:1)

确定要正确格式化请求吗?

我查看了官方文档here,并获得了与您的工作要求类似的信息,假设我删除了“ orders”键,并且它又回来了,

请求:

{
  "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数据中。

答案 1 :(得分:0)

我用这个。这对我有用:

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