Reputation: 610
I am adding products via Woocommerce Rest Api v2. But I am unable to add custom fields via the Api.
please suggest me the best way to do that.
The json response:
$data = array(
// 'product' => array(
'id' => 8555,
'title' => 'Notebook',
'type' => 'simple',
'product_id' => 8555,
'regular_price' => '21.99',
'description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.',
'short_description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
'categories' => array("General"),
'product_meta' => array(
'custom_products_key' => 'custom_product_value',
)
);
product_meta
is not working…
Upvotes: 1
Views: 3144
Reputation: 253784
You should normally replace 'product_meta'
by 'meta_data'
and separate the key from the value.
For example this will add 2 custom fields to your product:
'meta_data' => [
[
'key' => 'custom_key'
'value' => 'custom_value'
]
[
'key' => 'custom_key2'
'value' => 'custom_value2'
]
]
To create a product using latest Woocommerce Rest API v2 (with PHP), setting the good category ID in the following, will be:
$data = [
'name' => 'Notebook',
'type' => 'simple',
'regular_price' => '21.99',
'description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.',
'short_description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
'categories' => [
[
'id' => 9
]
],
'meta_data' => [
[
'key' => 'custom_key'
'value' => 'custom_value'
]
[
'key' => 'custom_key2'
'value' => 'custom_value2'
]
]
];
print_r($woocommerce->post('products', $data));
This should work.
Upvotes: 7