0

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 1

1

Putting 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 ) );
4
  • 1
    Working perfectly, thanks very much Sally!
    – wharfdale
    Mar 2, 2020 at 9:50
  • Next step for me is to try create both new tag(s) and products via the api and assign the new tag to the new product at the same time.
    – wharfdale
    Mar 2, 2020 at 9:54
  • Okay, good luck with that! :) ( and just a gentle reminder, if you got stuck, try asking on Stack Overflow first since WC questions here are off-topic.. :) )
    – Sally CJ
    Mar 2, 2020 at 10:29
  • 1
    Oh! Honestly thought WC questions fit better here than on Stack Overflow personally but thanks for the heads up.
    – wharfdale
    Mar 2, 2020 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.