Bulk Import WooCommerce Products with WP-CLI & Alfred From a Text File
The other day was rainy and I was bored, so I decided to play a bit with the WooCommerce WP-CLI integration. And as I am a huge fan of WP-CLI, I wanted to create a products import system linked to an Alfred 2 workflow, because I love Alfred.
Before I begin, what’s WP-CLI? Well, it’s a super powerful tool that allow you to play with WordPress using commands in the Terminal. You can do pretty much everything: create pages, delete posts, edit users, edit settings, activate/deactivate themes and plugins etc… I recommend you to install WP-CLI, I am sure you’ll love it!
And finally, WooCommerce, the best e-commerce plugin for WordPress. Version 2.5 introduces WP-CLI compatibility, which, when combined to Alfred, is just amazing.
So, here is what I did. I build an Alfred 2 workflow that bulk imports WooCommerce products from a text file. That works absolutely fine, here is the final result:
So, i created a text file called products.txt where I entered the products names and their regular price. You could add as many details as you want. You can get the list of all available fields by typing the following command in the terminal:
wp help wc product create
Then I created an Alfred workflow:
I defined a path to the text file:
And I finally added a loop through lines of the text file:
When enabling the debug mode in Alfred 2, you should see something like this:
If you want to import more fields, simply use the following structure:
and make sure you defined the value within the while loop. The value after — must be a valid value that you can get by typing the WP-CLI command mentioned above, while the field value can be whatever you want. I just prefer keeping the same field name (they are based on WordPress post meta).
Let’s say you want to import the sale price, the code would become:
And that’s pretty much it. Of course, I don’t cover the whole process of how to install WP-CLI, it’s pretty easy and you can find tons of tutorials on the Internet. Same for Alfred Workflows creation, you can find awesome tutorials everywhere. The point here is to explain how you can connect those elements together.
I added a few nice things like the page being refreshed automatically when the import is done etc… so, my final words on things are that:
- it’s pretty simple to bulk import data in WooCommerce from a text file, it can be users, orders, coupons, products etc…
- it’s fun
- it’s so amazing when it’s done from a single command in Alfred ;-)
If you want to know more about this experiment, or if you have suggestions to make it better, please feel free to leave a comment!