Hi I am trying to use the woocommerce API and do post requests of customer data and adding an internal customer ID.
Here is my data
I have tried to pass this data through a post request
$extension = "/wp-json/wc/v3/customers?";
$method = "POST";
$data = [
'email' => '[email protected]',
'first_name' => 'John',
'last_name' => 'Doe',
'username' => 'johndoe',
'billing' => [
'first_name' => 'John',
'last_name' => 'Doe',
'company' => '',
'address_1' => '969 Market',
'address_2' => '',
'city' => 'San Francisco',
'state' => 'CA',
'postcode' => '94103',
'country' => 'US',
'email' => '[email protected]',
'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'
],
'meta_data' => [
'proceed_id' => '2'
]
];
$response = woo_api_request($extension, $method, $data);
Below is the response when I commented out the meta data. But as you can see there is a meta data array.
{"id":7,"date_created":"2021-01-04T21:50:58","date_created_gmt":"2021-01-04T21:50:58","date_modified":"2021-01-04T21:51:02","date_modified_gmt":"2021-01-04T21:51:02","email":"[email protected]","first_name":"John","last_name":"Doe","role":"customer","username":"johndoe","billing":{"first_name":"John","last_name":"Doe","company":"","address_1":"969 Market","address_2":"","city":"San Francisco","postcode":"94103","country":"US","state":"CA","email":"[email protected]","phone":"(555) 555-5555"},"shipping":{"first_name":"John","last_name":"Doe","company":"","address_1":"969 Market","address_2":"","city":"San Francisco","postcode":"94103","country":"US","state":"CA"},"is_paying_customer":false,"avatar_url":"https://secure.gravatar.com/avatar/fd876f8cd6a58277fc664d47ea10ad19?s=96&d=mm&r=g","meta_data":[],"_links":{"self":[{"href":"https://localhost.com/wp-json/wc/v3/customers/7"}],"collection":[{"href":"https://localhost.com/wp-json/wc/v3/customers"}]}}
But when I try to pass the proceed_id I get an error.This is the error
{"code":"rest_invalid_param","message":"Invalid parameter(s): meta_data","data":{"status":400,"params":{"meta_data":"meta_data is not of type array."}}}
Do I need to register the meta data? Or am I missing something else?
UPDATE 1:
I’ve tried to change the way I structure my meta_data to this:
'meta_data' => [
['key' => "proceed_id"],
['value' => "4"]
]
It seems to be not throwing an error with array type now, but I am getting a notice :
Notice: Undefined index: key in
wp-content/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-customers-v2-controller.php
on line 106
2
Answers
This method worked. Since the documentation categorizes meta_data as an array instead of an object. You need to pass it as such. Found the answer here: https://github.com/claudiosanches/woocommerce-extra-checkout-fields-for-brazil/issues/56
Full parms :
According to the WooCommerce REST API documentation you should provide meta_data as an array. Most likely single PHP array gets converted to JSON object instead of an array.
In cases like this, it can help if you try making a request manually, using Postman or some similar tool.
If I understand documentation correctly, this should work: