简体   繁体   English

通过CLI为新产品创建新的WooCommerce类别

[英]Create a new WooCommerce category via CLI for new products

I am using the WooCommerce CLI to import a CSV with products into WooCommerce. 我正在使用WooCommerce CLI将包含产品的CSV导入WooCommerce。 That works, except that I can't figure out how to create the new categories for the imported products. 那行得通,除了我不知道如何为导入的产品创建新类别。

my products are stored in the $product array. 我的产品存储在$product数组中。 The $product['category'] is a string with the category name. $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.'"

At first I tried to use the category name unless I figured out that the --categories param needs an integer (or maybe a JSON-Object, according to another post). 起初,我尝试使用类别名称,除非我发现--categories参数需要一个整数(根据另一篇文章,它可能是一个JSON-Object)。

Because of that I am using the following code to save the current category id into a variable to use in product create (the categories and articles are in an loop). 因此,我正在使用以下代码将当前类别ID保存到要在产品创建中使用的变量中(类别和文章处于循环状态)。 But it does not work as integer ( --categories=12 ) or as JSON-Object [{"id" : 12}] . 但是它不能用作整数( --categories=12 )或JSON-Object [{"id" : 12}]

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

Just to wrap it up. 只是把它包起来。 At the moment the order is like that: 目前的顺序是这样的:

  1. Create categories and save the current category id to $cat (works) 创建类别并将当前类别ID保存到$ cat(有效)
  2. Create a new product (works) except, that the category is not set. 创建一个新产品(工程),但未设置类别。 It's marked as "uncategorized". 标记为“未分类”。

I know, I could load the article afterwards to set the category. 我知道,以后可以加载文章以设置类别。 But I think that's a bit messy. 但是我认为这有点混乱。

Got it! 得到它了! The problem was the quotes. 问题是引号。 Escaping the double quotes in the object did the job. 转义对象中的双引号可以完成此工作。

[{\"id\" : 12}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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