These pages refer to Hydrogen versions 2026-04 and earlier. For versions 2026-10 and later, refer to the latest Hydrogen docs. If you're not using the latest version of Hydrogen, then we recommend that you migrate your storefront. To learn more, refer to Migrate from legacy Hydrogen.
Update selected delivery options
This guide shows you how to use a cart handler to update selected delivery options, such as standard or expedited delivery.
Anchor to RequirementsRequirements
- You've completed the quickstart guide.
- You've set up a cart handler.
Anchor to Step 1: Read selected and available delivery groupsStep 1: Read selected and available delivery groups
Carts return an empty array for available delivery groups by default. To get a list of delivery groups, the cart must be associated with a customer access token by updating the buyer identity when a customer is logged in.
Update the cart query fragment to return selected and available delivery options. Request more than the first group, because a cart can have several groups, such as a separate group for subscription items. The following fragment requests up to 10 groups and their pageInfo.
A cart can also have a shipping group and a pickup group at the same time. For more information, refer to Support shipping and pickup delivery groups in a cart.
File
server.js
JavaScript
const cart = createCartHandler({
storefront,
getCartId: cartGetIdDefault(request.headers),
setCartId: cartSetIdDefault(),
cartQueryFragment: CART_QUERY_FRAGMENT,
});
const CART_QUERY_FRAGMENT = `#graphql
fragment CartApiQuery on Cart {
id
deliveryGroups(first: 10) {
nodes {
id
groupType
deliveryOptions {
handle
title
code
estimatedCost {
amount
currencyCode
}
}
selectedDeliveryOption {
handle
deliveryMethodType
}
}
pageInfo {
hasNextPage
}
}
}
`;TypeScript
const cart = createCartHandler({
storefront,
getCartId: cartGetIdDefault(request.headers),
setCartId: cartSetIdDefault(),
cartQueryFragment: CART_QUERY_FRAGMENT,
});
const CART_QUERY_FRAGMENT = `#graphql
fragment CartApiQuery on Cart {
id
deliveryGroups(first: 10) {
nodes {
id
groupType
deliveryOptions {
handle
title
code
estimatedCost {
amount
currencyCode
}
}
selectedDeliveryOption {
handle
deliveryMethodType
}
}
pageInfo {
hasNextPage
}
}
}
`;If pageInfo.hasNextPage is true, then the cart has more groups than the fragment returns, and an update that leaves a group out can clear that group's selection. The form in step 2 disables every option in that case. To let buyers choose in larger carts, request more groups.
Anchor to Step 2: Create a select delivery option formStep 2: Create a select delivery option form
Use React Router's useFetcher hook to create a delivery option form. The hook submits a form request to the /cart route's action when users choose a delivery option. You can use this component anywhere in the app. The examples import useFetcher from react-router, and the TypeScript example also imports the FetcherWithComponents type from there. In a Remix app, import both from @remix-run/react.
When you use fetcher.submit, make sure there's a data key with the name CartForm.INPUT_NAME. The key value must be a JSON stringified object with action and inputs defined.
When the buyer changes one group, the request sends the new option and the current selection of every other group. A group that the request leaves out can lose its selection.
In a cart without a subscription group, the request also leaves out each other pickup group, that is, a group whose selected option has a deliveryMethodType of PICK_UP. The cart keeps that pickup group, with its store and its selected option. In a cart with a subscription group, a pickup group that a request leaves out can lose its store, so there the request sends every group.
For more information, refer to Step 4 of the delivery groups guide.
The component calls useFetcher with a fixed key, so every group shares one fetcher, even when the component renders in several places. The example also disables every option while an update is pending. Together, these keep a request from sending a selection that an earlier request is still changing. The example disables the options too when pageInfo.hasNextPage is true, so that no update leaves out a group that the fragment didn't return.
The example labels each option with its title. An option's title can be null, and when the cart hides the buyer's delivery details, every option's title is Delivery. If the buyer can't tell a group's options apart by their titles, then don't show that group's options, and let the buyer choose in checkout.
Each group's options are in a fieldset, so that buyers and screen readers can tell the groups apart. To show which items each group's options apply to, add the cart's lines and the id of each group's cartLines to the fragment, and pass the cart's lines to the component. In the group's legend, show the item of the cart line with the same id, or for a bundle, the item in the lineComponents of the bundle's line. Don't request merchandise in the group's cartLines, because the request can fail for a bundle component whose product isn't published to your storefront. The example labels each group only with its number.
Each group's options are in a fieldset, so that buyers and screen readers can tell the groups apart. To show which items each group's options apply to, add the cart's lines and the id of each group's cartLines to the fragment, and pass the cart's lines to the component. In the group's legend, show the item of the cart line with the same id, or for a bundle, the item in the lineComponents of the bundle's line. Don't request merchandise in the group's cartLines, because the request can fail for a bundle component whose product isn't published to your storefront. The example labels each group only with its number.
File
/app/components/SelectDeliveryGroup.jsx
JavaScript
import {CartForm} from '@shopify/hydrogen';
import {useFetcher} from 'react-router';
function SelectDeliveryGroups({deliveryGroups}) {
const fetcher = useFetcher({key: 'select-delivery-option'});
const groups = deliveryGroups?.nodes ?? [];
const hasMoreGroups = deliveryGroups?.pageInfo?.hasNextPage ?? false;
return groups.map((group, index) => (
<SelectDeliveryOption
key={group.id}
fetcher={fetcher}
groups={groups}
index={index}
hasMoreGroups={hasMoreGroups}
/>
));
}
function SelectDeliveryOption({fetcher, groups, index, hasMoreGroups}) {
const group = groups[index];
const deliveryOptions = group.deliveryOptions;
const selectedHandle = group.selectedDeliveryOption?.handle;
const hasSubscriptionGroup = groups.some((cartGroup) => cartGroup.groupType === 'SUBSCRIPTION');
const name = `delivery-group-${index}`;
return (
<fieldset>
<legend>{`Delivery ${index + 1}`}</legend>
{deliveryOptions.map((option) => {
return (
<div key={option.handle}>
<input
checked={selectedHandle === option.handle}
disabled={hasMoreGroups || fetcher.state !== 'idle'}
type="radio"
name={name}
value={option.handle}
id={`${name}-${option.handle}`}
onChange={(event) => {
fetcher.submit(
{
[CartForm.INPUT_NAME]: JSON.stringify({
action: CartForm.ACTIONS.SelectedDeliveryOptionsUpdate,
inputs: {
selectedDeliveryOptions: groups.flatMap((otherGroup, otherIndex) => {
if (otherIndex === index) {
return [{deliveryGroupId: otherGroup.id, deliveryOptionHandle: event.target.value}];
}
const selected = otherGroup.selectedDeliveryOption;
return selected && (hasSubscriptionGroup || selected.deliveryMethodType !== 'PICK_UP')
? [{deliveryGroupId: otherGroup.id, deliveryOptionHandle: selected.handle}]
: [];
}),
},
}),
},
{method: 'POST', action: '/cart'}
)
}}
/>
<label htmlFor={`${name}-${option.handle}`}>{option.title}</label>
</div>
)
})}
</fieldset>
);
}TypeScript
import {CartForm, type CartActionInput} from '@shopify/hydrogen';
import type {Cart} from '@shopify/hydrogen/storefront-api-types';
import {useFetcher, type FetcherWithComponents} from 'react-router';
function SelectDeliveryGroups({
deliveryGroups
}: {
deliveryGroups: Cart['deliveryGroups'];
}) {
const fetcher = useFetcher({key: 'select-delivery-option'});
const groups = deliveryGroups?.nodes ?? [];
const hasMoreGroups = deliveryGroups?.pageInfo?.hasNextPage ?? false;
return groups.map((group, index) => (
<SelectDeliveryOption
key={group.id}
fetcher={fetcher}
groups={groups}
index={index}
hasMoreGroups={hasMoreGroups}
/>
));
}
function SelectDeliveryOption({
fetcher,
groups,
index,
hasMoreGroups
}: {
fetcher: FetcherWithComponents<unknown>;
groups: Cart['deliveryGroups']['nodes'];
index: number;
hasMoreGroups: boolean;
}) {
const group = groups[index];
const deliveryOptions = group.deliveryOptions;
const selectedHandle = group.selectedDeliveryOption?.handle;
const hasSubscriptionGroup = groups.some((cartGroup) => cartGroup.groupType === 'SUBSCRIPTION');
const name = `delivery-group-${index}`;
return (
<fieldset>
<legend>{`Delivery ${index + 1}`}</legend>
{deliveryOptions.map((option) => {
return (
<div key={option.handle}>
<input
checked={selectedHandle === option.handle}
disabled={hasMoreGroups || fetcher.state !== 'idle'}
type="radio"
name={name}
value={option.handle}
id={`${name}-${option.handle}`}
onChange={(event) => {
fetcher.submit(
{
[CartForm.INPUT_NAME]: JSON.stringify({
action: CartForm.ACTIONS.SelectedDeliveryOptionsUpdate,
inputs: {
selectedDeliveryOptions: groups.flatMap((otherGroup, otherIndex) => {
if (otherIndex === index) {
return [{deliveryGroupId: otherGroup.id, deliveryOptionHandle: event.target.value}];
}
const selected = otherGroup.selectedDeliveryOption;
return selected && (hasSubscriptionGroup || selected.deliveryMethodType !== 'PICK_UP')
? [{deliveryGroupId: otherGroup.id, deliveryOptionHandle: selected.handle}]
: [];
}),
},
}),
},
{method: 'POST', action: '/cart'}
)
}}
/>
<label htmlFor={`${name}-${option.handle}`}>{option.title}</label>
</div>
)
})}
</fieldset>
);
}Anchor to Step 3: Handle the selected delivery option form requestStep 3: Handle the selected delivery option form request
Handle the selected delivery option form request in an action. Use the cart, created from createCartHandler, to handle cart mutation requests to the Storefront API.
File
/app/routes/cart.jsx
JavaScript
import {CartForm} from '@shopify/hydrogen';
import invariant from 'tiny-invariant';
export async function action({request, context}) {
const {cart} = context;
const formData = await request.formData();
const {action, inputs} = CartForm.getFormInput(formData);
let result;
switch(action) {
case CartForm.ACTIONS.SelectedDeliveryOptionsUpdate:
result = await cart.updateSelectedDeliveryOption(inputs.selectedDeliveryOptions);
break;
default:
invariant(false, `${action} cart action is not defined`);
}
// The Cart ID might change after each mutation, so update it each time.
const headers = cart.setCartId(result.cart.id);
return json(
result,
{status: 200, headers},
);
}TypeScript
import {
type CartQueryData,
CartForm,
} from '@shopify/hydrogen';
import invariant from 'tiny-invariant';
export async function action({request, context}: ActionArgs) {
const {cart} = context;
const formData = await request.formData();
const {action, inputs} = CartForm.getFormInput(formData);
let result: CartQueryData;
switch(action) {
case CartForm.ACTIONS.SelectedDeliveryOptionsUpdate:
result = await cart.updateSelectedDeliveryOption(inputs.selectedDeliveryOptions);
break;
default:
invariant(false, `${action} cart action is not defined`);
}
// The Cart ID might change after each mutation, so update it each time.
const headers = cart.setCartId(result.cart.id);
return json(
result,
{status: 200, headers},
);
}