Skip to content
Advertisement

How do I insert meta data to my customers in woocommerce using an object array?

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' => 'johndoe@example.com',
            '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' => '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'
            ],
            '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":"johndoe@example.com","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":"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","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

Advertisement

Answer

'meta_data' => 
[
    [
     'key' => "proceed_id",
     'value' => "4"
    ]
]

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 :

       $data = [
            'email' => 'johndoe6@example.com',
            'first_name' => 'John',
            'last_name' => 'Doe',
            'username' => 'johndoe6',
            'proceed_id' => '2',
            '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'
            ],
            'meta_data' => [
            ['key' => "proceed_id",
            'value' => "4"]
            ]

        ];
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement