1

stuck trying to update line item meta fields I've attached to each line item. From a call to a particular order you an see the line item meta attached below:

"line_items":[  
      {  
         "id":243,
         "name":"Example Product",
         "product_id":194,
         "variation_id":0,
         "quantity":2,
         "tax_class":"",
         "subtotal":"84.00",
         "subtotal_tax":"0.00",
         "total":"84.00",
         "total_tax":"0.00",
         "taxes":[  ],
         "meta_data":[  
            {  
               "id":2197,
               "key":"_tracking_number",
               "value":""
            },
            {  
               "id":2198,
               "key":"_estimate_delivery_date",
               "value":""
            },
            {  
               "id":2199,
               "key":"_carrier",
               "value":""
            },
            {  
               "id":2200,
               "key":"_service",
               "value":""
            },
         ]

The idea is our distributor will pull each order down programmatically and then update the meta which triggers a shipping confirmation email.

My issue is when I try to update the meta myself (testing) with curl as below:

--data '{"status":"completed","line_items":{"0":{"meta_data":{"_tracking_number":"being tracked"}}}}'

I get an error saying:

"Invalid parameter(s): line_items","data":{"status":400,"params":{"line_items":"line_items[0][meta_data] is not of type array."}}

This is confusion as line_items certainly is an array! I've also tried using the specific line item ID, but based on the return structure line item id and the meta data are both top level keys in a line item object.

I'm just accessing line_items[0] to test as I only have one line item and the line_item value is an array as you can see from the above.

line_items[0][meta_data] also looks to be an array AFAIK. Any guidance on what I'm doing incorrectly?

I also had a thought that ok, maybe the line items are accessed in the route itself a la:

/wp-json/wc/v2/orders/2594/line_items

but many iterations of this (also as a param) also did not work.

EDIT: so I had to add the product_id associated with the line item for some reason... The following cURL works(HTTP 200) worked but didn't actually update the field. I'm using v2 of the API.

--data '{"status":"completed","line_items":{"0":{"product_id":"194","meta_data":{"3":{"_carrier":"carrier"}}}}}'

Any idea why this isn't updating?

1

For some reason, you have to double the arrays, maybe this doesn't affect you as you are referencing the firt element. I use this in PHP:

function saveMetaItem($id_order,$id_item,$id_product,$meta_key,$meta_value){
    $woo = new wooclient();
    $data = array(
        'line_items' => array(array(
                       'id' => $id_item,
                       'product_id' => $id_product,
                       'meta_data' => array(array(
                            'key' => $meta_key,
                            'value' => $meta_value
                               ))
                       ))
    );
    $result = $woo->put('orders/'.$id_order, $data);
    return $result;
}

Notice the double array() calls in both line_items and meta_data. Also provide both id and product_id. Convert that to JSON and your done ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.