skip to Main Content

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


  1. You need to specify attributes as JSON. Since you have 2 attributes, the proper command along with JSON Structure is.

    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"] } 
        ]'
    

    Check the 2nd FAQ here

    It says that certain properties needs to be passed as JSON.

    Some ‘lists’ are objects, for example, if you want to set categories for a product the REST API expects an array of objects: https://woocommerce.github.io/woocommerce-rest-api-docs/#product-properties

    Login or Signup to reply.
  2. 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

    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
    
    Login or Signup to reply.
  3. 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.

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

    Where,

    product_id is 2898
    attributes are "Background Style" and "Demographic Level" and options are its corresponding terms.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search