I recently had a client, Stay For Life, mistakenly delete her WooCommerce inventory quantities for a variable product that had 12 variants. It was a sweatshirt product with multiple colors and sizes. What a bummer. Unfortunately, there were other changes to the database that had happened after she realized the issue, so doing a simple database restore via the SpinupWP dashboard wasn’t a great solution. So I had to figure out how to export WooCommerce inventory in a quick and useful way from a backed-up database.
How I Approached the Export WooCommerce Inventory Dilemma
My first instinct was to download the database from the nightly backup the day before the deletion happened. And then I restored it to a local site I have running using LocalWP. All I cared about was restoring the database, and not the files. I also needed to make sure WooCommerce was installed. This didn’t need to be pretty; it just had to get me visibility into what the inventory was.
Restore the Database
So that’s what I did. After running wp db import my-backup-database.sql
and making sure my wp-config.php
(or in my case, .env file using Bedrock) was set to the correct database prefix, I logged in to the wp-admin. I then navigated to the product that was in question and looked at each variation’s quantity.
Export The Inventory Data
My next move was to copy and paste this data, one by one, to the client or directly into the live site. But then I paused. I hate doing repetitive work like that. There must be a better way.
Enter my friend, ChatGPT.
Word Smarter, Not Harder with ChatGPT
I simply asked Chat the question:
any way i could easily export inventory numbers from woocommerce, like via wp cli?
The response gave me a WP CLI command using WP Eval that would loop through the products, grab the stock quantity, and add it to a CSV. The only issue with this solution is that it didn’t take into account variable products.
So then I asked:
I want to make sure we capture the stock of a particular variable product
And that’s when the magic happened. I looked at the code, and in a glance it seemed to have accounted for what I needed, so I hastily copied and pasted it into terminal and scurried to the file it created for me in Finder. I opened it up and, viola! It had all the proper stock quantities for each variable product! Yippee!
The Final Working WP CLI Snippet
Here’s the code snippet that anyone can copy and paste into their own instance of a Wordpress site running WooCommerce and WP CLI to quickly get a file with stock quantities for variable products:
wp eval ' $products = wc_get_products([ "limit" => -1, "status" => "publish", "type" => ["simple", "variable"], ]); $csv = fopen("php://output", "w"); fputcsv($csv, ["Product ID", "Variation ID", "SKU", "Product Name", "Attributes", "Stock"]); foreach ($products as $product) { if ($product->is_type("simple")) { fputcsv($csv, [ $product->get_id(), "", // no variation ID $product->get_sku(), $product->get_name(), "", // no variation attributes $product->get_stock_quantity(), ]); } elseif ($product->is_type("variable")) { $variations = $product->get_children(); // Get variation IDs foreach ($variations as $variation_id) { $variation = wc_get_product($variation_id); fputcsv($csv, [ $product->get_id(), $variation->get_id(), $variation->get_sku(), $product->get_name(), wc_get_formatted_variation($variation, true, false, false), $variation->get_stock_quantity(), ]); } } } fclose($csv); ' > stock-export.csv
If you want to read the full ChatGPT conversation, you can here: https://chatgpt.com/share/6814f178-41ec-800e-916d-17b01d895436

Frequently Asked Questions
What should I do if I accidentally delete WooCommerce inventory for a variable product?
If you’ve accidentally deleted WooCommerce inventory, especially for variable products, the first step is to locate a recent backup. You can restore the database using tools like SpinupWP or manually import it into a local environment using LocalWP. This helps retrieve lost WooCommerce inventory data without affecting the live site.
Can I restore WooCommerce inventory from a database backup without restoring the entire site?
Yes! You can download a backup of your database and import it into a local WordPress environment. With WooCommerce installed, you can access the admin dashboard and manually check the WooCommerce inventory for specific products—especially useful when only the database (not files) needs review.
Is there a fast way to export WooCommerce inventory using WP CLI?
Absolutely. Using WP CLI with a custom wp eval
command, you can loop through all products (including variable products) and export WooCommerce inventory data to a CSV file. This method is efficient and avoids the need for manual data entry.
How do I export WooCommerce inventory for variable products specifically?
Variable products have multiple variations (like size and color), each with its own stock level. A tailored WP CLI script can capture each variation’s WooCommerce inventory, including SKU, product name, attributes, and stock quantity, and save it in a CSV format.
Why is managing WooCommerce inventory for variable products more complex?
Each variation in a variable product has its own inventory value. Unlike simple products, WooCommerce inventory for variables must track each variant individually, making bulk exports or manual updates more time-consuming unless automated.