Skip to main content
shoppy.cart.checkout()
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

Checkout button

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()
    }
}

Disable button if cart empty

shoppy.cart.subscribe((cart) => {
    const button = document.querySelector('.checkout-button')
    button.disabled = !cart || cart.totalQuantity === 0
})

Behavior

  1. Gets the checkout URL from the current cart
  2. Redirects the browser using window.location.href
  3. 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).