Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to create product in WC REST API v2 #23

Open
chamster opened this issue Jun 27, 2017 · 4 comments
Open

Trying to create product in WC REST API v2 #23

chamster opened this issue Jun 27, 2017 · 4 comments

Comments

@chamster
Copy link

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

?>
@judgej
Copy link
Owner

judgej commented Aug 2, 2017

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

@treehighroots
Copy link

treehighroots commented Nov 21, 2017

@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

@willfaulds
Copy link

@treehighroots did you have any luck resolving this?

@treehighroots
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants