WP-CLI commands
WP-CLI is a console program for managing WordPress. WooCommerce adds the wp wc command group to it: products, orders and customers can be read and changed without the admin panel. That is convenient for deployment scripts, bulk operations and work on a server without a browser.
The first run
Commands are run in the site folder:
wp wc product list --user=1 --fields=id,name,price,stock_quantityThe command prints a product table with four columns. The --user parameter is required for all wp wc commands: it is the ID or login of a user allowed to manage the shop, on whose behalf the operation runs.
How commands are built
Commands mirror the REST API: a resource name, then an action.
| Action | What it does |
|---|---|
list | A list |
get <id> | One record |
create | Creating |
update <id> | Changing |
delete <id> | Deleting |
| Resource | What it is |
|---|---|
product | Products |
product_variation | Variations |
product_cat | Categories |
shop_order | Orders |
customer | Customers |
shop_coupon | Coupons |
All parameters of a resource are shown by the help: wp wc product create --help.
Products
wp wc product create --name="Saucer" --regular_price=4.50 --sku=SAUCER-001 --user=1
wp wc product update 12 --regular_price=12.90 --stock_quantity=20 --user=1
wp wc product get 12 --fields=id,name,price --user=1The first command creates a product and prints its ID, the second changes the price and stock, the third shows the result. Parameters are named the same as REST API fields.
Orders
wp wc shop_order list --status=processing --fields=id,total,date_created --user=1
wp wc shop_order update 1024 --status=completed --user=1The first command prints paid orders, the second moves order 1024 to Completed — with all hooks and emails, as with a status change in the admin panel.
Output format
wp wc product list --user=1 --format=json
wp wc product list --user=1 --format=ids
wp wc product list --user=1 --format=csv > products.csvBesides the table there are json, csv, ids and count. The ids format is handy to pass to another command, csv to save to a file.
Service commands
| Command | What it does |
|---|---|
wp wc update | Updates database tables after a plugin update |
wp wc tool list --user=1 | The tools from the Status → Tools page |
wp wc tool run clear_transients --user=1 | Clears the WooCommerce cache |
wp wc hpos status | The state of the HPOS order storage |
wp action-scheduler run | Runs the background job queue |
Your own command
A plugin can add its own command. Create the includes/cli.php file and include it in acme-shop.php:
// wp-content/plugins/acme-shop/includes/cli.php
if (defined('WP_CLI') && WP_CLI) {
WP_CLI::add_command('acme low-stock', function () {
$products = wc_get_products(['manage_stock' => true, 'limit' => 100]);
foreach ($products as $product) {
if ($product->get_stock_quantity() < 5) {
WP_CLI::line($product->get_id() . ' ' . $product->get_name() . ': ' . $product->get_stock_quantity());
}
}
WP_CLI::success('Done.');
});
}The WP_CLI check keeps the code from running on ordinary site requests. Run wp acme low-stock — the command prints products with fewer than five left.
Common mistake
A command is run without --user and fails with a permissions error: Sorry, you cannot list resources. wp wc commands pass the same permission checks as the REST API, and the console works without a user by default. Add --user=1 or an administrator login to every command.