For the complete documentation index, see llms.txt. This page is also available as Markdown.

Custom Add to cart button

Use this guide when you create a custom Add to cart button, custom Liquid block, or custom JavaScript that needs to add a product to the cart and open Ascent's native cart drawer.

The safest method is to reuse Ascent's built-in <product-form> element. It already handles loading states, errors, cart bubble updates, cart drawer updates, and opening the drawer.

<script src="{{ 'product-form.js' | asset_url }}" defer="defer"></script>

<product-form class="product-form" data-section="{{ section.id }}">
  <div
    id="Product-Form-Error-Message-{{ section.id }}"
    class="alert-message alert-error mb-2"
    role="alert"
    aria-live="assertive"
    hidden
  ></div>

  {% form 'product', product, class: 'form', novalidate: 'novalidate', data-type: 'add-to-cart-form' %}
    <input type="hidden" name="id" value="{{ product.selected_or_first_available_variant.id }}">
    <input type="hidden" name="quantity" value="1">

    <button type="submit" name="add" class="button button--primary">
      Add to cart
    </button>
  {% endform %}
</product-form>

For this method to open the drawer, the store cart type should be set to drawer and the cart drawer should be present in the theme.

Custom JavaScript method

If you need to add to cart from a fully custom script, send the required sections with the Ajax cart request, update the returned sections, then open the drawer.

Replace 1234567890 with the variant ID you want to add.

Notes

  • Use a variant ID, not a product ID.

  • Include sections_url so Shopify returns section HTML for the current page.

  • Do not open the drawer on the cart page; Ascent silences the drawer there with data-status-silence.

  • If you only need a normal product add-to-cart button, prefer the built-in <product-form> method.

Last updated