Skip to main content
Cart state lives in CartProvider and is read with useCart. Amounts are handled in minor units, so see Money for formatting.

cart/cart-provider

@replohq/sdk/cart/cart-provider

CartContextType

Public cart context type for LLM use. For adding products to cart, use useAddToCart. For buy now functionality, use useBuyNow.

CartContextTypeInternal

Internal cart context type with full functionality. This is for internal use only - external code should use the limited CartContextType.

CartError

The most recent cart change that could not be saved. message is written for the shopper, so a cart UI can render it as-is next to a retry.

CartProvider

Provider component that manages global cart state and operations. Handles both server-side cart synchronization and client-side optimistic updates.
Returns: A context provider wrapping the children with cart functionality

useCart

Hook to access cart UI state and controls. For adding products to cart, use useAddToCart. For buy now functionality, use useBuyNow.

useCartInternal

Deprecated. For internal use only. Use useAddToCart or useBuyNow for cart operations. Use useCart for cart UI state (itemsCount, openCart, closeCart).
Also exported

cart/hooks/use-add-to-cart

@replohq/sdk/cart/hooks/use-add-to-cart

AddToCartItem

Describes a single item to add to the cart. Uses the merchandiseId (e.g. a variant GID) directly from data loader output. Product display data for optimistic UI is resolved automatically from the React Query cache for both single-product loaders (e.g. ProductLoader) and product-list loaders (e.g. CollectionProductsLoader, RebuyRecommendationsLoader). Pass merchandise explicitly to bypass the cache lookup when the variant is known but its product was not loaded via a supported loader.

AddToCartResult

What an addToCart call did. adjustments lists what the server changed about a saved request (a quantity clamped to stock, an unavailable line removed). error is set instead when the cart could not be created or saved; the same message is on useCart().error for the cart UI.

useAddToCart

Hook for adding items to cart using merchandise IDs from data loader output. This is the primary hook for adding items to cart in the data-loader architecture. It works with the resolver-backed CartGateway so the scaffold never needs direct integration credentials. Merchandise display data (title, price, image) for optimistic cart UI is auto-resolved from the React Query cache for both single-product loaders (e.g. ProductLoader) and product-list loaders (e.g. CollectionProductsLoader, RebuyRecommendationsLoader). Just pass the merchandiseId. Pass merchandise explicitly only when the variant is known but its product was not loaded via a supported loader. addToCart never throws. It resolves an AddToCartResult: adjustments when the add was saved, or error when the cart could not be created or saved. On failure the provider also records the error on useCart().error and opens the cart UI, which renders it, so a click handler needs no handling of its own.
Also exported

cart/hooks/use-buy-now

@replohq/sdk/cart/hooks/use-buy-now

BuyNowOutcome

What a buyNow call did. Out of stock is an expected storefront outcome (a buyer can race the last unit), so it rides the return value rather than a thrown error; variants carries each short variant with the purchasable remainder so the button can render a sold-out state.

useBuyNow

React hook that returns a buyNow function for express checkout flows. Handles creating the cart/checkout session server-side and redirecting the browser to checkout. Stripe checkout ignores selling plans and discount codes, logging a server-side warning when either is supplied.
Returns: Object containing the buyNow function

VariantBuyNowInfo

Describes a single variant to send directly to checkout.

cart/cart-types

@replohq/sdk/cart/cart-types

CartDiscountAllocation

One discount Shopify applied to a cart line or to the whole cart. The amount is already subtracted from cost.totalAmount (and from cost.subtotalAmount for line-level discounts).

CartDiscountCode

Represents a discount code applied to a cart.

CartLineAdjustment

How a cart write differed from what the shopper asked for, derived by comparing the requested lines against the cart the server saved. Cart UIs render these as Shopify-style notices (“Only 9 in stock — we’ve updated your cart”); the underlying clamp has already been applied to the cart.

REPLO_ATTRIBUTION_PROPERTY

Replo attribution property added to cart/buy-now line items for order attribution in Shopify.
Also exported

cart/buy-now-action

@replohq/sdk/cart/buy-now-action

buyNowAction

Server action that handles the full buy-now flow: 1. For Shopify: creates a cart via the canopy-api-backed gateway, returns checkoutUrl. 2. For Stripe: creates a Stripe checkout session via canopy-api, returns session URL. Variant IDs are expected to already be Storefront GIDs (from data loader output). No internal→external ID mapping is needed.
Also exported

cart/utils/cart-utils

@replohq/sdk/cart/utils/cart-utils

applyCartLineReconciliation

Rewrite a cookie cart with the catalog’s current facts: live prices and currency always, unavailable lines dropped, and — on write paths only — quantities clamped to the purchasable remainder. Reads never clamp (clampQuantities: false): a quantity change without messaging is worse than one the mutation or checkout path reports, mirroring Shopify’s cart behavior. reconciledLines must be the catalog’s answer for this cart’s merchandise-bearing lines in order — the shape reconcileCartLines returns.

calculateCartTotals

Calculate cart totals including subtotal, discounts, and final total. When cart cost data is provided (from Shopify), uses the actual totalAmount and sums the provider’s discount allocations (automatic, code, and custom discounts, at both line and cart level) for the discount amount.

calculateLineItemsSubtotal

Sum the final (post selling-plan) price of every line item in the cart. This is the authoritative subtotal for cookie-based carts, which have no server-computed cost to rely on.

formatPrice

Deprecated. Back-compat shim kept for surface parity with canopy-sdk, so sites migrating off import { formatPrice } from "canopy-sdk/cart/utils/cart-utils" keep rendering with only the package specifier renamed. It formats INTEGER MINOR UNITS — the units every cart value uses (calculateCartTotals, getCartLinePricing, CartCost): passing 1999 yields “19.99",not"19.99", not "1,999.00”. New code should call formatAmount({ amount, currencyCode }) from @replohq/sdk/money directly.

formatSellingPlanId

Validates and formats a Shopify selling plan ID to ensure it has the correct GID format.
Returns: The formatted selling plan ID with GID prefix, or null if no ID provided

recalculateCartCost

Recompute a cart’s cost from its line items. Cookie carts are their own source of truth (no server roundtrip) and apply no discounts, so subtotal equals total and there are no discount allocations. Without this the cost stays at the 0 it was created with, and every consumer that trusts it (cart total, checkout analytics) reports $0 for a priced cart.
Also exported

cart/utils/cookie-cart-persistence

@replohq/sdk/cart/utils/cookie-cart-persistence

addToCartWithPersistence

Add lines to cart with configurable line merging strategy

clearCartCookie

Clear cart cookie

createCartWithPersistence

Create a new cart with persistence

LineMergeFn

Line merge function type - returns index of existing line to merge with, or -1 to add as new line

loadCartFromCookie

Load cart from cookie

removeCartLineItemsWithPersistence

Remove cart line items

saveCartToCookie

Save cart to cookie

updateCartInCookie

Update cart in cookie atomically (load-transform-save pattern)

updateCartLineItemWithPersistence

Update cart line item

cart/utils/variant-to-cart-line

@replohq/sdk/cart/utils/variant-to-cart-line

createOptimisticSellingPlanAllocation

Creates an optimistic selling plan allocation using product data. This matches the exact structure that Shopify returns so the UI doesn’t need conditional logic.

getCartLineAttribute

Gets a specific attribute value from a cart line.
Returns: The attribute value, or undefined if not found

getCartLineBundleInfo

Extracts bundle information from cart line attributes.
Returns: Object containing bundle ID, discount, and whether it’s a bundle item

getCartLinePricing

Calculates pricing information for a cart line, including discounts and selling plan adjustments.
Returns: Object containing compareAtPrice, discountPercentage, hasDiscount, sellingPlanAdjustedPrice, and finalPrice

getCartLineSpecialInfo

Extracts special item information from cart line attributes.
Returns: Object containing whether it’s a special item and any special instructions

getCartLineSubscriptionInfo

Extracts subscription (selling plan) information from a cart line.
Returns: Object containing isSubscription, frequency, discount, and sellingPlan

cart/gateways/cart-constants

@replohq/sdk/cart/gateways/cart-constants Exports

cart/return-urls

@replohq/sdk/cart/return-urls Exports

hooks/use-formatted-price

@replohq/sdk/hooks/use-formatted-price

useFormattedPrice

Use useFormattedPrice to format prices with proper currency and locale.
Returns: A formatted price string based on the price and currency code. It already takes into account the user’s locale/browser.