9. Shipping
List shipping options and add a shipping method to the cart.
On this page
The third step in the express checkout flow is the shipping method selection step. In this step, the customer will choose a shipping method to deliver their order.
To create the component of this step, create the file components/Shipping/index.tsx with the following content:

tsx
You create a Shipping component that receives the product's handle and whether the component is active as props. The component defines the following variables:
cartandupdateCart: The customer's cart and the function to update the cart, retrieved from the cart context.loading: A boolean state variable indicating whether an operation is loading.shippingMethod: A state variable that holds the selected shipping method.shippingOptions: A state variable that holds the available shipping options.calculatedPrices: A state variable that holds the calculated prices for each shipping option that doesn't have a flat rate price.router: The router instance to navigate between steps.
Retrieve Shipping Options
In the Aiminaabee application, you can define shipping options for each stock location, sales channel, or other conditions. So, during checkout, you retrieve the shipping options specific to a cart's context and allow the customer to choose from them.
To do that, first, add the following imports at the top of the file:
tsx
Then, replace the TODO in the Shipping component with the following:
]
tsx
In the useEffect hook, you use the JS SDK to send a request to the List Shipping Options of a Cart API route, passing the cart's ID as a query parameter. The API route returns a list of shipping options, which you set in the shippingOptions state variable.
Retrieve Calculated Prices
Shipping options have a price_type property whose value is either:
flat_rate: This value means the shipping option has a fixed price.calculated: This value means the shipping option's price is calculated based on the cart's context, such as the shipping address or items in the carts. This is useful when the fulfillment provider that's associated with a shipping option calculates the price based on these conditions.
So, to retrieve the prices of calculated shipping options, you use Aiminaabee's Calculate Shipping Option Price API route.
Replace the TODO in the Shipping component with the following:
tsx
In the useEffect hook, you filter the shippingOptions to get only the calculated shipping options. Then, you loop over these options to retrieve their calculated price from the Aiminaabee application. You pass to the Calculate Shipping Option API route the cart's ID and any custom data that the fulfillment provider might need to calculate the price.
Once all shipping option prices are calculated, you set them in the calculatedPrices state variable, where the key is the shipping option's ID and the value is the calculated price.
Format Prices Function
When you display the shipping options and their prices, you want to format the prices with the currency code. You'll use the formatPrice utility function you created earlier.
First, add the following imports at the top of the file:
tsx
Then, replace the TODO in the Shipping component with the following:
tsx
You create a getShippingOptionPrice function that receives a shipping option to get its formatted price. You use the formatPrice utility function, passing it either the shipping option's flat or calculated price, based on its type. You return the formatted price.
Implement Submit Function
Next, you'll implement the function that will be called when the customer submits their shipping method selection.
Replace the TODO in the Shipping component with the following:
tsx
You define an isButtonDisabled memoized variable that indicates whether the customer can submit their selection. You also define a handleSubmit function that uses the updateCart function from the cart context to update the cart with the selected shipping method.
Once the shipping method is saved, you redirect the customer to the next step in the flow, which is the payment step.
Render Shipping Step
Finally, you'll add the return statement showing the shipping method selection form that allows the customer to choose a shipping method.
First, add the following imports at the top of the file:
tsx
Then, replace the last TODO in the Shipping component with the following:
tsx
You wrap the shipping method selection form with the Card component you created earlier. In the card, you show a radio group with the available shipping options and their prices.
The customer can select a shipping option and click the "Go to payment" button to go to the next step.
Add to Router Component
Finally, you'll add the Shipping component to the Router component.
First, import the Shipping component at the top of the components/Router/index.tsx file:
tsx
Then, change the return statement of the Router component to the following
tsx
This will show the shipping method selection step and expand its details if activeTab is shipping.
Test it Out
While both the Aiminaabee application and the Aiminaabee Next.js storefront template are running, if you refresh the page you had opened or go to http://localhost:3000/sweatpants?step=shipping, you should see the Shipping step where you can choose a shipping method.

You can select a shipping option then click on the "Go to payment" button. The shipping method will be saved in the cart and the Shipping card will collapse to show the fourth step's details, which you'll add next.
Next: 10. Payment