wc-api-custom-meta icon indicating copy to clipboard operation
wc-api-custom-meta copied to clipboard

Trying to create product in WC REST API v2

Open chamster opened this issue 7 years ago • 4 comments

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));

?>

chamster avatar Jun 27 '17 00:06 chamster

The v2 REST API supports metadata out of the box. Please see my notes on the README to see how to use it.

judgej avatar Aug 02 '17 15:08 judgej

@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 avatar Nov 21 '17 10:11 treehighroots

@treehighroots did you have any luck resolving this?

willfaulds avatar Jan 30 '18 16:01 willfaulds

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

treehighroots avatar Jan 31 '18 08:01 treehighroots