Skip to main content

Shop Pay button

The ShopPayButton component renders a Shop Pay button that checks out a selected variant right away or checks out the current cart. Use the component in React and Vue apps.

For DOM code, server-rendered HTML, and frameworks without a Hydrogen binding, use createShopPayButton, renderShopPayButton, and defineShopPayButton from @shopify/hydrogen.

Import ShopPayButton from @shopify/hydrogen/react or @shopify/hydrogen/vue.


Props for the ShopPayButton component. The props match the options of createShopPayButton. The component doesn't accept className or style props.

Anchor to accessibilityLabel
accessibilityLabel
string

Localized accessible label for the button. Defaults to "Buy with Shop Pay". Keep the brand name Shop Pay untranslated and in Latin script in every locale.

Anchor to borderRadius
borderRadius
string

CSS border radius of the button, such as 8px. Defaults to 12px.

Pass a single CSS value. The Shop Pay functions throw an error when the radius contains a semicolon or a curly brace.

Anchor to channel
channel
'headless' | 'hydrogen'

Sales channel that the checkout URL carries as channel. Omit the channel unless checkout needs explicit headless or hydrogen attribution.

Anchor to checkoutUrl
checkoutUrl
string

Absolute checkout base URL. Omit the URL to link to the same-origin /checkout and /cart permalink paths, which Hydrogen's Shopify route handler redirects to the store's checkout.

Pass the cart's checkout URL when your app doesn't send same-origin checkout paths through the Shopify route handler. The button URL keeps the query params of the checkout URL, replaces any params that Shop Pay sets, and drops the hash. A bare domain such as my-store.myshopify.com gets an HTTPS scheme. The Shop Pay functions throw an error when the value isn't a valid URL.

Anchor to disabled
disabled
boolean

Disables the button. A disabled button renders without an href and with aria-disabled="true".

Anchor to nonce
nonce
string

Content Security Policy nonce for the button's style element.

The nonce covers only the style element. Custom width and border radius values render as an inline style attribute, which a strict Content Security Policy must allow.

Anchor to paymentOption
paymentOption
'shop_pay' | 'shop_pay_installments'

Checkout payment mode. Defaults to shop_pay. Use shop_pay_installments to open Shop Pay Installments at checkout.

Anchor to source
source
string

Attribution source that the checkout URL carries as source. Defaults to hydrogen. Most storefronts shouldn't change the source.

Anchor to sourceToken
sourceToken
string

Attribution token that the checkout URL carries as source_token.

Anchor to variants
variants

Variants that the customer buys right away through a Shopify cart permalink. Omit the variants to check out the current cart.

Pass either all variant ID strings or all objects with an id and an optional quantity. Each quantity must be a positive integer. The Shop Pay functions throw an error for product IDs, non-numeric variant IDs, invalid quantities, and mixed formats.

Anchor to width
width
string

CSS width of the button, such as 100%. Defaults to 260px.

Pass a single CSS value. The Shop Pay functions throw an error when the width contains a semicolon or a curly brace.

Examples

React

import { ShopPayButton } from "@shopify/hydrogen/react";

type BuyNowProps = {
variantId: string | null;
quantity: number;
disabled: boolean;
};

export function BuyNow({ variantId, quantity, disabled }: BuyNowProps) {
if (!variantId) return null;

return (
<ShopPayButton
variants={[{ id: variantId, quantity }]}
disabled={disabled}
width="100%"
borderRadius="9999px"
/>
);
}

Creates a Shop Pay button element in the browser. Append the element to the page.

The function requires a DOM. Use renderShopPayButton during server rendering.

Anchor to options
options
required

The variants, checkout URL, and display settings for the button.

HTMLElement

The Shop Pay button element, ready to append to the page.

Examples

TypeScript

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

export function mountShopPayButton(container: HTMLElement, variantId: string, quantity: number) {
const button = createShopPayButton({
variants: [{ id: variantId, quantity }],
width: "100%",
});

container.append(button);
return button;
}

Anchor to Server HTML and custom elementServer HTML and custom element

Returns the Shop Pay button as an HTML string for server templates and frameworks without a Hydrogen binding.

On the server, the markup includes the button's styles and link, and the button works without client JavaScript. In the browser, the function returns an empty element that renders the button when you add the element to the page.

Anchor to options
options
required

The variants, checkout URL, and display settings for the button.

The Shop Pay button markup.

Anchor to defineShopPayButton
defineShopPayButton()

Registers the Shop Pay button custom element in the browser. Call the function when you use the hydrogen-shop-pay-button tag directly on a framework without a Hydrogen binding.

The Hydrogen Shop Pay components, createShopPayButton, and renderShopPayButton register the element for you. The function does nothing during server rendering or when the browser already has the element.

Nothing.

Examples

TypeScript

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

export function renderBuyNow(variantId: string, quantity: number, pending: boolean) {
const shopPay = renderShopPayButton({
variants: [{ id: variantId, quantity }],
disabled: pending,
width: "100%",
borderRadius: "9999px",
});

return `<div class="buy-buttons">${shopPay}</div>`;
}

The Shop Pay button is a hydrogen-shop-pay-button custom element that holds the link and logo in a shadow root. Page CSS can't reach the shadow root. Size the button with the width and borderRadius options.


Was this page helpful?