简体   繁体   中英

Adding tags to WooCommerce products through Rest Api

I am adding products to WooCommerce through the code below https://woocommerce.github.io/woocommerce-rest-api-docs/?php#create-a-product

<?php
$data = [
    'name' => 'my title',
    'type' => 'simple',
    'regular_price' => '19',
    'description' => 'my description',
    'categories' => [
        [
            'id' => 9
        ],
    ],
    'tags' => [
        [
            'name' => 'my tag',
        ],
    ],

];

print_r($woocommerce->post('products', $data));
?>

Now I have some tags that are like this: (The number of tags is variable and they are in an array)

array (size=3)
  'tag1' => string 'tag1' (length=4)
  'tag2' => string 'tag2' (length=4)
  'tag3' => string 'tag3' (length=4)

I want all these tags to be sent to WooCommerce when creating a product. I put the array in the code above, it gets a key of 0 and it is not sent. what's the solution?

Maybe syntax error? In this example I'm using ids and not slug/name of tags.

<?php
    $data = [
      'name' => 'my title',
      'type' => 'simple',
      'regular_price' => '19',
      'description' => 'my description',
      'categories' => [
        [
          'id' => 9
        ],
      ],
      'tags' => [
        ['id' => 'TAG_ID_int',
        ],
        ['id' => 'TAG_ID_int',
        ],
      ],

    ];

    print_r($woocommerce->post('products', $data));
    ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM