This is the second post of the series WC CLI – WooCommerce via command-line and I’ll show you how to manage your products with WooCommerce CLI.

What can you do with products and WooCommerce CLI? Everything you can do from the editing screen can be done via command-line.

  • Creating products (or importing them from a file)
  • Updating products
  • Deleting products
  • Getting single products
  • Getting all the products together
  • Getting registered product types
  • Getting product categories

I won’t go over all the basic commands listed above, you can find those examples in the documentation.

In this article I’ll cover some commands that are not in the documentation, like importing and deleting products in bulk, creating variable products, adding images, attributes and dimensions to products.

Creating Products

You can create all the default types of product that you can create via the editing screen, so simple, variable, grouped and external products. Also you can create them in bulk from a file, like a CSV or a text file.

For the available fields for products check the documentation.

Simple Products

Creating a simple products is easy and the command is relatively short. This command would create a simple product with title Test Product, a SKU WCCLITESTP and a price of $ 20.00.

wp wc product create --title="Test Product" --type=simple --sku=WCCLITESTP --regular_price=20

External Products

It’s exactly the same of simple products, but you need to specify the product URL and the button text. Both of them are optional, but at least the product URL should be specified or you will have to manually add it later.

To the command above for simple products, you can add these parameters to specify the URL and the button text:

--product_url="https://domain.com/product/test/" --button_text="Buy me"

Also make sure to change the product type to external with --type=external.

Grouped Products

To create grouped products you need simple products. I’ll assume that you already have those created. Then you can create the grouped product:

wp wc product create --title="Grouped Product" --type=grouped --sku=WCCLITESTGROUPED

This is just the grouped product, you need to assign simple products to it. When creating a simple product make sure to specify the parent ID with the parameter --parent_id=123 where 123 is the ID of the grouped product to use.

Variable Products

play-game-meme

Now starts the fun part of the article. Creating variable products is harder than other products, you need to define attributes and variations which could be a bit tricky.

WooCommerce CLI treats variations and attributes as arrays. You need to define them one by one specifying their index in the array. For example, to define a variation regular price you would use --variations.0.regular_price=20. This tells to WooCommerce CLI that the regular price for the first variation (the 0 is the index which start from 0, not 1) is $ 20.00.

The same would be for attributes. You can find the full list of parameters available for attributes and variations in the documentation.

Let’s create a variable product:

wp wc product create --title="Variable Product Test" --type=variable
    --attributes.0.name="Color" --attributes.0.visible=yes --attributes.0.variation=yes --attributes.0.options="Black|Blue"
    --attributes.1.name="Size"  --attributes.1.visible=yes --attributes.1.variation=yes --attributes.1.options="Small|Medium"

    --variations.0.attributes.color="Black" --variations.0.attributes.size="Small"  --variations.0.regular_price=20
    --variations.1.attributes.color="Black" --variations.1.attributes.size="Medium" --variations.1.regular_price=20
    --variations.2.attributes.color="Black" --variations.2.attributes.size="Large"  --variations.2.regular_price=20
    --variations.3.attributes.color="Blue"  --variations.3.attributes.size="Small"  --variations.3.regular_price=20
    --variations.4.attributes.color="Blue"  --variations.4.attributes.size="Medium" --variations.4.regular_price=20
    --variations.5.attributes.color="Blue"  --variations.5.attributes.size="Large"  --variations.5.regular_price=20

That’s a huge command! What does it do? Check this breakdown:

The command above creates a variable products with Size and Color. There are 2 sizes (Small and Medium), and 2 colors (Black and Blue). It creates all the possible combinations of these attributes (variations) and sets their price to $ 20.00.

wp wc product create --title="Variable Product Test" --type=variable

This part creates the variable product.

--attributes.0.name="Color" --attributes.0.visible=yes --attributes.0.variation=yes --attributes.0.options="Black|Blue"
--attributes.1.name="Size"  --attributes.1.visible=yes --attributes.1.variation=yes --attributes.1.options="Small|Medium"

This one instead defines the attributes Size and Color. And the last one creates the 6 variations:

--variations.0.attributes.color="Black" --variations.0.attributes.size="Small"  --variations.0.regular_price=20
--variations.1.attributes.color="Black" --variations.1.attributes.size="Medium" --variations.1.regular_price=20
--variations.2.attributes.color="Black" --variations.2.attributes.size="Large"  --variations.2.regular_price=20
--variations.3.attributes.color="Blue"  --variations.3.attributes.size="Small"  --variations.3.regular_price=20
--variations.4.attributes.color="Blue"  --variations.4.attributes.size="Medium" --variations.4.regular_price=20
--variations.5.attributes.color="Blue"  --variations.5.attributes.size="Large"  --variations.5.regular_price=20

Note that when creating attributes and variations the command includes their indexes:

  • --attributes.0
  • --variations.0

This is needed for WooCommerce to process them properly. Make sure to start with 0 for the first attribute or variation, otherwise it won’t work properly.

Obviously creating products one by one in this way could be tedious, I’ll show you later in this post how to do it with a list of products in a file and a script, automagically.

Deleting Products

Deleting products also needs only one command, as per documentation:

wp wc product delete 123

Where 123 is the ID of the product to delete. You can also delete all the products in bulk with:

wp wc product delete $(wp wc product list --format=ids)

Updating Products

Updating products is the same of creating them, so please refer to that part. The only difference is the main command which will be:

wp wc product update 123

Where 123 is the ID of the product to update. After that, list all the fields to update with their new values.

Note: When updating variations, treat them like products by using:

wp wc product update VARIATION_ID_HERE

Do you want to bulk import products via WooCommerce CLI? Check this article by Remi Corson.

13 replies
  1. Fred
    Fred says:

    Hi,

    I looked at the article by Remi Corson and I don’t have a mac..
    Is there an alternative for windows user?
    Also Is it possible to import/export woo product using WooCommerce CLI?

    Thank you

    Reply
    • Nicola Mustone
      Nicola Mustone says:

      Hi Fred,

      Is there an alternative for windows user?

      I don’t have an alternative for Win, sorry.

      Also Is it possible to import/export woo product using WooCommerce CLI?

      It is, that script by Remi is a first step. The script to import/export products is harder than that though to write.

      I tried to write one but I don’t have a good knowledge of shell language so I wasn’t able to get it working properly.

      Reply
      • Andrew
        Andrew says:

        Nicely blog. I don’t know if they existed when you wrote this blog, but import & export now exist. Check wp help import and wp help export for details. Excerpts follow.

        wp import … –authors= [–skip=]

        Provides a command line interface to the WordPress Importer plugin, for performing data migrations.

        wp export [–dir=] [–skip_comments] [–max_file_size=]
        [–start_date=] [–end_date=] [–post_type=]
        [–post_type__not_in=] [–post__in=] [–start_id=]
        [–author=] [–category=] [–post_status=]
        [–filename_format=]

        Generates one or more WXR files containing authors, terms, posts,
        comments, and attachments. WXR files do not include site configuration
        (options) or the attachment files themselves.

        Reply
  2. Fred
    Fred says:

    Hi Nicola,

    Thank you for your replies..
    One more question please.
    Is it possible to bulk delete all the product that are in the bin?
    I cannot find a status like ‘published’, ‘bin’ or ‘draft’
    Fred

    Reply
  3. Rob Benz
    Rob Benz says:

    Is there any way to add custom product meta to the list of available fields?

    I have added some custom product meta, and I would like the ability to update or create that field.

    ex

    wp wc product update 1234 –condition=”Like New” –SKU=12345

    where condition is a custom post meta added with woo_add_custom_general_fields()

    Reply
  4. Andrew
    Andrew says:

    I’m trying to automate installation of multiple sites. I’ve got everything working except I’m stuck on how to automate the setup wizard so that shop, cart, etc. pages get created, and whatever other details the setup wizard normally take care of. Any help?

    Thanks!

    Reply
  5. Ryan
    Ryan says:

    This is a great article, but I wonder if it’s out of date?

    Woocommerce seems to be saying the CLI is deprecated and people should be using the REST API instead? I’m having a hard time figuring out the exact syntax of the “–attributes” option, but I’m coming up short.

    When I try your “array” method I get:

    Error: Parameter errors:
    unknown –title parameter
    unknown –attributes.0.name parameter
    unknown –attributes.0.visible parameter
    unknown –attributes.0.variation parameter
    unknown –attributes.0.options parameter
    unknown –attributes.1.name parameter
    […]

    I’m thinking it wants JSON instead of individually indexed array stuff, so I’ve tried things like –attributes='[{“slug”:”pa_city”,”value”:”Cityville”}]’ but I’ve been getting nowhere.

    Does anyone know how to create a product with attributes via the CLI?

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.