Ionic WooCommerce REST API:创建变化产品顺序

7ajki6be  于 2022-12-09  发布在  Ionic
关注(0)|答案(2)|浏览(140)

我正在使用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方面成功收到的,但这是主要产品,而不是变化。
如果有人看到我在这里做错了什么,请帮忙。谢谢!

rqenqsqc

rqenqsqc1#

您确定您的请求格式正确吗?
我看了这里的官方文档,能够得到类似于你的请求工作的东西,假设我删除了“订单”键,它回来了这样,
请求:

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

pftdvrlh

pftdvrlh2#

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

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

相关问题