TechEarl

Update WooCommerce Prices, Stock and Sales from Google Sheets

Validate SKU-based product changes from a Google Sheet, preview them, create private backups, and update WooCommerce prices, inventory and scheduled sales. Plus an idempotent WP-CLI command that bulk-creates products from a CSV.

Ishan KarunaratnePublished ⏱️ 12 min readUpdated
Share thisCopied
Update WooCommerce prices, stock and scheduled sales from a Google Sheet by SKU, with a preview and backups, or bulk-create products from a CSV with WP-CLI.

To update WooCommerce products from a Google Sheet, resolve rows by SKU, validate the entire input, preview the changes, then write through WC_Product setters and save(). Direct changes to _price or _stock bypass product data-store behavior and can leave lookup data and caches inconsistent.

I separate the sheet download from the write. That gives the operator an immutable JSON input to review, archive and rerun, instead of reading a changing sheet halfway through a live operation. The workflow below handles regular prices, owned stock quantities, status-only inventory and scheduled sales for simple products and variations. When the rows are new products rather than edits, the last section, bulk-create products from a CSV, is an idempotent WP-CLI command that creates or updates by SKU.

Sheet schema and SKU identity

Use these exact headers:

text
sku,regular_price,quantity,stock_status,sale_price,sale_from,sale_to
skuregular_pricequantitystock_statussale_pricesale_fromsale_to
MUG-BLU24.99719.992026-10-012026-10-07
TS-RED-M0
SERVICE-ONEinstock
CAP-BLKCLEAR

Blank means leave unchanged. 0 is a real price or quantity. CLEAR is accepted only in sale_price and removes the sale price and both dates. A nonblank numeric sale price replaces its schedule: blank dates mean an open-ended boundary for that sale. Dates without a sale price are rejected. Each SKU must occur once and resolve to exactly one product or variation.

A quantity row requires stock already managed directly by that product/variation. A status-only row requires unmanaged stock. The updater deliberately rejects a variation inheriting its parent's stock: silently moving it into an independent stock pool would change fulfillment behavior. Change stock ownership separately, after checking the store configuration.

Read the sheet with a service account

Enable the Sheets API, create a service account and share the spreadsheet with its email as a viewer. Keep its JSON key outside the web root and source control. The official Python authentication/client libraries avoid hand-maintaining a JWT implementation:

bash
python3 -m venv .venv
.venv/bin/pip install google-api-python-client google-auth

Save this as read-product-sheet.py. Format SKU and date columns as plain text in the sheet; the reader uses unformatted values so currency display formatting does not become a price string.

python
#!/usr/bin/env python3
"""Export a reviewed WooCommerce sheet to local JSON.
Author: Ishan Karunaratne - https://techearl.com/woocommerce-bulk-update-products-google-sheet
"""
import argparse
import json
import os
from decimal import Decimal
from google.oauth2 import service_account
from googleapiclient.discovery import build

parser = argparse.ArgumentParser()
parser.add_argument("--key", required=True)
parser.add_argument("--sheet", required=True)
parser.add_argument("--range", default="Products!A:G")
parser.add_argument("--output", required=True)
args = parser.parse_args()
credentials = service_account.Credentials.from_service_account_file(
    args.key, scopes=["https://www.googleapis.com/auth/spreadsheets.readonly"]
)
service = build("sheets", "v4", credentials=credentials, cache_discovery=False)
values = service.spreadsheets().values().get(
    spreadsheetId=args.sheet, range=args.range,
    valueRenderOption="UNFORMATTED_VALUE",
).execute().get("values", [])
headers = ["sku", "regular_price", "quantity", "stock_status", "sale_price", "sale_from", "sale_to"]
if not values or values[0] != headers:
    raise ValueError("Sheet headers do not match the required schema")
rows = []
for number, cells in enumerate(values[1:], 2):
    if not any(str(cell).strip() for cell in cells):
        continue
    if len(cells) > len(headers):
        raise ValueError(f"Extra columns in row {number}")
    cells = cells + [""] * (len(headers) - len(cells))
    for index in (0, 5, 6):
        if not isinstance(cells[index], str):
            raise ValueError(f"SKU and dates must be plain text, row {number}")
    row = {}
    for key, cell in zip(headers, cells):
        if isinstance(cell, bool):
            raise ValueError(f"Boolean value in row {number}")
        row[key] = format(Decimal(str(cell)), "f") if isinstance(cell, (int, float)) else str(cell).strip()
    rows.append(row)
if not rows:
    raise ValueError("No product rows")
# Exclusive creation prevents an unnoticed overwrite of a reviewed input file.
fd = os.open(args.output, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
with os.fdopen(fd, "w", encoding="utf-8") as output:
    json.dump(rows, output, ensure_ascii=False, indent=2)
    output.write("\n")
print(f"Wrote {len(rows)} rows to {args.output}")
bash
.venv/bin/python read-product-sheet.py \
  --key=/srv/private/sheets-reader.json \
  --sheet=YOUR_SPREADSHEET_ID \
  --output=/srv/private/products-review.json

Do not use a publicly published CSV for private supplier data. A failed API request raises an error here instead of pretending the sheet was empty and reporting success.

The footgun: do not write _price directly

For ordinary product pricing, _regular_price and _sale_price are inputs; the active price and lookup records also depend on sale timing and product type. Use WooCommerce's public object API:

php
$product = wc_get_product( $product_id );
if ( ! $product ) {
    throw new RuntimeException( 'Product not found.' );
}
$product->set_regular_price( '24.99' );
$product->save();

Normalize values before comparing them. A sheet value of 19.9 and a stored value of 19.90 should not create an endless stream of “changes.” Store prices using the configured currency precision, not an assumed two decimal places for every store.

A dry-run updater with a checked backup

This example requires PHP 8.1 or later for checked file synchronization. Save it as a private apply-products.php outside the web root. It runs through wp eval-file, so WordPress and WooCommerce are already loaded. The first argument is the reviewed JSON file; optional live and a new private backup filename enable writes.

This example keeps the input and plan in memory. Split very large catalogs into reviewed files of a suitable size. It is not a transaction with checkout: schedule quantity reconciliation for a controlled window, or use an inventory integration designed for concurrent orders and reservations.

php
<?php
/**
 * Apply a reviewed WooCommerce product sheet.
 * Author: Ishan Karunaratne - https://techearl.com/woocommerce-bulk-update-products-google-sheet
 */
function te_product_state( $p ) {
    return array(
        'regular_price' => $p->get_regular_price( 'edit' ),
        'sale_price' => $p->get_sale_price( 'edit' ),
        'sale_from' => $p->get_date_on_sale_from( 'edit' ) ? $p->get_date_on_sale_from( 'edit' )->getTimestamp() : null,
        'sale_to' => $p->get_date_on_sale_to( 'edit' ) ? $p->get_date_on_sale_to( 'edit' )->getTimestamp() : null,
        'manage_stock' => $p->get_manage_stock( 'edit' ),
        'quantity' => $p->get_stock_quantity( 'edit' ),
        'stock_status' => $p->get_stock_status( 'edit' ),
        'backorders' => $p->get_backorders( 'edit' ),
    );
}
function te_sheet_price( $value ) {
    if ( ! preg_match( '/^(?:0|[1-9][0-9]*)(?:\.[0-9]+)?$/D', $value ) ) {
        throw new RuntimeException( 'Use a nonnegative decimal price without currency symbols.' );
    }
    return wc_format_decimal( $value, wc_get_price_decimals() );
}
function te_sheet_date( $value, $end ) {
    if ( '' === $value ) { return null; }
    $date = DateTimeImmutable::createFromFormat( '!Y-m-d', $value, wp_timezone() );
    if ( ! $date || $date->format( 'Y-m-d' ) !== $value ) {
        throw new RuntimeException( 'Use an exact YYYY-MM-DD date.' );
    }
    return ( $end ? $date->setTime( 23, 59, 59 ) : $date )->getTimestamp();
}
function te_write_all( $handle, $text ) {
    $length = strlen( $text );
    $offset = 0;
    while ( $offset < $length ) {
        $written = fwrite( $handle, substr( $text, $offset ) );
        if ( false === $written || 0 === $written ) {
            throw new RuntimeException( 'Private file write failed.' );
        }
        $offset += $written;
    }
    if ( ! fflush( $handle ) || ! fsync( $handle ) ) {
        throw new RuntimeException( 'Private file flush failed.' );
    }
}
function te_private_file( $path ) {
    $dir = realpath( dirname( $path ) );
    if ( ! $dir || ! is_writable( $dir ) || ( fileperms( $dir ) & 0077 ) !== 0 ||
        '/' !== substr( $path, 0, 1 ) ) {
        throw new RuntimeException( 'Use an existing mode-0700 private directory and absolute filename.' );
    }
    $uploads = wp_get_upload_dir();
    foreach ( array( ABSPATH, WP_CONTENT_DIR, $uploads['basedir'] ) as $public ) {
        $public = realpath( $public );
        if ( $public && ( $dir === $public || 0 === strpos( $dir . '/', $public . '/' ) ) ) {
            throw new RuntimeException( 'Backup must be outside WordPress web, content and upload roots.' );
        }
    }
    $old_mask = umask( 0077 );
    $handle = fopen( $path, 'x' );
    umask( $old_mask );
    if ( ! $handle ) { throw new RuntimeException( 'Cannot create exclusive private file.' ); }
    return $handle;
}

try {
    if ( ! function_exists( 'wc_get_product' ) ) {
        throw new RuntimeException( 'WooCommerce is not active.' );
    }
    $file = $args[0] ?? '';
    $mode = $args[1] ?? 'dry-run';
    if ( ! in_array( $mode, array( 'dry-run', 'live' ), true ) ) {
        throw new RuntimeException( 'Mode must be dry-run or live.' );
    }
    $raw = file_get_contents( $file );
    if ( false === $raw ) { throw new RuntimeException( 'Cannot read input.' ); }
    $rows = json_decode( $raw, true, 512, JSON_THROW_ON_ERROR );
    if ( ! is_array( $rows ) || ! $rows || array_keys( $rows ) !== range( 0, count( $rows ) - 1 ) ) {
        throw new RuntimeException( 'Input must be a nonempty JSON list.' );
    }
    $headers = array( 'sku', 'regular_price', 'quantity', 'stock_status', 'sale_price', 'sale_from', 'sale_to' );
    $seen = array();
    $seen_ids = array();
    $plan = array();
    global $wpdb;
    foreach ( $rows as $row ) {
        if ( ! is_array( $row ) || array_keys( $row ) !== $headers ) {
            throw new RuntimeException( 'Unexpected row schema.' );
        }
        foreach ( $row as $cell ) {
            if ( ! is_string( $cell ) ) { throw new RuntimeException( 'All cells must be strings.' ); }
        }
        $sku = $row['sku'];
        if ( '' === $sku || isset( $seen[ $sku ] ) ) {
            throw new RuntimeException( 'Empty or duplicate sheet SKU.' );
        }
        $seen[ $sku ] = true;
        $matches = $wpdb->get_col( $wpdb->prepare(
            "SELECT DISTINCT p.ID FROM {$wpdb->posts} p
             JOIN {$wpdb->postmeta} m ON m.post_id = p.ID
             WHERE m.meta_key = '_sku' AND m.meta_value = %s
             AND p.post_type IN ('product', 'product_variation')", $sku
        ) );
        if ( 1 !== count( $matches ) ) {
            throw new RuntimeException( 'SKU must resolve uniquely: ' . $sku );
        }
        $p = wc_get_product( (int) $matches[0] );
        if ( ! $p || ! in_array( $p->get_type(), array( 'simple', 'variation' ), true ) ||
            'trash' === $p->get_status( 'edit' ) ) {
            throw new RuntimeException( 'Unsupported product type/status: ' . $sku );
        }
        $product_id = $p->get_id();
        if ( 'variation' === $p->get_type() && $p->get_stock_managed_by_id() !== $product_id ) {
            throw new RuntimeException( 'Inherited variation stock requires a separate ownership-aware updater: ' . $sku );
        }
        if ( isset( $seen_ids[ $product_id ] ) ) {
            throw new RuntimeException( 'Multiple rows resolve to the same product: ' . $sku );
        }
        $seen_ids[ $product_id ] = true;
        $before = te_product_state( $p );
        if ( '' !== $row['regular_price'] ) {
            $p->set_regular_price( te_sheet_price( $row['regular_price'] ) );
        }
        if ( '' !== $row['quantity'] ) {
            if ( '' !== $row['stock_status'] || true !== $p->get_manage_stock( 'edit' ) ||
                ! preg_match( '/^(?:0|[1-9][0-9]*)$/D', $row['quantity'] ) ||
                strlen( $row['quantity'] ) > 9 ) {
                throw new RuntimeException( 'Quantity needs owned managed stock and a nonnegative integer: ' . $sku );
            }
            $p->set_stock_quantity( (int) $row['quantity'] );
        }
        if ( '' !== $row['stock_status'] ) {
            if ( false !== $p->get_manage_stock( 'edit' ) ||
                ! in_array( $row['stock_status'], array( 'instock', 'outofstock', 'onbackorder' ), true ) ) {
                throw new RuntimeException( 'Status-only updates require unmanaged stock: ' . $sku );
            }
            $p->set_stock_status( $row['stock_status'] );
        }
        if ( 'CLEAR' === $row['sale_price'] ) {
            if ( '' !== $row['sale_from'] || '' !== $row['sale_to'] ) {
                throw new RuntimeException( 'CLEAR must have blank dates.' );
            }
            $p->set_sale_price( '' );
            $p->set_date_on_sale_from( null );
            $p->set_date_on_sale_to( null );
        } elseif ( '' !== $row['sale_price'] ) {
            $sale = te_sheet_price( $row['sale_price'] );
            if ( '' === $p->get_regular_price( 'edit' ) ||
                (float) $sale >= (float) $p->get_regular_price( 'edit' ) ) {
                throw new RuntimeException( 'Sale must be below a defined regular price: ' . $sku );
            }
            $from = te_sheet_date( $row['sale_from'], false );
            $to = te_sheet_date( $row['sale_to'], true );
            if ( null !== $from && null !== $to && $from > $to ) {
                throw new RuntimeException( 'Sale starts after it ends: ' . $sku );
            }
            $p->set_sale_price( $sale );
            $p->set_date_on_sale_from( $from );
            $p->set_date_on_sale_to( $to );
        } elseif ( '' !== $row['sale_from'] || '' !== $row['sale_to'] ) {
            throw new RuntimeException( 'Dates require a sale price: ' . $sku );
        }
        $p->validate_props();
        $after = te_product_state( $p );
        if ( $before !== $after ) {
            $plan[] = array( 'id' => $p->get_id(), 'sku' => $sku, 'before' => $before, 'after' => $after );
        }
    }
    foreach ( $plan as $item ) {
        WP_CLI::log( wp_json_encode( $item ) );
    }
    if ( 'live' !== $mode || ! $plan ) {
        WP_CLI::success( count( $plan ) . ' planned changes; no writes.' );
        return;
    }
    $backup_path = $args[2] ?? '';
    if ( '' === $backup_path ) { throw new RuntimeException( 'Live mode requires a new private backup path.' ); }
    $backup = te_private_file( $backup_path );
    te_write_all( $backup, json_encode( array(
        'site' => home_url(), 'input_sha256' => hash( 'sha256', $raw ), 'plan' => $plan,
    ), JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT ) . "\n" );
    fclose( $backup );
    $journal = te_private_file( $backup_path . '.results.jsonl' );
    foreach ( $plan as $item ) {
        $p = wc_get_product( $item['id'] );
        if ( ! $p || te_product_state( $p ) !== $item['before'] ) {
            throw new RuntimeException( 'Product changed since planning; stopped: ' . $item['sku'] );
        }
        $want = $item['after'];
        $p->set_regular_price( $want['regular_price'] );
        $p->set_sale_price( $want['sale_price'] );
        $p->set_date_on_sale_from( $want['sale_from'] );
        $p->set_date_on_sale_to( $want['sale_to'] );
        $p->set_stock_quantity( $want['quantity'] );
        $p->set_stock_status( $want['stock_status'] );
        $saved = $p->save();
        $readback = $saved ? wc_get_product( $p->get_id() ) : false;
        $ok = $readback && te_product_state( $readback ) === $want;
        te_write_all( $journal, json_encode( array(
            'id' => $item['id'], 'sku' => $item['sku'], 'verified' => $ok,
        ), JSON_THROW_ON_ERROR ) . "\n" );
        if ( ! $ok ) { throw new RuntimeException( 'Write verification failed; stopped: ' . $item['sku'] ); }
    }
    fclose( $journal );
    WP_CLI::success( count( $plan ) . ' verified product writes.' );
} catch ( Throwable $error ) {
    WP_CLI::error( $error->getMessage() . ' A live run can be partially applied; inspect the backup and result journal before retrying.' );
}

The SKU query is a read-only uniqueness check for WooCommerce's current WordPress product storage. Actual mutations use CRUD. If an extension replaces product storage or SKU resolution, adapt that lookup to its supported API and retest rather than assuming this SQL is portable.

Run a preview, then the live operation

bash
wp eval-file /srv/private/apply-products.php /srv/private/products-review.json
wp eval-file /srv/private/apply-products.php /srv/private/products-review.json \
  live /srv/private/product-backups/run-2026-10-01.json

Use an operator-only directory with mode 0700 that is not exposed by any web-server alias. The script rejects paths beneath the WordPress/content roots, but it cannot discover every server alias. Keep a database backup as well as the per-product snapshot. Practice restoring the affected fields through CRUD on staging before the first live run. Do not blindly restore old stock after new orders have arrived.

The snapshot records all planned before/after values before the first mutation. The result journal records verified outcomes; a failed write stops the batch. This is recoverable evidence, not an automatic rollback or a guarantee of atomicity across products.

Supplier stock and managed inventory

Managed quantity and independent stock status are different models. When stock is managed, WooCommerce validates status using quantity, backorders and its configured out-of-stock threshold. Setting instock manually does not necessarily override those rules on save.

With status-only stock, quantity is not the authority. The updater rejects mixing quantity and status in a row. It preserves the existing backorder policy rather than silently changing whether customers may buy unavailable items.

A variable parent can manage a shared stock pool, or variations can own their stock. The combined updater accepts simple products and variations with unambiguous ownership; it rejects variable parents and inherited stock. For a shared parent-stock integration, design a stock-only update against that parent and test how every child inherits it. A variation SKU is not permission to split the shared pool.

Scheduled sale prices and date behavior

A date-only value passed straight to the generic sale-date setter is not an inclusive end-of-day instruction. This updater parses sale_from at local midnight and sale_to at 23:59:59 in wp_timezone(), then supplies timestamps. Use a named store timezone when daylight-saving rules matter.

WooCommerce's sale scheduling and product data store handle price activation. The site's scheduled-task system must actually run; the stored date is not a promise of a callback executing at the exact second on an idle or broken scheduler. Verify the active storefront price around both boundaries on staging, including a daylight-saving transition if relevant.

For percentage discounts, validate a percentage between 0 and 100 and convert against the regular price using the store's currency precision. Do not assume every currency uses two decimal places or accept a localized string such as $1,299.00 as a machine price.

Invalid rows, partial failures and verification

Before use, test duplicate and unknown SKUs, a variation inheriting stock, blank versus zero values, invalid dates such as February 30, a sale ending before it starts, a decimal using commas, a read-only backup directory and an existing backup filename. None should begin product writes.

Then test valid simple products and variations, a zero-stock transition with and without backorders, CLEAR, a scheduled sale and a repeat run. Compare product admin values, storefront prices, stock behavior and lookup-driven sorting. If a plugin modifies values on save, the read-back mismatch should stop the operation; investigate rather than suppressing it.

For scheduling, download a new uniquely named input, run the same validation, retain the snapshot/journal and prevent overlapping jobs with an OS lock. Do not repeatedly import an old sale schedule after WooCommerce has expired and cleared it. A large or continuously changing inventory needs pagination, reservations-aware reconciliation and explicit source ownership beyond this example. The safe batch-update guide and two-way sheet sync rules cover the surrounding operational decisions.

Bulk-create products from a CSV

The updater above treats an unknown SKU as an error. Creating a catalog is the inverse job: a missing SKU means a new product. The same two rules carry over, resolve by SKU and write through the CRUD, and the command below is idempotent: wc_get_product_id_by_sku() decides whether each row updates an existing product or creates a new one, so re-running the same file never duplicates.

When the core CSV importer is not enough

The built-in importer (Products, Import) handles variations, image URLs and most standard columns. For a one-off load of a clean catalog of simple products, use it. A script earns its place when you need:

  • Idempotent re-runs. The importer can update by ID or SKU, but it is a manual click-through. A script keyed on SKU can run on a schedule and converge: new rows create, existing rows update.
  • Computed fields. A retail price of cost * 1.4 rounded to .99, a SKU assembled from a brand code and a model number. The importer maps columns one to one.
  • Conditional category logic. "If the type is hoodie, file it under Apparel > Hoodies." Branching belongs in code.
  • Custom meta. A supplier ID, an ERP key, an ACF field, written explicitly through the CRUD.
  • A live feed a cron should ingest unattended.

The source rows

One row per product, with sku as the idempotency key:

skunamepricecategorydescription
TE-HOODIE-001TechEarl Hoodie49.99Apparel > HoodiesHeavyweight cotton hoodie.
TE-MUG-002TechEarl Mug14.99Drinkware11oz ceramic mug.
TE-CAP-003TechEarl Cap24.99Apparel > CapsSix-panel cotton cap.

The creation logic only needs an array of associative rows. For a private sheet, adapt the service-account reader above to these headers; for a file on disk, a plain CSV reader is the lowest-friction start. All the PHP below goes in one plugin file (wp-content/plugins/te-products/te-products.php), top to bottom:

php
/** Read a CSV into an array of associative rows keyed by the header. */
function te_read_csv( $path ) {
	$fh   = fopen( $path, 'r' );
	$head = fgetcsv( $fh );
	$rows = array();
	while ( ( $line = fgetcsv( $fh ) ) !== false ) {
		$rows[] = array_combine( $head, $line );
	}
	fclose( $fh );
	return $rows;
}

One safety note: a cell that starts with =, +, - or @ is a formula. WooCommerce escapes product names and descriptions on output, so the storefront is fine, but any field you might round-trip back into a spreadsheet can execute in the viewer's Excel or Sheets (CSV or formula injection). Neutralise it with a leading apostrophe, or strip a leading =+-@, before re-export.

Upsert by SKU through the CRUD

WC_Product_Simple and its setters have been the supported path since WooCommerce 3.0 (April 2017). The SKU lookup at the top is the whole idempotency story:

php
/**
 * Create or update one product from a source row. Returns ['action' => ..., 'id' => ...].
 * Idempotent: the same SKU updates the same product, never duplicates.
 */
function te_upsert_product( array $row ) {
	$sku = trim( $row['sku'] ?? '' );
	$name = trim( $row['name'] ?? '' );

	// Validate the required fields before touching anything.
	if ( '' === $sku || '' === $name ) {
		return array( 'action' => 'SKIP (missing sku/name)', 'id' => 0, 'sku' => $sku );
	}

	$existing_id = wc_get_product_id_by_sku( $sku );
	$product     = $existing_id ? wc_get_product( $existing_id ) : new WC_Product_Simple();
	$action      = $existing_id ? 'update' : 'create';

	$product->set_name( $name );
	$product->set_sku( $sku );
	$product->set_regular_price( (string) $row['price'] );      // string, e.g. '49.99'
	$product->set_description( $row['description'] ?? '' );
	$product->set_status( 'publish' );

	// Categories: resolve (and create) the terms, then assign by ID.
	if ( ! empty( $row['category'] ) ) {
		$product->set_category_ids( te_resolve_category_ids( $row['category'] ) );
	}

	// A custom meta field (supplier ID, ERP key, anything). Written via the CRUD.
	if ( ! empty( $row['supplier_id'] ) ) {
		$product->update_meta_data( '_te_supplier_id', $row['supplier_id'] );
	}

	$id = $product->save();
	return array( 'action' => $action, 'id' => $id, 'sku' => $sku );
}

Prices go in as strings, which is what the setters expect and avoids float-formatting surprises. set_status('publish') makes a new product live; use 'draft' to stage and review. save() persists the object, updates the lookup tables and clears caches, the same reason the updater never writes _price directly.

Resolve category paths and build attributes

The sheet carries a readable path like Apparel > Hoodies; WooCommerce wants term IDs. This resolver walks the path, creates any missing term under its parent, and returns the leaf ID:

php
/** Turn "Apparel > Hoodies" into the leaf category term ID, creating terms as needed. */
function te_resolve_category_ids( $path ) {
	$parent = 0;
	$leaf   = 0;
	foreach ( array_map( 'trim', explode( '>', $path ) ) as $name ) {
		if ( '' === $name ) { continue; }
		$term = term_exists( $name, 'product_cat', $parent );
		if ( ! $term ) {
			$term = wp_insert_term( $name, 'product_cat', array( 'parent' => $parent ) );
		}
		if ( is_wp_error( $term ) ) { break; }
		$parent = $leaf = (int) $term['term_id'];
	}
	return $leaf ? array( $leaf ) : array();
}

Outside the CRUD flow, wp_set_object_terms( $product_id, $term_ids, 'product_cat' ) does the same job; inside the upsert, set_category_ids batches into the one save().

Attributes such as Size or Color are WC_Product_Attribute objects, not strings. This builds a custom (per-product) attribute, the common case for a flat import:

php
/** Build a custom (non-taxonomy) product attribute from a name and a list of values. */
function te_build_attribute( $name, array $values ) {
	$attr = new WC_Product_Attribute();
	$attr->set_name( $name );          // e.g. 'Size'
	$attr->set_options( $values );     // e.g. ['S', 'M', 'L']
	$attr->set_visible( true );        // show in the Additional Information tab
	$attr->set_variation( false );     // simple product, not a variable one
	return $attr;
}

// Inside te_upsert_product, before save(), if the row carries a 'sizes' column:
// $product->set_attributes( array( te_build_attribute( 'Size', explode( '|', $row['sizes'] ) ) ) );

set_variation(false) matters: these attributes describe a simple product and do not drive variations. Real variable products need WC_Product_Variable plus WC_Product_Variation children, which is a bigger job than this importer.

A dry-run WP-CLI import command

Nothing writes unless you pass --live. Back up the database before the first live run.

php
<?php
/**
 * Plugin Name: TE Product Importer
 * Plugin URI:  https://techearl.com/woocommerce-bulk-update-products-google-sheet
 * Description: WP-CLI command that creates or updates WooCommerce products from a CSV or sheet, idempotent by SKU.
 * Author:      Ishan Karunaratne
 * Author URI:  https://techearl.com
 */

defined( 'ABSPATH' ) || exit;
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { return; }

class TE_Product_Import_Command {

	/**
	 * Create or update products from a CSV. Idempotent by SKU.
	 *
	 * ## OPTIONS
	 * --file=<path> : Path to the CSV (sku,name,price,category,description).
	 * [--live]      : Actually write. Without it, dry run.
	 */
	public function import( $args, $assoc ) {
		if ( ! function_exists( 'wc_get_product_id_by_sku' ) ) {
			WP_CLI::error( 'WooCommerce is not active.' );
		}
		$rows = te_read_csv( $assoc['file'] );
		$live = isset( $assoc['live'] );

		$report = array();
		foreach ( $rows as $row ) {
			$sku  = trim( $row['sku'] ?? '' );
			$name = trim( $row['name'] ?? '' );
			if ( '' === $sku || '' === $name ) {
				$report[] = array( 'sku' => $sku, 'action' => 'SKIP (missing sku/name)', 'id' => '' );
				continue;
			}
			$exists = wc_get_product_id_by_sku( $sku );
			$action = $exists ? 'update' : 'create';

			if ( $live ) {
				$result = te_upsert_product( $row );
				$report[] = array( 'sku' => $sku, 'action' => $result['action'], 'id' => $result['id'] );
			} else {
				$report[] = array( 'sku' => $sku, 'action' => "WOULD {$action}", 'id' => $exists ?: '' );
			}
		}

		WP_CLI\Utils\format_items( 'table', $report, array( 'sku', 'action', 'id' ) );
		$verb = $live ? 'applied' : 'previewed (dry run)';
		WP_CLI::success( sprintf( '%d rows %s.', count( $report ), $verb ) );
	}
}
WP_CLI::add_command( 'te-products', 'TE_Product_Import_Command' );

Dry run first, every time:

bash
wp te-products import --file=products.csv
A terminal running wp te-products import as a dry run, showing a table of source rows resolved by SKU: new SKUs marked WOULD create, an already-existing SKU marked WOULD update, and a row missing a name marked SKIP, ending with a count of rows previewed
The dry run resolves every row by SKU before writing anything: new SKUs would create, existing SKUs would update, and a row missing a required field is skipped.

New SKUs show WOULD create, existing ones WOULD update with the product ID, and rows missing a required field show SKIP. When the preview looks right:

bash
wp te-products import --file=products.csv --live

Re-run the same file and every row flips to update, because each SKU now resolves to a real product. The file is a desired-state document, which is what makes a nightly cron running wp te-products import --file=/path/to/feed.csv --live safe: new feed rows create products, changed rows update them, and nothing duplicates.

For a clean one-off load of simple products, do use it; it handles most columns. A script earns its place when you need idempotent re-runs keyed on SKU, computed fields, conditional category logic, custom meta the importer does not map, or a feed a cron should ingest unattended.

It resolves the SKU first. wc_get_product_id_by_sku($sku) returns an existing ID, which the script loads and updates, or 0, in which case it creates a new product. The same row maps to the same product on every run.

Because the active price, lookup records and caches depend on more than one meta value. The WC_Product setters plus save() keep them consistent; direct meta writes bypass the product data store and can leave lookup data and caches inconsistent.

Turn the category path (Apparel > Hoodies) into term IDs with term_exists / wp_insert_term, then assign with set_category_ids. For attributes, build a WC_Product_Attribute per attribute with set_name, set_options and set_visible, and pass the array to set_attributes before save().

Sources

Authoritative references this article was fact-checked against.

TagsWordPressWooCommerceGoogle SheetsWP-CLIService AccountPHPCSV

Found this useful? Pass it on.

Copied

Ishan Karunaratne

Systems and Network Architect · Chief Technology Officer

Systems and network architect and Chief Technology Officer with more than two decades designing, building, and running production software, cloud and network architecture, Linux systems, and the bare metal underneath them, and lately working AI into the stack. A US Army veteran who served in Operation Iraqi Freedom. What I write here is drawn from the full arc of that work, across architecture, engineering, and operations, not any single job.

Keep reading

Related posts

Update WordPress from a Google Sheet: an Apps Script push to a REST endpoint, a service-account pull, two-way sync, and recipes for fields, SEO and images.

Update WordPress From a Google Sheet With a REST Endpoint

Edit posts in a Google Sheet and have the changes land in WordPress: a secret-gated MU-plugin REST endpoint with an Apps Script push, a service-account pull over WP-CLI, onEdit and two-way sync, a multi-site controller, and recipes for custom fields, SEO meta, directories, menus, events and featured images.

Managed WordPress vs VPS for agencies: decision tree, break-even math, when to self-host, profit per client, and honest takes on Kinsta, WP Engine, Cloudways and more.

Managed WordPress Hosting vs VPS for Agencies

Managed WordPress hosting vs VPS for agencies, in one guide: a decision tree by traffic and workload, the break-even math, when self-hosting pays, how hosting drives agency profit, and honest takes on Kinsta, Rocket.net, Cloudways, WP Engine and Bluehost.