'Update Order custom Field value with WooCommerce REST API

I am trying to update the Order custom field value lworder. I used the below code but I did not find any update. Can anyone please help me with this?

$api_response = wp_remote_post( 'https://your-website/wp-json/wc/v3/orders/{ORDER ID}', array(
    //'method'    => 'PUT',
    'headers' => array(
        'Authorization' => 'Basic ' . base64_encode( 'KEY:SECRET' )
    ),
    'meta_key' => array(
            'lworder' => 'ordered', 
    )
) );

$body = json_decode( $api_response['body'] );
//print_r( $body );

if( wp_remote_retrieve_response_message( $api_response ) === 'OK' ) {
    echo 'The Order field  ' . $body->name . ' has been updated';
}


Solution 1:[1]

Tested OK with WooCommerce 6.1.1

$api_response = wp_remote_post('https://mujuonly.github.io/index.php/wp-json/wc/v3/orders/1631817799', array(
        'method' => 'PUT',
        'headers' => array(
            'Authorization' => 'Basic ' . base64_encode('ck_b0f7480d348807e3c065053e7810fb83ce3b6b41:cs_0c44e9ad9173fd3698010e86f1140914dcb8fc8a')
        ),
        'body' => array('billing' => array(
                'city' => 'Disssssmmmmss',
            ),
            'meta_data' => array(0 => array(
                    'key' => 'lworder',
                    'value' => 'orderd'
                )
            )
        )
    ));

    $body = json_decode($api_response['body']);

    if (wp_remote_retrieve_response_message($api_response) === 'OK') {
        echo 'The Order field  ' . $body->name . ' has been updated';
    }

Solution 2:[2]

curl --location --request PUT 'https://yourwebsite.com/wp-json/wc/v3/orders/yourorderID' \
--header 'Authorization: Basic YOURTOKEN' \
--header 'Content-Type: application/json' \
--data-raw '{
"meta_data": [
    {
      "key": "testvalue",
      "value": 12
    }
  ]}'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mujuonly
Solution 2 Hamza Üzümcü