Skip to main content

Product form store

The createProductFormStore function creates a store that tracks the customer's option selection for one product and adds the selected variant to the cart. Use the store to show which option values are available, find the selected variant, and submit the add-to-cart form.

React and Vue apps use createProductComponents.

Import createProductFormStore and createProductFormRegister from @shopify/hydrogen.


Creates a store that tracks the customer's option selection for one product and adds the selected variant to the cart.

The store resolves variants from the product data that you pass and leaves the URL to you. Build the product URL from the result of selectOption(). Call destroy() when the product form unmounts. The product provider from createProductComponents creates and destroys the store for you.

Anchor to product
product
<>
required

The product from your Storefront API query.

Anchor to cartStore
cartStore
required

The cart store that the form adds variants to and reads cart errors from.

Anchor to options
options

The selection to start from when the product has no selected or first available variant.

A product form store. The starting selection comes from the product's selected or first available variant, or else from the selection in the options.

Examples

TypeScript

import { createCartStore, createProductFormStore, type ProductInput } from "@shopify/hydrogen";

declare const product: ProductInput;

const cartStore = createCartStore();
const store = createProductFormStore(product, cartStore);

const unsubscribe = store.subscribe((state) => {
console.log("selected variant:", state.selectedVariant);
});

const result = store.selectOption("Color", "Red");

if (result.status === "invalid") {
console.warn(result.reason);
}

unsubscribe();
store.destroy();

Creates the register function that returns props for each product form field.

The merchandise ID field submits the variant that you pass at creation. Create a new register function each time the product form state changes. The product form hooks create a new register function for you.

Anchor to selectedVariant
selectedVariant
required

The variant that the merchandise ID field submits, or null when the selection doesn't resolve to a variant.

Anchor to selectOption
selectOption
(name: string, value: string) => void
required

The function that option value fields call with the option name and value.

A register function for the merchandise ID, quantity, option value, attribute, and add-to-cart fields.

Examples

TypeScript

import { createProductFormRegister, type ProductFormStore } from "@shopify/hydrogen";

declare const store: ProductFormStore;

const { selectedVariant } = store.getState();
const register = createProductFormRegister(selectedVariant, (name, value) => {
store.selectOption(name, value);
});

const merchandiseId = register("merchandiseId", {});
const quantity = register("quantity", { value: 1 });
const engraving = register("attributeValue", { key: "Engraving", value: "Hello" });
const addToCart = register("addToCart", {});
const red = register("optionValue", { optionName: "Color", value: "Red" });

console.log(merchandiseId, quantity, engraving, addToCart);
red.onClick();

Was this page helpful?