wc_create_variation: create a variation
Adds a variation to a variable product.
Arguments
| Argument | Required | Meaning |
|---|---|---|
product_id |
Yes | The parent product |
attributes |
No | The attribute combination this variation represents |
sku |
No | Stock keeping unit |
regular_price |
No | Normal price |
sale_price |
No | Sale price |
description |
No | Variation description |
stock_quantity |
No | Stock level |
dry_run |
No | Report without creating |
Permissions
Allow WooCommerce catalog writes open, plus manage_woocommerce or edit_products.
The parent must be ready first
A variation only works if its parent is a variable product whose attributes are marked as used for variations.
Before creating any:
wc_get_product— confirmtypeisvariable.wc_get_product_attributesandwc_get_attribute_terms— see what exists.wc_set_product_attributes— attach them, flagged for variations.
Skipping this produces variations that exist in the database and never appear in the product's selector, because their attribute combination does not correspond to anything the product offers.
Nothing warns you. The creation succeeds.
attributes defines which combination this is
The map identifies the variation — a size and a colour, say. It must use the attribute names and term slugs the parent product actually declares.
An empty or mismatched attributes produces a variation customers cannot select. That is the single most common failure here, and it is why reading the parent's attributes first is not optional.
Give it a price
A variation without a price is not purchasable. WooCommerce shows the product but the customer cannot complete a selection.
Set regular_price on every variation you create. Set sale_price only if it should launch discounted.
Stock depends on the parent
stock_quantity sets a number, but whether WooCommerce uses it depends on stock management being enabled on the parent product. Setting a quantity on a product that does not manage stock stores a value nothing acts on.
Decide the stock model at the product level first.
Create one, check, then the rest
Variations are created individually, so a product with twelve combinations is twelve calls — and twelve chances to repeat the same mistake.
Create the first variation, then show me the product page so I can select it. Stop there.
If the first variation is selectable on the storefront, the attribute mapping is right and the rest will follow. If it is not, you have one to fix rather than twelve.
The dry run
Returns the payload it would create, without creating it. Useful for confirming the attribute map before the first one.
SKUs must be unique
Across the whole catalogue, not just within the product. A batch of variations sharing a SKU pattern will fail on the duplicates.
After creating
- Read the variations back.
- Open the product page on the storefront.
- Select the new combination and confirm the price and availability shown.
- Check the product's displayed price range updated.
- Purge the cache.
Step 3 is the one that proves it worked. The stored data being right does not guarantee the selector behaves.
Common problems
| Symptom | Cause |
|---|---|
| The write is refused | Catalog write gate closed |
pom_ai_mcp_wc_forbidden |
Missing shop capability |
| The variation is not selectable | Its attributes do not match the parent's |
| Customers cannot buy it | No price set |
| Stock is ignored | Stock management is off on the parent |
| Creation failed | Duplicate SKU |
| The price range did not change | Cache not purged |
Related
wc_set_product_attributes— do this firstwc_update_variation- Manage product variations