Common patterns
Storefront Web Components can be customized for many different applications. This guide covers some of the most common ways they can be used to sell online, and includes ready-to-use examples that you can adapt to your own storefront. To learn more, see the component documentation.
Add a button that redirects the customer to a Shopify-hosted checkout page. This is useful when you want to sell a single product.
To create a "Buy now" button:
Add a
button
component inside a shopify-context component that's associated with a product.Call the
method in the
button
component'sonclick
attribute.- The method needs an event object where the event target must be inside a product context component.
- The component will redirect the customer to the checkout page with the selected product.
Customize the state and location of the checkout page:
- Add discount codes by passing them in the options parameter:
- Set the target window or tab for the checkout page:
{target: '_blank'}
- Combine both:
- Add discount codes by passing them in the options parameter:
Configure the button state with the
shopify-attr--disabled
attribute. You can use this to automatically disable the button when the product variant is not available for sale.
You can mix "Buy now" buttons from different stores on the same page. Each button will open the checkout page of its own store.
Buy now button example
Anchor to add-to-cartAdd to cart button
Add a button that lets customers add your products to their cart. This is useful when you want to sell multiple products, because it lets customers add products to their cart without being redirected from your site to the checkout page.
To create an "Add to cart" button:
Add a
button
component inside a shopify-context component that's associated with a product.Call the
method in the
button
component'sonclick
attribute to add the product to the customer's cart.- This method requires an event object or a product data object.
- If using an event, the event target must be inside a product context component.
Display the cart using a native HTML
<dialog>
element.- To show it as a popup modal, call the
method.
- To show it as a popup modal, call the
Customize the cart's styling and content with CSS parts and slots. View examples
The cart component doesn't support mixing products from multiple stores.
Add to cart example
Anchor to product-details-dialogProduct details dialog
Add a button that lets customers view details about a product on a separate dialog. This is useful if you have limited space on your page, but still want to provide a way for customers to view product details.
To create a "View details" button":Add an HTML
<dialog>
element to your page. Inside the dialog, place a product shopify-context component with await-for-update
attribute. Thewait-for-update
attribute prevents the dialog from loading product details until a specific product is selected. Include a template and components within the product context to display the product information.Add a
button
element inside another shopify-context component that's associated with a product.Add a click event listener to the button that calls the update method on the product context inside the dialog. This links the dialog's product context (destination) with the product context nearest to the button (source), allowing the dialog to display details for the selected product.