Good day,
I’m using the wp-cli
for adding products to WordPress, for example i use this one:
wp --allow-root wc product create --user=1 --name="Restricted" --regular_price=1
I do have some attributes called test_1 (checkbox for yes) and test_2 is a multiselect. But is there a way to fill that attributes?
I did try this:
wp wc product create --user=1 --name="Restricted" --regular_price=1 --test_1=yes --test_2=testvalue,testvalue2
But that did result in an error:
Error: Parameter errors:
unknown --test_1 parameter
unknown --test_2 parameter
And did this one, but the values were still empty:
wp wc product create --user=1 --name="Restricted" --regular_price=1 --attributes='[{"test_1": "yes", "test_2": ["testvalue","testvalue2"]}]'
And this one:
wp wc product create --user=1 --name="Restricted" --regular_price=1 --attributes='[{"test_1": 1, "test_2": ["testvalue","testvalue2"]]'
3
Answers
You need to specify
attributes
as JSON. Since you have 2 attributes, the proper command along with JSON Structure is.Check the 2nd FAQ here
It says that certain properties needs to be passed as JSON.
this reference to create woocommerce product using WP-CLI
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
Most of the time terminal isn’t formatted properly sometimes it skips , sometime not formatting bash variable causes empty values. It depends how you’re using declaring bash variable and using within woocommerce cli.
I was looking way correct format for adding / updating product attributes. This is how I was able to add product attributes for my product.
Where,
product_id is 2898
attributes are "Background Style" and "Demographic Level" and
options
are its corresponding terms.