Skip to main content

Cart forms

The cart form functions build cart forms that submit through the cart store and still work without JavaScript. Use these functions to get the attributes for cart form fields and buttons, submit the form when a customer changes a quantity, and keep a typed quantity within limits.

In React and Vue apps, use the cart form hook from createCartComponents.

Import createCartFormRegister, attachQuantityInput, and sanitizeQuantity from @shopify/hydrogen.


Anchor to createCartFormRegister
createCartFormRegister()

Creates a register function that returns the HTML attributes for cart form fields and action buttons.

Use the function to build cart forms without a framework binding. In React, useCartForm returns a register function and form props for you. To submit an interactive quantity input on change, call attachQuantityInput.

The register function holds no state. Share one register function across components.

A register function for cart form fields and action buttons.

Examples

TypeScript

import { createCartFormRegister } from "@shopify/hydrogen";

const register = createCartFormRegister();

export function lineItemFormAttributes(line: { id: string; quantity: number }) {
return {
setButton: register("set"),
lineId: register("lineId", { value: line.id }),
quantity: register("quantity", { value: line.quantity, interactive: true }),
increaseButton: register("increase"),
decreaseButton: register("decrease"),
removeButton: register("remove"),
};
}

export const giftMessageField = register("attributeValue", {
key: "gift-message",
defaultValue: "",
});

Submits a cart form when the customer changes the form's quantity input.

Make the form's first submit button the set button from register("set"). The function throws when the first submit button is any other button.

In React, register the quantity field with interactive: true in useCartForm, which calls the function for you. Call the function yourself in Vue and in frameworks without a binding.

Anchor to inputEl
inputEl
HTMLInputElement
required

The quantity input that submits the form on change.

Anchor to formEl
formEl
HTMLFormElement
required

The cart form that holds the input and the set button.

() => void

A function that stops the automatic submits. Call the function when you remove the input.

Examples

TypeScript

import { attachQuantityInput } from "@shopify/hydrogen";

export function enhanceQuantityInputs(root: ParentNode = document) {
const cleanups: Array<() => void> = [];

for (const input of root.querySelectorAll<HTMLInputElement>('input[name="quantity"]')) {
const form = input.closest("form");
if (form) cleanups.push(attachQuantityInput(input, form));
}

return () => {
for (const cleanup of cleanups) cleanup();
};
}

Turns a raw value, such as text from a quantity input, into a whole-number quantity between a minimum and a maximum.

The function parses strings and rounds decimals. Input that the function can't parse returns the minimum.

unknown
required

The value to turn into a quantity.

Anchor to options
options
{ min?: number; max?: number; }

The lower and upper bounds. The min option defaults to 1, and max defaults to no limit. To stop at available stock, pass the variant's available quantity as max.

The rounded quantity, between the minimum and the maximum.

Examples

TypeScript

import { DEFAULT_MINIMUM_QUANTITY, sanitizeQuantity } from "@shopify/hydrogen";

export function readQuantity(input: HTMLInputElement, quantityAvailable?: number | null) {
return sanitizeQuantity(input.value, {
min: DEFAULT_MINIMUM_QUANTITY,
max: quantityAvailable ?? undefined,
});
}

Was this page helpful?