# Solution for the Variation Selector


> A typed selection exercise with explicit availability feedback and no implied order creation or payment.


# Solution for the variation selector

**src/components/VariationExercise.tsx**

```tsx
import { defineProduct, useWpProductVariations, useWpCurrency } from "@forgewp/woocommerce"

const product = defineProduct({
  id: 201, name: "Cotton shirt", type: "variable", price: "30.00",
  attributes: [{ name: "size", options: ["Small", "Large"], variation: true }],
  variations: [
    { id: 202, attributes: { size: "Small" }, price: "30.00", stock_status: "instock" },
    { id: 203, attributes: { size: "Large" }, price: "34.00", stock_status: "outofstock" },
  ],
})

export default function VariationExercise() {
  const selection = useWpProductVariations(product)
  const { formatPrice } = useWpCurrency()
  const ready = selection.isSelectionComplete && Boolean(selection.matchingVariation) && !selection.isOutOfStock
  return <section aria-label="Variation exercise">
    <label htmlFor="shirt-size">Size</label>
    <select id="shirt-size" value={selection.selectedAttributes.size || ""}
      onChange={event => selection.setAttribute("size", event.target.value)}>
      <option value="">Choose a size</option>
      <option value="Small">Small</option><option value="Large">Large</option>
    </select>
    <p>{formatPrice(selection.currentPrice, { decimals: 2 })}</p>
    <p role="status">{!selection.isSelectionComplete ? "Choose a size." : ready ? "Available choice." : "Unavailable choice."}</p>
    <button type="button" disabled={!ready}>Selection ready (demo)</button>
    <button type="button" onClick={selection.resetAttributes}>Reset selection</button>
  </section>
}
```

This check does not reserve stock. The action demonstrates readiness only; production must validate stock and the selected variation on the server. Explicit decimals stabilize this fixture's display because the formatter otherwise can default to zero decimal places.

---

Source: [Solution for the Variation Selector](https://www.forgewp.dev/docs/woocommerce/woo-challenges/variant-selector-solution)
