wordpresswoocommerceputwoocommerce-rest-apipartial-ordering

WooCommerce REST API: updating order line item metadata for shipment


I've stubled upon an issue for updating order line items' metadata via WooCommerce REST API using node.js.

I've been following these steps for updating orders (and was succesful with some fields): https://woocommerce.github.io/woocommerce-rest-api-docs/#update-an-order

Now, what I would like to achieve is changing the number of shipped line items of an order. Something I would normally use the partial orders WC plugin in the wordpress UI.

Below, you can find a screenshot of a line item I get from WC using the orders API call. The last element of the meta_data array has key 'shipped' and it contains an array with one object, stating that one (out of two ordered items) had been shipped:

"line_items": [{
        "id": 1937,
        "name": "Maya",
        "product_id": 1271,
        "variation_id": 1272,
        "quantity": 2,
        "tax_class": "",
        "subtotal": "140.00",
        "subtotal_tax": "0.00",
        "total": "140.00",
        "total_tax": "0.00",
        "taxes": [],
        "meta_data": [{
                "id": 21637,
                "key": "pa_product-color",
                "value": "beige"
            }, {
                "id": 21638,
                "key": "pa_shoe-size",
                "value": "42"
            }, {
                "id": 21639,
                "key": "pa_shoe-width",
                "value": "wide"
            }, {
                "id": 21640,
                "key": "shipped",
                "value": [{
                        "qty": 1,
                        "date": "Nov 21, 2017"
                    }
                ]
            }
        ],
        "sku": "2522BE42W",
        "price": 70
    },

As you can see, the value of the key 'shipped' is an object. When I ty to send it (back) to WC, I get an error saying: "data":{"status":400,"params":{"line_items":"line_items[0][meta_data][3][value] is not of type string."}}}

When I try to send the value as a string, i.e. lineItems[0].meta_data = [{key:"shipped", value: "[{qty:'2'}]" }]

I get no errors, but WC treats this as string, not as an object and doesn't update the shipment qty in the DB the way I intended (it only pulls the shipped quantity down to 0 instead):

{
                "id": 21640,
                "key": "shipped",
                "value": "[{qty:'2'}]"
}

Any insights or ideas - how could I modify the shipped quantity of line items via the WC API?


Solution

  • So, apparently there was a bug in WP 4.9 version, which was fixed recently in the following commit: https://github.com/woocommerce/woocommerce/pull/17849

    It concerns REST API schema and after merging the fix to WooCommerce, the problems disappear and now I am able to send the data as an object.

    More on the topic can be found here:
    https://github.com/woocommerce/wc-api-dev/pull/74