Lesson 7.3. ERP/CRM Sync

Goal

Design a reliable two-way sync between the store and an ERP or CRM.

Key concepts

  • A source of truth for every field: stock and prices — the ERP, orders — the store
  • The matching key is the SKU (wc_get_product_id_by_sku()), not the ID
  • Upsert: found — update, not found — create; a repeat doesn't create duplicates
  • Incremental sync by modified_after instead of a full one every time
  • Errors: retries with backoff, "dead" jobs for manual review, logs through wc_get_logger()
  • Loop protection: a change from the ERP must not be sent back to the ERP

Diagram

flowchart LR
  ERP[("ERP")] -->|"stock, prices<br/>every 15 min"| Q["Action Scheduler<br/>group erp"]
  Q --> U["upsert by SKU<br/>CRUD + save()"]
  U --> WC[("WooCommerce")]
  WC -->|"order processing"| Q2["push_order job"]
  Q2 --> ERP
  U -. "_from_erp flag" .-> STOP["don't send back"]

Code

An idempotent stock update by SKU:

PHP
<?php
add_action( 'my_stock_update', static function ( string $sku, int $qty ) {
	$logger  = wc_get_logger();
	$context = [ 'source' => 'erp-sync' ];

	$product_id = wc_get_product_id_by_sku( $sku );
	if ( ! $product_id ) {
		$logger->warning( "SKU {$sku} not found", $context );
		return;
	}
	$product = wc_get_product( $product_id );
	if ( $product->get_stock_quantity() === $qty ) {
		return; // nothing changed — don't touch the database or the hooks
	}
	$product->set_manage_stock( true );
	$product->set_stock_quantity( $qty );
	$product->update_meta_data( '_from_erp', time() );
	$product->save();
	$logger->info( "SKU {$sku}: {$qty}", $context );
}, 10, 2 );

Common mistakes

SymptomCauseFix
Duplicate products after a syncMatching by name, or by an ERP ID that isn't storedThe SKU or a stored external ID in meta
An endless ERP ↔ store exchangeA change from the ERP triggers the hook that sends to the ERPMark the source of changes and filter
The sync fails on 5,000 productsEverything in one requestBatches and separate jobs in the queue

Weak and strong answer

Question: How do you sync 20,000 products with an ERP?

Weak answer. Write a script that goes through all products once a day.

Strong answer. Incrementally: the ERP returns changes since the last sync. Every change is a separate idempotent Action Scheduler job: an upsert by SKU through CRUD, with no write if nothing changed. Retries and the error log come from the queue. A full sync only as a nightly reconciliation. The source of truth for every field is defined up front.

Why the second is stronger: An architecture with a queue, idempotency and a clear source of truth.

Practice

Describe the sync design for a store with 20,000 products and an ERP with an API of 200 items per page: the source of truth for every field, the schedule, the job format, error handling. In the terminal run the nightly jobs, find the failed one, run my_stock_update on 3 SKUs (repeat one of them) and read the log.

curl / wp — Module 7. APIs and Integrations

Interview Questions

An "update if it exists, otherwise create" operation by a stable key. Running it again doesn't create duplicates — the sync can be safely restarted.

wc_get_logger() with your own source. The logs are in WooCommerce → Status → Logs, with levels from debug to emergency.

Mark changes coming from the external system (a meta or a runtime flag) and don't send them back. Or define a single source of truth for every field.

Save your progress

Sign in or create a free account to mark lessons as completed and continue the whole course. You will come back to this lesson.