Redirects the user to Shopify’s hosted checkout page.
Parameters
None
Returns
void
This method redirects the browser. Any code after checkout() will not
execute.
Examples
document.querySelector('.checkout-button').addEventListener('click', () => {
shoppy.cart.checkout()
})
Checkout with validation
document.querySelector('.checkout-button').addEventListener('click', () => {
if (shoppy.cart.count() === 0) {
alert('Your cart is empty!')
return
}
shoppy.cart.checkout()
})
Checkout with confirmation
function proceedToCheckout() {
const count = shoppy.cart.count()
const cart = shoppy.cart.get()
if (
confirm(
`Proceed to checkout with ${count} items (${cart.cost.totalAmount.currencyCode} ${cart.cost.totalAmount.amount})?`
)
) {
shoppy.cart.checkout()
}
}
shoppy.cart.subscribe((cart) => {
const button = document.querySelector('.checkout-button')
button.disabled = !cart || cart.totalQuantity === 0
})
Behavior
- Gets the checkout URL from the current cart
- Redirects the browser using
window.location.href
- User completes checkout on Shopify’s hosted page
After checkout completion, Shopify will redirect the user based on your
store’s settings (typically to an order confirmation page).