If there’s one thing developers love, it’s finding ways to speed up and automate boring, manual tasks for WooCommerce development. After all, that’s one of the reasons why we built Robot Ninja.
And I’m sure a lot of developers would agree with me that WP-CLI is one of the best inventions since sliced bread. It’s my favorite way to set up new WordPress sites, work with WooCommerce, manage plugins, test code, and a whole bunch of other tasks that are just so much quicker to do via command line.
Even better, WooCommerce CLI (WC-CLI) allows developers to tackle tasks in WooCommerce development more efficiently. Want to create a new product for your store? You can get it done with a single command, which is faster than clicking around the WordPress admin.
In this post, I’ll run you through how WP-CLI and WC-CLI work and give you a rundown of my go-to commands for spinning up new WordPress sites and WooCommerce development.
What is WP-CLI?
For a long time, WordPress didn’t have a way for developers to work around simple tasks that would take just a few lines of code. But that changed when WP-CLI came along in 2009.
If you’re new to the magic of WP-CLI, here’s how it’s described on the official website:
“WP-CLI is a set of command-line tools for managing WordPress installations. You can update plugins, set up multisite installs and much more, without using a web browser.”
Originally developed by Andreas Creten and Cristi Burcă, the project was maintained by Daniel Bachhuber until it was picked up as the official WordPress command line tool under the WordPress.org umbrella in December 2016.
When WordPress co-founder Matt Mullenweg announced WordPress’ support for the project, he called WP-CLI “one of the highest impact developments for WP in many years.”
WP-CLI and WC-CLI
While WP-CLI is a set of command-line tools for managing WordPress installations, the WooCommerce CLI (WC-CLI) layer adds tools for managing products, coupons, payment gateways, shipping zones, and more.
WooCommerce support for WP-CLI was introduced way back in 2.5 and there have been a ton of improvements since then. In versions 2.5 and 2.6, the CLI was powered by its own code separate from the REST API or WooCommerce core, which meant code could end up being duplicated across the code base. It also meant certain things possible in the REST API were not possible at all with the CLI.
WooCommerce 2.7 was a game changer—it introduced a new CLI powered by the REST API by forking RESTful. So most of what is possible with the REST API can also be achieved via command-line.
The full list of available WC-CLI commands (as of WooCommerce 3.0) are listed on the WC-CLI wiki page, which contains more information and examples.
Save time and worry less about updates.
Take all the tedious manual work of testing your site away and let Robot Ninja take care of it for you!
Using WC-CLI for Development
Here’s a quick example to show you just how powerful—and time-saving—WC-CLI can be.
Imagine you’re creating a new online store for a client and you have a huge list of products to add, which are in CSV format.
If you were to set everything up without WP-CLI and WC-CLI, you would have to:
- Download and install WordPress manually (which doesn’t take 5 minutes, despite the “famous” 5-minute install!) – 10 minutes
- Install and activate plugins, one-by-one (including WooCommerce) and your theme – 10 minutes
- Create every single product in the CSV manually, or import them with a plugin – could take days
The same process with WP-CLI and WC-CLI:
- Download and install WordPress via command line – 2 minutes
- Install and activate plugins (including WooCommerce) and theme via command line – 2 minutes
- Create product via command line – this would still take a long time, but not days.
The fact is, computers are faster than humans. And who wants to spend all day clicking around WordPress anyway?
Getting Started with WP-CLI
Before you can start playing around with WC-CLI, you need to set up WP-CLI, making sure your hosting environment meets the basic requirements:
- A UNIX-like environment.
- PHP 5.3.29 (or later).
- WordPress 3.7 (or later).
- Secure Shell (SSH) access to your hosting environment.
Obviously, you need access to the command line, but not all hosts provide it. So get in touch with your host to find out if it’s available. Most (if not all?) managed WordPress hosts have support WP-CLI, including:
I won’t go into detail about how to install and set-up WP-CLI here. Instead, I highly recommend you check out the installation guide on the WP-CLI site.
Core and Community WP-CLI Commands
There are two main classes of WP-CLI commands:
- Core commands – The core list of WP-CLI commands.
- WP-CLI packages – Community maintained commands.
- Bundled commands – Commands that are bundled with plugins.
A quick look through the huge list of community commands could save you a bunch of time, with all kinds of commands for day-to-day tasks like security checks, deployment and migration assistant tools, search and replace (great for syncing test and production sites), database checkpoints, and more.
One of the best things about WP-CLI is that you’re not limited to the core commands—you can define your own custom commands (I’ve created my own presets—more on that later), which is as simple as writing your own PHP class. The commands cookbook sets out how to write your own commands.
For a non-exhaustive list of commands available in plugins, check out the Tools page on the WP-CLI site, which lists all kinds of commands for popular and lesser-known plugins.
WP-CLI Commands for WordPress Development
Everything you can do via the WordPress admin can be done via command line for WooCommerce development. Here are my go-to WP-CLI commands.
Setting Up a New WordPress Install
Let’s start with the basics. To download and extract the latest version of WordPress to the current directory:
To create the wp-config.php file and configure the database settings:
Finally, to run the install command to set up the database:
wp db <command>
This command is for basic database operations using credentials stored in wp-config.php. It’s by far the command I use the most for quickly making a database backup/export. You can read more about how to use it over on the WordPress Developer Handbook site.
Exporting your database is easy and the command is relatively short. I usually run this quick command before making updates, or when testing WooCommerce features locally in case I want to revert the site back to a specific state from before I started tinkering with it.
When I want to revert a site back to a specific state, this is the command:
Other Useful Commands
To display the size of the tables in the database:
To quickly optimize the database:
To repair the database:
The wp post generate command is useful for creating posts with dummy content for test sites.
Search and Replace
To search and replace all the strings in the database, the WP-CLI team have created a handy package, which you can download from GitHub.
This command searches through all rows in a selection of tables and replaces appearances of the first string with the second string:
It handles PHP serialized data and doesn’t change primary key values.
Local Development with Laravel Valet
If you use Laravel Valet, this package is useful for setting up a local development environment.
This command will spin up a new WordPress installation—complete with database and HTTPS:
Saving Time with WP-CLI Presets
Trying to remember every single WP-CLI command can be painful at times—it takes 3 commands just to install WordPress. So I created my own personal presets, which run through some common actions I do day-to-day in WooCommerce development.
You can check out my presets here on GitHub.
Basically, it’s a wrapper for a bunch of other WP-CLI commands that I use locally to make the process of spinning up new sites a little quicker. For example:
Or, to clone WooCommerce, Subscriptions, Stripe etc:
Do you have any time-saving tips for WP-CLI? I’d love to hear if you have similarly created your own personalized commands.
WC-CLI Commands for WooCommerce Development
Like WP-CLI, everything you can do in the WordPress admin during WooCommerce development can also be done via command line, such as:
- Creating products (or importing them from a file)
- Updating/deleting products and orders
- Creating/updating customer data, passwords, coupons
- Reviewing products
- and a whole lot more
You can quickly tell a WC-CLI command from a WP-CLI—it’ll be prefaced by a “wc” rather than a “wp.”
For a full list of WC-CLI commands, check out the WC-CLI wiki page.
With WC-CLI, you can create all the default types of products that you can create via the WordPress admin, i.e. simple, variable, grouped, and external products.
WooCommerce Happiness Engineer Nicola Mustone has previously published a fantastic post about managing products with WP-CLI, which I highly recommend checking out. Here are the basics:
This command would create a simple product with title Test Product, a SKU WCCLITESTP and a price of $20.00.
Creating external products is similar to the command above, but you need to also specify the product URL and the button text. These are options, but it’s a good idea to enter the product URL so you don’t have to manually do it later.
Add these parameters to the command above for simple products to specify the URL and the button text:
Also, make sure to change the product type to external with
For more on grouped products and variable products, check out Nicola’s post.
Creating Bulk Products
When creating bulk products for testing, I’ve found WC Cyclone really useful. WC Cyclone is a WordPress plugin that adds several WP-CLI commands for generating fake WooCommerce data.
For example, to generate a bunch of products, you would use this command:
By default, the product type is
books but can also be
Getting a List of Orders
Want to see a list of orders? This command is useful for quickly viewing orders on your development site and for when you’re testing:
I’d recommend specifying the fields you want to see to avoid a lot of data appearing in your terminal.
Getting a List of Customers
Similar to the command above, this one gives you a list of customers:
Again, I highly recommend specifying the fields you want to see.
Both this command and the one above for getting a list of orders are commands I rely on heavily during development. They save me so much time—I don’t have to click through the WordPress admin to view certain variables, especially if I find I’m repeating tests after making a lot of changes.
WooCommerce Extensions That Work with WP-CLI
As I mentioned earlier, a lot of plugins come with bundled WP-CLI commands, and WooCommerce extensions are no exception.
WooCommerce Memberships (a SkyVerge product) and WooCommerce Subscriptions (a Prospress product, like Robot Ninja) are two extensions I use a lot. Here are some useful commands that come with these extensions:
Managing WooCommerce Memberships plans and members is quick and easy via command line. This is the primary command for this extension:
To create an unlimited membership for user 1 to plan 100:
This command creates an expired silver membership for user 1 that started on Jan 1, 2016, and ended on June 30, 2016:
When you’re developing locally and repeating these functions a lot, having the ability to copy and paste these commands rather than click through the WordPress dashboard is a huge time-saver.
Combining a bunch of the common commands you run into a script, or something like the presets commands I mentioned earlier, can really help with automating tedious, repetitive commands.
For a detailed rundown of all the WP-CLI commands for WooCommerce Memberships, check out the documentation.
WooCommerce Subscriptions + Action Scheduler 2.0
Action Scheduler is a robust job queue and background processing library for use in WordPress plugins and WooCommerce extensions. Version 2.0 also comes with some new WP-CLI commands for running scheduled actions. This command provides an important new way to run scheduled actions, especially for high-volume stores.
Whilst the Action Scheduler WP-CLI commands aren’t specifically for development, they are really useful for developers/site admins who want optimal performance for running scheduled actions.
WooCommerce Subscriptions uses Action Scheduler to handle triggering actions, such as subscription renewals on a recurring or one-off basis.
Out-of-the-box, Action Scheduler makes use of the default WP Cron runner to trigger these actions. For many sites, specifically sites with large numbers of actions, relying on WP Cron can become a performance hazard. Using the WP CLI commands you can now speed up these actions and do cools things like create queues to process only actions in a specific group, or with a specific hook or hooks. When processing large batches of actions concurrently, making each queue handle a specific action can greatly increase the rate of processing and reduce any potential for issues like database deadlocks.
Common cases where WP-CLI is a better option include:
- Long-running tasks – Tasks that take a significant amount of time to run.
- Large queues – A large number of tasks will naturally take a longer time.
- Other plugins with extensive WP Cron usage – WP Cron’s limited resources are spread across more tasks.
With a regular web request, you may have to deal with script timeouts enforced by hosts, or other restraints that make it more challenging to run Action Scheduler tasks. Using WP CLI to run commands directly on the server gives you more freedom, and means that you typically don’t have the same constraints of a normal web request.
There are a number of WP-CLI commands and options available. The best way to get a full list of commands and their available options is to use WP-CLI itself. This can be done by running
wp action-scheduler to list all Action Scheduler commands, or by including the
--help flag with any of the individual commands. This will provide all relevant parameters and flags for the command.
We’ve barely scratched the surface of what WP-CLI and WC-CLI can do in this post. For WooCommerce development, I highly encourage you to check out this fantastic tool. Here are some resources to help get you up to speed:
- WP-CLI website (wp-cli.org) – Learn more about the official command line interface for WordPress.
- WP-CLI handbook (wordpress.org) – The official WordPress developer handbook entry.
- WP-CLI Commands – Full list of WP-CLI commands.
- WC-CLI Commands – Full list of WC-CLI commands.
- WooCommerce WP-CLI Basics – Automattic Engineer Rémi Corson put together this excellent presentation on the basics for a WordPress Meetup.
- Managing Products with WooCommerce CLI – WooCommerce Happiness Engineer Nicola Mustone has published some handy commands for WooCommerce development via command line.
- Bulk Import WooCommerce Products with WP-CLI & Alfred From a Text File – A straightforward guide also put together by Rémi Corson. His blog is a good one to bookmark for other WooCommerce development tutorials.
If you’re like me, you enjoy the thrill of fixing problems, building things, and losing yourself in lines of code. But there’s only so much time in the day. You’ve probably got better things to do than the same boring tasks over and over.
WP-CLI is such an amazing, handy tool. As I’ve talked about in this post, it’s my go-to for everything from setting up local installs of WordPress to managing WooCommerce products. Hopefully, this overview has given you a good basis for understanding how WP-CLI and WC-CLI work and how you can use the command line to speed up your own WooCommerce development.
If you’re interested in learning more about how we develop with WooCommerce, check out WooCommerce Developer’s Toolbox: 10 Tools We Use at Robot Ninja.
Do you use WP-CLI? Got any questions about using WP-CLI and WC-CLI? Let us know in the comments below.