wc-api-custom-meta
wc-api-custom-meta copied to clipboard
Trying to create product in WC REST API v2
The following code creates a new product, but doesn't create the meta. Would this be expected to work? If so, can you see anything in the code that doesn't look right? Thanks.
WC REST API v2 WC v 3.0.9
<?php
// Setup:
require('../../../../vendor/autoload.php');
use Automattic\WooCommerce\Client;
$woocommerce = new Client(
'https://mysite.com', // Your store URL
'ck_xxxxxxxxxxxx', // Your consumer key
'cs_xxxxxxxxxxxxx, // Your consumer secret
[
'wp_api' => true, // Enable the WP REST API integration
'version' => 'wc/v2' // WooCommerce WP REST API version
]
);
$data = [
'name' => 'My New product Name',
'custom_meta' => [
'gb_item_id' => '24680'
]
];
print_r($woocommerce->post('products', $data));
?>
The v2 REST API supports metadata out of the box. Please see my notes on the README to see how to use it.
@judgej could you please help me out? i'm using the rest 'version' => 'wc/v2' and you wrote that it supports custom meta out of the box. (i'm using this library: https://packagist.org/packages/automattic/woocommerce)
but when i'm adding custom meta via the provided example code, i'm getting this error:
Parameter: meta_data [rest_invalid_param]
the array i'm trying to post looks like:
stdClass Object ( [meta_data] => Array ( [first_key] => firstValue [second_key] => secondValue, [third_key] => thirdValue ) [name] => My Product
when i delete the meta_data entry, everything works fine. product details, like name, description aso. are getting updated, so the code works correctly.
thanks for any help and best regards
@treehighroots did you have any luck resolving this?
hi @willfaulds. yes, i managed to get it right. i had to setup my json array of data like this:
"product_data": { "meta_data": [{ "key": "first_key", "first_value": "123" }, { "key": "second_key" ...
best regards