I’m really confused here but I want to try create multiple terms via the woocommerce rest api, the example shows one term being added:
$data = [
'name' => 'Leather Shoes'
];
$woocommerce->post('products/tags', $data);
When adding a term this way, the rest api requires me to set the array key as name. I thought maybe I just add multiple items like this:
$data = [
'name' => 'Leather Shoes',
'name' => 'tagtest',
'name' => 'helloworld',
];
But because each item is using name as the key, it ends up only setting the last item:
$data = [
'name' => 'Leather Shoes',
'name' => 'Example',
'name' => 'TagTest',
];
var_dump($data);
array (size=1)
'name' => string 'TagTest' (length=7)
Then, tried setting different values other than name and got an error:
Error: Missing parameter(s): name
Question
So I’m trying to figure out how I pass multiple terms/tags to the $woocommerce->post('products/tags')
rest api endpoint.
I’m really confused here but I want to try create multiple terms via the woocommerce rest api, the example shows one term being added:
$data = [
'name' => 'Leather Shoes'
];
$woocommerce->post('products/tags', $data);
https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-product-tag
When adding a term this way, the rest api requires me to set the array key as name. I thought maybe I just add multiple items like this:
$data = [
'name' => 'Leather Shoes',
'name' => 'tagtest',
'name' => 'helloworld',
];
But because each item is using name as the key, it ends up only setting the last item:
$data = [
'name' => 'Leather Shoes',
'name' => 'Example',
'name' => 'TagTest',
];
var_dump($data);
array (size=1)
'name' => string 'TagTest' (length=7)
Then, tried setting different values other than name and got an error:
Error: Missing parameter(s): name
Question
So I’m trying to figure out how I pass multiple terms/tags to the $woocommerce->post('products/tags')
rest api endpoint.
1 Answer
Reset to default 1Putting aside whether you're making the API request from and to the same site or not, I assume that the $woocommerce
is an instance of Automattic\WooCommerce\Client
.
And you can use the batch
endpoint for product tags (/wp-json/wc/v3/products/tags/batch
) to create/update/delete multiple (by default, up to 100) product tags at a time. Here's an example for creating multiple product tags:
$data = [
'create' => [
[
'name' => 'Round toe',
],
[
'name' => 'Flat',
],
],
];
print_r( $woocommerce->post( 'products/tags/batch', $data ) );