Lesson 1.2. CRUD and Data Stores

Thursday, 4:30 PM

You updated prices for 300 products with a script: update_post_meta( $id, '_price', $new ). The admin shows new prices. The storefront shows old ones. The price filter says one thing, the cart charges another.

Nothing broke. You simply bypassed the layer that was supposed to update the cache and the lookup table and fire other plugins' hooks.

Goal

After the lesson you change products and orders only through CRUD and can explain what exactly breaks when code writes to the database directly.

Key concepts

  • WC_Data — the base class for WC_Product, WC_Order, WC_Customer, WC_Coupon
  • Getters / setters: get_price(), set_regular_price(); changes apply on save()
  • Meta: get_meta(), update_meta_data(), delete_meta_data() + save()
  • Data store — a class that knows where and how to store an object (posts, HPOS, custom tables)
  • wc_get_product(), wc_get_order(), wc_get_products(), wc_get_orders() — the entry points
  • Hooks woocommerce_before_{object}_object_save / woocommerce_after_{object}_object_save

Diagram

flowchart LR
  CODE["Your code"] -->|"set_*(), save()"| OBJ["WC_Order / WC_Product"]
  OBJ --> DS{"Data store"}
  DS -->|HPOS on| HPOS[("wc_orders ...")]
  DS -->|HPOS off| POSTS[("wp_posts + wp_postmeta")]
  DS --> CACHE["Cache, lookup tables,<br/>hooks, invalidation"]

Code

Right: changes through CRUD.

PHP
<?php
$product = wc_get_product( $product_id );
if ( ! $product ) {
	return;
}
$product->set_regular_price( '499' );
$product->set_sale_price( '399' );
$product->update_meta_data( '_supplier_code', 'SUP-1042' );
$product->save(); // updates the lookup table and cache, fires hooks

$order = wc_get_order( $order_id );
$order->update_meta_data( '_erp_id', 'ERP-778' );
$order->add_order_note( 'Synced with ERP' );
$order->save();

Wrong: bypassing CRUD.

PHP
<?php
update_post_meta( $product_id, '_price', 399 );   // lookup table and cache are now stale
update_post_meta( $order_id, '_erp_id', 'ERP-778' ); // with HPOS it never reaches wc_orders_meta

Common mistakes

SymptomCauseFix
The price changed in the admin, but the filter shows the old oneupdate_post_meta( _price ) bypasses CRUDset_regular_price() + save()
Order meta "disappears" after HPOS is enabledCode writes via update_post_meta()$order->update_meta_data() + save()
Changes weren't savedForgot save()Setters only change the object in memory
Infinite loopsave() inside woocommerce_after_product_object_saveRemove the hook before save() or make changes in before_*

Weak and strong answer

Question: How do you change a product's price programmatically?

Weak answer. update_post_meta( $id, '_price', 100 ).

Strong answer. Through CRUD: $product = wc_get_product( $id ); $product->set_regular_price( '100' ); $product->save();. save() goes through the data store: it updates the lookup table and cache and fires hooks. For a variable product the price is set on each variation.

Why the second is stronger: The candidate explains what exactly breaks when CRUD is bypassed and mentions the variations nuance.

Practice

The client asks: "10% off HOODIE-BLUE, and I want to see when the price changed." In the IDE below, write a function that finds the product by SKU (wc_get_product_id_by_sku()), sets the price with a 10% discount and adds a _last_price_update meta field with the date. Verify it with Check, then Run.

TEXT

Interview Questions

A class responsible for saving and reading a specific object type. The CRUD object doesn't know where its data lives. That's why HPOS could be introduced without changing the public API: only the order data store changed.

It takes parameters in WooCommerce terms (price, stock_status, sku, visibility), builds the query itself and returns WC_Product objects. The code doesn't depend on how the data is laid out in tables.

The woocommerce_after_order_object_save hook — it fires no matter where the order was saved from: admin, REST, checkout, CLI.

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.