无法使用 WooCommerce API 将产品添加到相应的类别/子类别

问题描述 投票:0回答:1

`嗨,我有一些类别/子类别(甚至多个嵌套子类别)由 Js 添加到 woocommerce 网站。问题是,当我尝试发布产品时,我尝试将产品(它们有一个对象的 category_path 数组,包括 id、name、url - 对于每个产品)分配给相应的类别/子类别,但唯一他们之间的匹配,就是名字。我试图仅在传递类别属性时分配名称,但在产品帖子中需要一个 ID,否则它不会将产品保存到类别中,只是将它们添加为未分类。我的代码片段:

P.S:我知道当我遍历 detailsData 时我收到了一组名称,我尝试了很多方法,但我卡住了。`

const WooCommerce = new WooCommerceAPI({
  url: "https://X.X/",
  consumerKey: "ck_X",
  consumerSecret: "cs_X0",
  wpAPI: true,
  version: "wc/v3",
});

await createCategories(WooCommerce);

for (let i = 0; i < searchResults.products.length; i++) {
  const product = searchResults.products[i];
  const detailsData = await details(apiKey, product.id);

  const sections = detailsData.category_path;

  const productData = {
    name: product.title,
    type: "simple",
    categories: [
      {
        name: sections.name, 

      },
    ],
  };
  await WooCommerce.post("products", productData);
}

来自 category_path 的名称与类别/类别名称相匹配 我希望根据匹配的名称实现类别中的产品。

javascript woocommerce woocommerce-rest-api
1个回答
0
投票

所以,我将我所有的类别/子类别从 Woocommerce 网站保存到一个 json 中,并基本上将基于该类别的产品添加到相应的类别中,这样速度更快,因为它不必在每个产品之后运行一个循环。

© www.soinside.com 2019 - 2024. All rights reserved.