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
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.
Leave a Reply