Search code examples
wordpresswoocommercewp-cli

WP-Cli wc product create Custom attributes


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

Solution

  • 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