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 an 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, 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 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

ChatGPT’s artistic rendering of my chat. LMAO!