【问题标题】:Adding tags to WooCommerce products through Rest Api通过 Rest Api 为 WooCommerce 产品添加标签
【发布时间】:2022-12-15 18:49:17
【问题描述】:

我通过下面的代码将产品添加到 WooCommerce 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));
?>

现在我有一些标签是这样的:(标签的数量是可变的,它们在一个数组中)

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

我希望在创建产品时将所有这些标签发送到 WooCommerce。 我把数组放在上面的代码中,它得到一个 0 的键,它没有被发送。有什么解决办法?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    也许语法错误? 在这个例子中,我使用的是 ids 而不是 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));
        ?>
    

    【讨论】:

      猜你喜欢
      • 2015-06-21
      • 2018-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-26
      • 1970-01-01
      • 2021-08-08
      相关资源
      最近更新 更多