---
title: Common patterns
description: 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](/docs/api/storefront-web-components/components/shopify-cart).
api_name: storefront-web-components
source_url:
html: https://shopify.dev/docs/api/storefront-web-components/common-patterns
md: https://shopify.dev/docs/api/storefront-web-components/common-patterns.md
---
# 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](https://shopify.dev/docs/api/storefront-web-components/components/shopify-cart).
***
## Buy now button
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:
1. Add a `button` component inside a [shopify-context component](https://shopify.dev/docs/api/storefront-web-components/components/shopify-context) that's associated with a product.
2. Call the `buyNow()` method in the `button` component's `onclick` attribute.
* The method needs an event object where the event target must be inside a product [context component](https://shopify.dev/docs/api/storefront-web-components/components/shopify-context).
* The component will redirect the customer to the checkout page with the selected product.
3. Customize the state and location of the checkout page:
* Add discount codes by passing them in the options parameter: `{discountCodes: ['CODE1', 'CODE2']}`
* Set the target window or tab for the checkout page: `{target: '_blank'}`
* Combine both: `{discountCodes: ['SAVE10'], target: '_blank'}`
4. 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.
Note
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
Add a button that redirects the customer to a Shopify-hosted checkout page.
Include discount codes
Add discount codes to the checkout. This applies the discount codes automatically when the customer reaches checkout.
Sell from multiple stores
Use "Buy now" buttons from multiple stores on the same page. Each button will open the checkout page of its own store.
Set a custom target window
Choose where the checkout page opens by setting the target as a new tab or the same tab. You can also specify discount codes that will be applied to the cart.
### Examples
* #### Buy now button example
##### Default
```html
```
## Add 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:
1. Add a `button` component inside a [shopify-context component](https://shopify.dev/docs/api/storefront-web-components/components/shopify-context) that's associated with a product.
2. Call the `addLine()` method in the `button` component's `onclick` 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](https://shopify.dev/docs/api/storefront-web-components/components/shopify-context).
3. Display the cart using a native [HTML `