Using the WordPress CLI with WooCommerce

The other day I was looking at how to bulk load test products into WooCommerce with the aim of load testing for speed etc. It occurred to me that it would be hard to approach the topic of bulk loading test products without first looking at the WP Cli. So here’s an article that looks at using the WordPress CLI with WooCommerce.

What is the WordPress CLI?

The WordPress CLI is the command-line interface for WordPress. It basically allows you to carry out WordPress actions via the command line.

Learn How To Change Your Store Without Hiring a Coder
Click here to enroll in our Free WooCommerce Coding Course

So, instead of having to log-in to a WordPress site in a web browser you are able to log on to the machine that hosts your WordPress site an issue commands from the command line.

If that all sounds a bit pointless then imagine having to install 10 plug-ins to a WordPress site, if you installed the plug-ins via the web interface it would be quite a lengthy process and you’d need to wait for each plugin to install and then make a number of mouse clicks to install  the next plugin.

Using the WordPress CLI you can just issue a command like this

wp plugin install akismet --activate

and then “boom”, the plugin is installed and activated without anyone needing to sit in front of a browser clicking the mouse.

You can see that if you needed to install 10 plugins the CLI would be a significant time-saver, if you also needed to install the plugins across multiple sites then you can really begin to appreciate the time savings a CLI interface could bring.

If you want to find out a bit more about writing WP CLI scripts, I’d recommend Nate Finch’s blog post on how he wrote his first CLI script.

How to use the WordPress CLI with WooCommerce

When you install the WooCommerce plugin it adds functionality to the to WP CLI, you can access the functionality by issuing a command similar to the one below

wp wc <some commands>

You can see a summary of all of the WooCommerce CLI functionality on the WooCommerce GitHub pages.

If you’re interested in the code that adds the functionality it can be viewed here.

Let’s look at a quick example to show what you can do with the WooCommerce CLI. In the introduction to this article I mentioned adding products to Woocommerce, the CLI makes this very easy, we only need to issue the following command

wp wc product create --name="Test Product" --type=simple --sku=WCCLITESTP --regular_price=20 --user=admin

The command adds a new simple product with the name “Test Product” the SKU “WCCLITESTP” and a price of $20. Note that we have to supply a username at the end of the argument list. This user needs to have permissions to perform the action issued in the command. So the admin user would need to have permissions to add products to the website we’re running the command against.

Using WP CLI with Local by Flywheel

As mentioned previously, I like to use Local by Flywheel for local WooCommerce development.

Local by Flywheel uses Docker instances to host each development website you spin up, and this can cause some difficulties when trying to run a WP CLI command against a specific Local website.

The easiest way to get around this is to select “Open Site SSH” from the right-click context menu of the website you wish to run the WordPress CLI against

Loca by Flywheel will then open an SSH window for you and you can run CLI commands from there without any further set-up.

Unfortunately, on Windows at least, Local by Flywheel does not give you the option to change the SSH client so you can run the commands via the Windows DOS prompt.

Running WP CLI Outside of the Local by Flywheel SSH Window

It would be nice to be able to run the WP CLI outside of the SSH window provided by Local by Flywheel, unfortunately, on a Windows machine, I haven’t been able to get it running.

I think it should be possible, there’s an excellent guide on how to set up the WordPress CLI on Windows on here.

When trying to get everything working I get this error

Fatal error: Uncaught Error: Call to undefined function mysql_connect()

I believe this is because the WP CLI is unable to access the database that is been used by the Local by Flywheel website I’m trying to issue the commands against.

There is a way to create a config file specific to the WordPress CLI for a website and use that to point at the Local database you wish to use.

Sal Ferrarello has an excellent guide on how to set up all this on his website –  https://salferrarello.com/wp-cli-local-by-flywheel-without-ssh/

There is also an NPM package that does all the set-up for you, you can read more details about that here – https://asharirfan.com/local-wpcli-for-local-by-flywheel/

There’s also a good thread on the WordPress CLI config on WordPress Stack Exchange  here – https://wordpress.stackexchange.com/questions/190973/how-to-use-wp-cli-yml-file

I believe the problem is linked to the WP CLI using different PHP binaries to the ones used by Local by Flywheel, but that’s only a theory. If you’ve been able to get this working on a Windows box I’d be very interested to hear how you did it in the comments.


Warning: Undefined array key "format" in H:\root\home\bravenewworld101-001\www\hardworkingnerd\wp-content\plugins\convertkit\includes\class-convertkit-resource-forms.php on line 330

Leave a Comment