【问题标题】:Create a new WooCommerce category via CLI for new products通过 CLI 为新产品创建新的 WooCommerce 类别
【发布时间】:2019-02-28 08:37:02
【问题描述】:

我正在使用 WooCommerce CLI 将包含产品的 CSV 导入 WooCommerce。这行得通,只是我不知道如何为导入的产品创建新类别。

我的产品存储在$product 数组中。 $product['category‘] 是一个带有类别名称的字符串。

wp wc product create --sku="'.$product['sku'].'" --type="simple" --name="'.$product['name'].'" --regular_price="'.$product['price'].'" --categories="'.$catid.'" --images="{'.$product['image'].'}" --user="'.$user.'"

起初我尝试使用类别名称,除非我发现 --categories 参数需要一个整数(或者可能是 JSON-Object,根据另一篇文章)。

因此,我使用以下代码将当前类别 ID 保存到一个变量中以在产品创建中使用(类别和文章处于循环中)。但它不能用作整数 (--categories=12) 或 JSON-Object [{"id" : 12}]

$cat = shell_exec('wp wc product_cat create --name="'.$product['category'].'" --porcelain --user="'.$user.'"');

只是为了结束它。目前顺序是这样的:

  1. 创建类别并将当前类别 ID 保存到 $cat (works)
  2. 创建一个新产品(作品),但未设置类别。它被标记为“未分类”。

我知道,我可以稍后加载文章来设置类别。但我觉得这有点乱。

【问题讨论】:

    标签: php wordpress woocommerce command-line-interface


    【解决方案1】:

    知道了!问题是引号。转义对象中的双引号就可以了。

    [{\"id\" : 12}]
    

    【讨论】:

      猜你喜欢
      • 2019-07-19
      • 2014-09-13
      • 1970-01-01
      • 2018-10-22
      • 2012-07-15
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多