Saturday, 10:30 AM
The "Hoodies" category page takes 8 seconds to load. It shows 24 products.
Under each card the theme draws color swatches, and to do that it calls get_available_variations(). Every hoodie has 40 variations: size × color.
That's 960 variation objects per page — just to draw 24 rows of swatches.
Goal
After the lesson you know how simple and variable products, attributes and variations are built, and you don't load variations where a price range is enough.
Key concepts
- Types: simple, variable, grouped, external; the type is the
product_typetaxonomy - Variable — a parent
product+ childproduct_variationposts, each with its own price, SKU and stock - Global attributes —
pa_colortaxonomies; local ones are stored in the product itself - An attribute "used for variations" vs "visible only"
- A variable product's price is a min–max range cached in a transient
Diagram
flowchart TB
P["product #40<br/>Hoodie (variable)"] --> A1["attribute pa_size: S, M, L"]
P --> A2["attribute pa_color: black, grey"]
P --> V1["product_variation #41<br/>S / black · 1200 · 5 pcs"]
P --> V2["product_variation #42<br/>M / black · 1200 · 0 pcs"]
P --> V3["product_variation #43<br/>L / grey · 1300 · 2 pcs"]
Code
Create a variable product through CRUD:
<?php
$attribute = new WC_Product_Attribute();
$attribute->set_id( wc_attribute_taxonomy_id_by_name( 'pa_size' ) );
$attribute->set_name( 'pa_size' );
$attribute->set_options( [ 'S', 'M' ] );
$attribute->set_variation( true );
$attribute->set_visible( true );
$product = new WC_Product_Variable();
$product->set_name( '4WP Hoodie' );
$product->set_attributes( [ $attribute ] );
$parent_id = $product->save();
foreach ( [ 'S' => '1200', 'M' => '1250' ] as $size => $price ) {
$variation = new WC_Product_Variation();
$variation->set_parent_id( $parent_id );
$variation->set_attributes( [ 'pa_size' => sanitize_title( $size ) ] );
$variation->set_regular_price( $price );
$variation->set_manage_stock( true );
$variation->set_stock_quantity( 10 );
$variation->save();
}
Common mistakes
| Symptom | Cause | Fix |
|---|---|---|
| The category page is slow | get_available_variations() in the catalog loop | Price range via get_variation_prices(), variations only on the product page |
| A variable product's price didn't update | The variation price cache (transient) | Save variations through CRUD — the cache is cleared; for dynamic prices see module 10 |
| A variation is missing from the form | No price, or the attribute isn't marked "used for variations" | Set a price and set_variation( true ) |
Weak and strong answer
Question: How is a variable product built?
Weak answer. It's a product with several options, like size.
Strong answer. A parent
productpost of type variable with attributes marked for variations. Each variation is a separateproduct_variationpost with its own price, SKU, stock and image. The parent's price is a range WooCommerce calculates and caches. That's why the number of variations affects catalog performance.
Why the second is stronger: The candidate describes the data model and its consequences.
Practice
Create a variable product with WP-CLI: two attributes (size, color) and four variations. Leave one variation out of stock and see how it shows on the product page.
Interview Questions
A global attribute is a pa_* taxonomy shared by all products: you can filter the catalog by it. A local one lives only in one product and has no filters.
Hundreds are technically possible, but the product form and price calculation slow down. If there are thousands of combinations, rethink the model: separate products, add-ons or options instead of variations.
$product->get_variation_price( 'min' ) and ( 'max' ), or get_price_html(). Don't loop over all variations by hand.