# JavaScript helpers

Functions and hooks available on the bundle page, for custom Template Creator templates.

A bundle page exposes a few global functions for custom templates built in [Template Creator](https://admin.shopify.com/apps/bundlebuilder/app/settings/editor). They're available once the bundle has loaded.

:::note
Template Creator needs a working knowledge of Liquid and JavaScript. If you'd rather not write it yourself, a Shopify Expert can.
:::

## Functions

### `canAddVariant()`

```js
window.BundleBuilder.canAddVariant(variantId, sectionIndex)
```

Whether a variant can be added to the bundle right now, taking the bundle's limits and the variant's available quantity into account.

| Parameter | Type | Description |
|---|---|---|
| `variantId` | `string` | The Shopify variant ID. |
| `sectionIndex` | `string` | The index of the section the variant is in, from `0`. |

Returns `boolean`. Logs an error and returns `false` if there is no section at `sectionIndex`.

```js
if (window.BundleBuilder.canAddVariant('44012345678901', '0')) {
  // show the add button
}
```

### `showMoreModal()`

```js
window.BundleBuilder.showMoreModal(variantId, sectionIndex, canSelectVariant, canAdd)
```

Opens the **Read more** dialog for a variant, with all of its product's images and its description.

| Parameter | Type | Description |
|---|---|---|
| `variantId` | `number` | The variant to show. |
| `sectionIndex` | `number` | The index of the section the variant is in, from `0`. |
| `canSelectVariant` | `boolean`, optional | `false` hides the variant pickers. Defaults to `true`. |
| `canAdd` | `boolean`, optional | `false` hides the add button. Defaults to `true`. |

```html
<img
  src="{{ product.featured_image | image_url: width: 400 }}"
  onclick="window.BundleBuilder.showMoreModal({{ variant.id }}, {{ forloop.index0 }})"
>
```

### `srcset()`

```js
window.BundleBuilder.srcset(url)
```

A `srcset` attribute value for an image on Shopify's CDN, at the widths Kitenzo uses. Returns an empty string for any other URL.

```js
img.srcset = window.BundleBuilder.srcset(img.src);
```

## After add to cart

By default, adding a bundle to the cart sends the shopper to the cart page. Define `window.bbAfterAddToCart` to do something else instead, for example open your theme's cart drawer.

:::caution
Staying on the bundle page can confuse shoppers: changes they make there don't change the bundle already in their cart. When the hook runs, Kitenzo offers to replace the bundle in the cart if the shopper edits it.
:::

```html title="Bundle page template"
<script>
  window.bbAfterAddToCart = function () {
    // Open your theme's cart drawer here.
    // Return { offerReplace: false } to skip the replace offer.
  };
</script>
```

| Return value | What happens |
|---|---|
| Nothing | The shopper stays on the page, and Kitenzo offers to replace the bundle if they edit it. |
| `{ offerReplace: false }` | The shopper stays on the page, and the bundle page behaves as a plain add to cart. |

To react to an add without changing where the shopper goes, listen for the `kitenzo:addtocart:complete` event instead. It fires on `document` after every successful add, before any redirect.

```js
document.addEventListener('kitenzo:addtocart:complete', () => {
  // e.g. update a cart count in your header
});
```
