最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WooCommerce - Create multiple product tags via rest api endpoint

programmeradmin1浏览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);

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.

Share Improve this question asked Mar 1, 2020 at 23:22 wharfdalewharfdale 3484 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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 ) );
发布评论

评论列表(0)

  1. 暂无评论