Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 40 ยท Catalog and Products

Variants and Options

Model product options and variants: option axes, generating the variant matrix, avoiding combinatorial explosion, per-variant price and stock, and URL handling.

A t-shirt comes in three sizes and four colours. Is that one product or twelve? To the customer it is one shirt on one page. To the warehouse it is twelve different things on twelve shelf labels, each with its own stock count. The solution used by almost every platform is one product with options, and one variant per valid combination.

This lesson shows how to model options and variants, how to generate the variant matrix, why the number of variants can explode, which fields belong to the variant, and how to make variant selection work with URLs. It builds on Product Catalogs and Data Modeling.

Options, values and variants

An option (also called an axis or attribute) is a dimension a customer chooses along: Size, Colour, Material. Each option has a list of values: S, M, L. A variant is one combination that picks exactly one value per option: Size M plus Colour Red.

flowchart TB
    P["Product: Classic T-shirt"] --> O1["Option: Size<br/>S, M, L"]
    P --> O2["Option: Colour<br/>Red, Blue"]
    P --> V["Variants<br/>S/Red, S/Blue, M/Red,<br/>M/Blue, L/Red, L/Blue"]

Two options with 3 and 2 values produce 6 variants.

The product page shows the options as selectors. The cart and the order, however, always store the variant, because that is the item that has a SKU, a price and a stock level.

Generating the matrix

The set of all combinations is the Cartesian product of the option values. You can generate it with a few lines of code. Each generated variant gets a derived SKU, which merchants can then edit.

JS Generate all variants

Press Run (or Ctrl + Enter) to see the console output here.

No output.

When a merchant later adds a new value, such as a size XL, the system should create only the missing variants, never recreate or delete existing ones, because those already have stock, orders and URLs attached.

Combinatorial explosion

Variants multiply. Three options with 6, 12 and 8 values give 576 variants for a single product. Add a fourth option and the count can reach tens of thousands, which makes the admin unusable, slows pages and bloats the database. Many platforms cap options (often at three) and variants per product for this reason.

JS How fast variants grow

Press Run (or Ctrl + Enter) to see the console output here.

No output.

You can keep the number under control with a few design choices:

  • Do not model free-form choices as options. A text engraving, an uploaded photo or a gift message is a custom field (a "line item property") on the cart line. It costs the customer a text box, not a variant.
  • Split genuinely different products. If red and blue differ in price, photos and description, two products may be clearer than one.
  • Skip impossible combinations. If a design only exists in blue and green, do not create red variants. Generation should allow deselecting combinations.
  • Use add-ons for accessories. "Add a gift box" can be a separate product the customer adds to the cart.

What belongs to the variant

Some data is shared by the whole product; some is specific to one variant. Getting this split right is the heart of the model.

Product level Variant level
Title and description SKU and barcode
Brand, category, tags Price and compare-at price
Shared images Variant-specific image
Option definitions Stock level, per location
SEO fields Weight and dimensions (they can differ)

Per-variant price is common: XXL costs more than S, or a 1 TB drive costs more than a 256 GB one. Per-variant image matters: when the customer selects "Red", the gallery should show the red photo. Per-variant stock matters most of all: "M / Red" can sell out while "M / Blue" has plenty.

Show the customer sensible information. If all variants cost the same, display one price; if they differ, a "From 19.00" display is typical until a variant is chosen. If a combination is sold out, mark it unavailable instead of hiding it silently, so the customer understands what happened.

Default variant and selection

The page needs something to display before the customer clicks. There are two common approaches:

  • Preselect a default variant, usually the best-selling or first available one. The page is complete on load, the price is concrete, and add-to-cart works at once.
  • Require a choice, leaving options unselected with a disabled button until all are chosen. This avoids a customer buying the preselected size by accident but adds friction.

For products with a single variant, hide the selector entirely. The platform may still have an internal "Default Title" option for it, and that label should never appear in the interface.

Variant URLs

How should the address look? A good pattern is one canonical product URL with the selected variant in the query string:

TEXT
/products/classic-tee
/products/classic-tee?size=M&colour=Blue
/products/classic-tee?variant=1042

With this pattern:

  • A shared link opens the page with the same variant preselected.
  • The server can render the correct image and price, which is good for users and for crawlers.
  • Search engines are told (with a canonical tag) that all variants belong to the one main page, so you do not split ranking across dozens of near-identical pages. The SEO lesson covers when a variant deserves its own indexable page.
JS Resolving a variant from a URL

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice the rules in resolve: an invalid combination in the URL never crashes the page, it falls back to a sensible default. Treat every URL parameter as untrusted input; the server must also check on add-to-cart that the variant exists, is for sale and is in stock. Never trust a price sent along with it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a cart line or order line reference?

    The variant carries the SKU, price and stock, so it is what is bought. The product is the page that presents it.
  2. 2. A product has options with 4, 5 and 6 values. How many variants does the full matrix contain?

    The Cartesian product multiplies the value counts: 4 x 5 x 6 = 120.
  3. 3. How should a customer's engraving text be modeled?

    Free-form input would create unlimited variants. A line item property stores the text without multiplying variants.
  4. 4. When a merchant adds the size XL to an existing product, what should the system do?

    Existing variants already have stock, orders and URLs. Only the new combinations should be added.

Summary

  • A product has options; a variant is one combination of option values and is what customers actually buy.
  • The variant matrix is the Cartesian product of option values, and it multiplies quickly.
  • Limit explosion by using custom fields for free input, splitting products and skipping impossible combinations.
  • SKU, price, image, stock and often weight are variant-level data; title, brand and options are product-level.
  • Use one canonical product URL with the selected variant in the query string, and always validate it on the server.

Next: Product Pages, Media and Content, where the catalog becomes a page people want to buy from.