--- title: Subscriptions in Hydrogen description: Add subscription-based products to your Hydrogen storefront. source_url: html: >- https://shopify.dev/docs/storefronts/headless/hydrogen/cookbook/subscriptions md: >- https://shopify.dev/docs/storefronts/headless/hydrogen/cookbook/subscriptions.md --- ExpandOn this page * [Requirements](https://shopify.dev/docs/storefronts/headless/hydrogen/cookbook/subscriptions.md#requirements) * [Ingredients](https://shopify.dev/docs/storefronts/headless/hydrogen/cookbook/subscriptions.md#ingredients) * [Step 1: Set up the Shopify Subscriptions app](https://shopify.dev/docs/storefronts/headless/hydrogen/cookbook/subscriptions.md#step-1-set-up-the-shopify-subscriptions-app) * [Step 2: Update Cart​Line​Item to display subscription details](https://shopify.dev/docs/storefronts/headless/hydrogen/cookbook/subscriptions.md#step-2-update-cartlineitem-to-display-subscription-details) * [Step 3: Show subscription options on product pages](https://shopify.dev/docs/storefronts/headless/hydrogen/cookbook/subscriptions.md#step-3-show-subscription-options-on-product-pages) * [Step 4: Show subscription details in the cart](https://shopify.dev/docs/storefronts/headless/hydrogen/cookbook/subscriptions.md#step-4-show-subscription-details-in-the-cart) * [Step 5: Add subscription management to the account page](https://shopify.dev/docs/storefronts/headless/hydrogen/cookbook/subscriptions.md#step-5-add-subscription-management-to-the-account-page) * [Next steps](https://shopify.dev/docs/storefronts/headless/hydrogen/cookbook/subscriptions.md#next-steps) # Subscriptions in Hydrogen This recipe lets you sell subscription-based products on your Hydrogen storefront by implementing [selling plan groups](https://shopify.dev/docs/api/storefront/latest/objects/SellingPlanGroup). Your customers will be able to choose between one-time purchases or recurring subscriptions for any products with available selling plans. In this recipe you'll make the following changes: 1. Set up a subscriptions app in your Shopify admin and add selling plans to any products that will be sold as subscriptions. 2. Modify product detail pages to display subscription options with accurate pricing using the `SellingPlanSelector` component. 3. Enhance GraphQL fragments to fetch all necessary selling plan data. 4. Display subscription details on applicable line items in the cart. 5. Add a **Subscriptions** page where customers can manage their subscriptions, which includes the option to cancel active subscriptions. *** ## Requirements To implement subscriptions in your own store, you need to install a subscriptions app in your Shopify admin. In this recipe, we'll use the [Shopify Subscriptions app](https://apps.shopify.com/shopify-subscriptions). *** ## Ingredients *New files added to the template by this recipe.* | File | Description | | - | - | | [app/components/SellingPlanSelector.tsx](https://github.com/Shopify/hydrogen/blob/12374c8f03f82c6800000cf08e327c4db4c287bb/cookbook/recipes/subscriptions/ingredients/templates/skeleton/app/components/SellingPlanSelector.tsx) | Displays the available subscription options on product pages. | | [app/graphql/customer-account/CustomerSubscriptionsMutations.ts](https://github.com/Shopify/hydrogen/blob/12374c8f03f82c6800000cf08e327c4db4c287bb/cookbook/recipes/subscriptions/ingredients/templates/skeleton/app/graphql/customer-account/CustomerSubscriptionsMutations.ts) | Mutations for managing customer subscriptions. | | [app/graphql/customer-account/CustomerSubscriptionsQuery.ts](https://github.com/Shopify/hydrogen/blob/12374c8f03f82c6800000cf08e327c4db4c287bb/cookbook/recipes/subscriptions/ingredients/templates/skeleton/app/graphql/customer-account/CustomerSubscriptionsQuery.ts) | Queries for managing customer subscriptions. | | [app/routes/account.subscriptions.tsx](https://github.com/Shopify/hydrogen/blob/12374c8f03f82c6800000cf08e327c4db4c287bb/cookbook/recipes/subscriptions/ingredients/templates/skeleton/app/routes/account.subscriptions.tsx) | Subscriptions management page. | | [app/styles/account-subscriptions.css](https://github.com/Shopify/hydrogen/blob/12374c8f03f82c6800000cf08e327c4db4c287bb/cookbook/recipes/subscriptions/ingredients/templates/skeleton/app/styles/account-subscriptions.css) | Subscriptions management page styles. | | [app/styles/selling-plan.css](https://github.com/Shopify/hydrogen/blob/12374c8f03f82c6800000cf08e327c4db4c287bb/cookbook/recipes/subscriptions/ingredients/templates/skeleton/app/styles/selling-plan.css) | Styles the `SellingPlanSelector` component. | *** ## Step 1: Set up the Shopify Subscriptions app 1. Install the [Shopify Subscriptions app](https://apps.shopify.com/shopify-subscriptions). 2. In your Shopify admin, [use the Subscriptions app](https://admin.shopify.com/apps/subscriptions-remix/app) to create one or more subscription plans. 3. On the [Products](https://admin.shopify.com/products) page, open any products that will be sold as subscriptions and add the relevant subscription plans in the **Purchase options** section. The Hydrogen demo storefront comes pre-configured with an example subscription product with the handle `shopify-wax`. *** ## Step 2: Update Cart​Line​Item to display subscription details 1. Update import paths to use absolute paths for better consistency. 2. Extract `sellingPlanAllocation` from cart line data to access subscription information. 3. Display the subscription plan name when items have an active selling plan. #### File: [CartLineItem.tsx](https://github.com/Shopify/hydrogen/blob/12374c8f03f82c6800000cf08e327c4db4c287bb/templates/skeleton/app/components/CartLineItem.tsx) ([patch](https://github.com/Shopify/hydrogen/blob/12374c8f03f82c6800000cf08e327c4db4c287bb/cookbook/recipes/subscriptions/patches/CartLineItem.tsx.2c9a50.patch)) ```diff @@ -3,8 +3,8 @@ import type {CartLayout} from '~/components/CartMain'; import {CartForm, Image, type OptimisticCartLine} from '@shopify/hydrogen'; import {useVariantUrl} from '~/lib/variants'; import {Link} from 'react-router'; -import {ProductPrice} from './ProductPrice'; -import {useAside} from './Aside'; +import {ProductPrice} from '~/components/ProductPrice'; +import {useAside} from '~/components/Aside'; import type {CartApiQueryFragment} from 'storefrontapi.generated'; type CartLine = OptimisticCartLine; @@ -20,7 +20,8 @@ export function CartLineItem({ layout: CartLayout; line: CartLine; }) { - const {id, merchandise} = line; + // Get the selling plan allocation + const {id, merchandise, sellingPlanAllocation} = line; const {product, title, image, selectedOptions} = merchandise; const lineItemUrl = useVariantUrl(product.handle, selectedOptions); const {close} = useAside(); @@ -54,6 +55,12 @@ export function CartLineItem({