use Producthook
Provides access to the product value passed to . It also includes properties for selecting product variants, options, and selling plans.
Anchor to propsProps
PartialDeep<
UseProductObjects,
{recurseIntoArrays: true}
> & UseProductFunctions
UseProductFunctions
- Anchor to isOptionInStockisOptionInStock(name: string, value: string) => booleanrequired
A callback that returns a boolean indicating if the option is in stock.
- Anchor to setSelectedOptionsetSelectedOption(name: string, value: string) => voidrequired
A callback to set the selected option.
- Anchor to setSelectedOptionssetSelectedOptions(options: ) => voidrequired
A callback to set multiple selected options at once.
- Anchor to setSelectedSellingPlansetSelectedSellingPlan(sellingPlan: PartialObjectDeep<SellingPlan, { recurseIntoArrays: true; }>) => voidrequired
A callback to set the selected selling plan to the one passed as an argument.
- Anchor to setSelectedVariantsetSelectedVariant(variant: PartialObjectDeep<ProductVariant, { recurseIntoArrays: true; }>) => voidrequired
A callback to set the selected variant to the variant passed as an argument.
UseProductObjects
- Anchor to optionsoptions[]required
An array of the product's options and values.
- Anchor to productproductProductrequired
The raw product from the Storefront API
- Anchor to selectedOptionsselectedOptionsrequired
- Anchor to variantsvariantsProductVariant[]required
An array of the variant
nodes
from the.
- Anchor to selectedSellingPlanselectedSellingPlanSellingPlanType
The selected selling plan.
- Anchor to selectedSellingPlanAllocationselectedSellingPlanAllocationSellingPlanAllocationType
The selected selling plan allocation.
- Anchor to selectedVariantselectedVariantProductVariantType | null
The selected variant.
- Anchor to sellingPlanGroupssellingPlanGroups(Omit<SellingPlanGroup, "sellingPlans"> & { sellingPlans: SellingPlan[]; })[]
The selling plan groups.
- Anchor to sellingPlanGroupsConnectionsellingPlanGroupsConnectionSellingPlanGroupConnection
- Anchor to variantsConnectionvariantsConnectionProductVariantConnection
ProductHookValue
PartialDeep<
UseProductObjects,
{recurseIntoArrays: true}
> & UseProductFunctions
UseProductObjects
- options
An array of the product's options and values.
OptionWithValues[]
- product
The raw product from the Storefront API
Product
- selectedOptions
SelectedOptions
- selectedSellingPlan
The selected selling plan.
SellingPlanType
- selectedSellingPlanAllocation
The selected selling plan allocation.
SellingPlanAllocationType
- selectedVariant
The selected variant.
ProductVariantType | null
- sellingPlanGroups
The selling plan groups.
(Omit<SellingPlanGroup, "sellingPlans"> & { sellingPlans: SellingPlan[]; })[]
- sellingPlanGroupsConnection
SellingPlanGroupConnection
- variants
An array of the variant `nodes` from the `VariantConnection`.
ProductVariant[]
- variantsConnection
ProductVariantConnection
{
/** The raw product from the Storefront API */
product: Product;
/** An array of the variant `nodes` from the `VariantConnection`. */
variants: ProductVariantType[];
variantsConnection?: ProductVariantConnection;
/** An array of the product's options and values. */
options: OptionWithValues[];
/** The selected variant. */
selectedVariant?: ProductVariantType | null;
selectedOptions: SelectedOptions;
/** The selected selling plan. */
selectedSellingPlan?: SellingPlanType;
/** The selected selling plan allocation. */
selectedSellingPlanAllocation?: SellingPlanAllocationType;
/** The selling plan groups. */
sellingPlanGroups?: (Omit<SellingPlanGroupType, 'sellingPlans'> & {
sellingPlans: SellingPlanType[];
})[];
sellingPlanGroupsConnection?: SellingPlanGroupConnection;
}
OptionWithValues
- name
string
- values
string[]
export interface OptionWithValues {
name: SelectedOptionType['name'];
values: SelectedOptionType['value'][];
}
SelectedOptions
{
[key: string]: string;
}
UseProductFunctions
- isOptionInStock
A callback that returns a boolean indicating if the option is in stock.
(name: string, value: string) => boolean
- setSelectedOption
A callback to set the selected option.
(name: string, value: string) => void
- setSelectedOptions
A callback to set multiple selected options at once.
(options: SelectedOptions) => void
- setSelectedSellingPlan
A callback to set the selected selling plan to the one passed as an argument.
(sellingPlan: PartialObjectDeep<SellingPlan, { recurseIntoArrays: true; }>) => void
- setSelectedVariant
A callback to set the selected variant to the variant passed as an argument.
(variant: PartialObjectDeep<ProductVariant, { recurseIntoArrays: true; }>) => void
{
/** A callback to set the selected variant to the variant passed as an argument. */
setSelectedVariant: (
variant: PartialDeep<ProductVariantType, {recurseIntoArrays: true}> | null,
) => void;
/** A callback to set the selected option. */
setSelectedOption: (
name: SelectedOptionType['name'],
value: SelectedOptionType['value'],
) => void;
/** A callback to set multiple selected options at once. */
setSelectedOptions: (options: SelectedOptions) => void;
/** A callback to set the selected selling plan to the one passed as an argument. */
setSelectedSellingPlan: (
sellingPlan: PartialDeep<SellingPlanType, {recurseIntoArrays: true}>,
) => void;
/** A callback that returns a boolean indicating if the option is in stock. */
isOptionInStock: (
name: SelectedOptionType['name'],
value: SelectedOptionType['value'],
) => boolean;
}
Example code
examples
Example code
description
I am the default example
JavaScript
import {ProductProvider, useProduct} from '@shopify/hydrogen-react'; export function Product({product}) { return ( <ProductProvider data={product} initialVariantId="some-id"> <UsingProduct /> </ProductProvider> ); } function UsingProduct() { const {product, variants, setSelectedVariant} = useProduct(); return ( <> <h1>{product?.title}</h1> {variants?.map((variant) => { <button onClick={() => setSelectedVariant(variant)} key={variant?.id}> {variant?.title} </button>; })} ; </> ); }
TypeScript
import {ProductProvider, useProduct} from '@shopify/hydrogen-react'; import type {Product} from '@shopify/hydrogen-react/storefront-api-types'; export function Product({product}: {product: Product}) { return ( <ProductProvider data={product} initialVariantId="some-id"> <UsingProduct /> </ProductProvider> ); } function UsingProduct() { const {product, variants, setSelectedVariant} = useProduct(); return ( <> <h1>{product?.title}</h1> {variants?.map((variant) => { <button onClick={() => setSelectedVariant(variant)} key={variant?.id}> {variant?.title} </button>; })} ; </> ); }