WP-Cli wc 产品创建自定义属性



美好的一天,

我正在使用wp-cli将产品添加到 Wordpress,例如我使用这个:

wp --allow-root wc product create --user=1 --name="Restricted" --regular_price=1 

我确实有一些称为test_1的属性(复选框为是(,test_2是多选。但是有没有办法填充这些属性?

我确实尝试过这个:

wp wc product create --user=1 --name="Restricted" --regular_price=1 --test_1=yes --test_2=testvalue,testvalue2

但这确实导致了一个错误:

Error: Parameter errors:
unknown --test_1 parameter
unknown --test_2 parameter

并做了这个,但值仍然是空的:

wp wc product create --user=1 --name="Restricted" --regular_price=1 --attributes='[{"test_1": "yes", "test_2": ["testvalue","testvalue2"]}]'

而这个:

wp wc product create --user=1 --name="Restricted" --regular_price=1 --attributes='[{"test_1": 1, "test_2": ["testvalue","testvalue2"]]'

您需要将attributes指定为 JSON。由于您有 2 个属性,因此正确的命令以及 JSON 结构是。

wp wc product create --name='Product Name'  --user=1 
--attributes='[ 
{ "name": "test_1", "visible": true, "options" : ["yes", "no"] }, 
{ "name": "test_2", "visible": true, "options" : ["small", "medium"] } 
]'

在此处查看第二个常见问题解答

它说某些属性需要作为 JSON 传递。

一些"列表"是对象,例如,如果要为产品设置类别,则 REST API 需要一个对象数组:https://woocommerce.github.io/woocommerce-rest-api-docs/#product-properties

此参考使用 WP-CLI 创建 wooCommerce 产品

https://github.com/woocommerce/woocommerce/wiki/WC-CLI-Overview#frequently-asked-questions

https://nicola.blog/2016/03/10/managing-products-woocommerce-cli/

https://woocommerce.github.io/woocommerce-rest-api-docs/#product-properties

if you add product Custom attributes or category through CLI than use  JSON format like this
--attributes= [{ "name": "color", "visible": true, "options":["black","blue"]}]
--categories= [ { "id" : category_id } ]
Example demo:-
wp wc product create --name="mobile11" --description="this is mobile 11" --type=simple --regular_price=500 --sale_price=400 --user=dharmesh --categories='[ { "id" : 35 } ]' --attributes='[{ "name": "color", "visible": true, "options":["black","blue","red"]}]' --allow-root

大多数时候终端格式不正确,有时会跳过,有时不格式化 bash 变量会导致空值。这取决于您如何使用声明 bash 变量以及在 woocommerce cli 中使用。

我正在寻找添加/更新产品属性的正确格式。这就是我能够为我的产品添加产品属性的方式。

wp wc product update 2898  --user=1  --attributes='[{ "name":"Background Style","options":"White"},{ "name":"Demographic Level","options":"college-university"}]'

哪里

product_id是 2898 属性为"背景样式"和"人口统计级别",options是其相应的术语。

最新更新