简体   繁体   中英

Create a new WooCommerce category via CLI for new products

I am using the WooCommerce CLI to import a CSV with products into 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. The $product['category'] is a string with the category name.

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).

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). But it does not work as integer ( --categories=12 ) or as 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)
  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}]

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