# Custom JavaScript events

{% hint style="warning" %}
**We do not support code customizations**. This guide offers a basic reference for advanced users familiar with HTML and CSS. Always test code changes on a duplicated, unpublished version of your theme. For assistance, we recommend finding a professional developer.
{% endhint %}

For Lorenza, we've introduced **custom events** so developers can easily add their own functionality to the theme without needing to edit the theme's JavaScript files.

Use the **`custom-events.js`** file included in the theme to add all your custom scripts in one place and effortlessly hook into Lorenza's JavaScript.

{% hint style="info" %}
For intermediate developers, Lorenza also includes an unminified `theme.js` file that can be enabled to [edit the theme JavaScript](/lorenza/for-developers/edit-theme-javascript.md) directly.
{% endhint %}

<h2 align="center">Enable custom events</h2>

To enable custom events, you need to update the **`theme-globals.liquid`** to import the **`custom-events.js`** file that you'll be using for your scripts.

<details>

<summary><mark style="color:blue;"><strong>STEPS</strong></mark></summary>

1. Open the **`theme-globals.liquid`** file in the **Snippets** folder.
2. Find the **`useCustomevents`** variable and set it to **`true`**.

   ```jsx


   {% assign useCustomEvents = false %}
   {% if useCustomEvents %}
     <script src="{{ 'custom-events.js' | asset_url }}" ></script>
   {% endif %}


   ```

</details>

<h2 align="center"><strong>Add scripts to custom-events.js</strong></h2>

Once you've enabled custom events, you can start adding your custom scripts to the **`custom-events.js`** file.

<details>

<summary><mark style="color:blue;"><strong>STEPS</strong></mark></summary>

1. Open the **`custom-events.js`** in the **Assets** folder
2. Add your scripts using the available exposed events or start from scratch and use this file to create something new.

</details>

<h2 align="center"><strong>Use exposed events</strong></h2>

We've listed all exposed events within the **`custom-events.js`** file so you can easily use and edit them there.

The following **exposed events** are available to use as a blueprint to get started:

### Item added to cart <a href="#item-added-to-cart" id="item-added-to-cart"></a>

This event fires when an item is added to the cart. The AJAX cart must be enabled for this event to be exposed. The product object is passed within the detail object.

{% code overflow="wrap" %}

```jsx
document.addEventListener('flu:cart:item-added', function (evt) {
  console.log('Item added to the cart', evt.detail.product);
});
```

{% endcode %}

### Cart updated <a href="#cart-updated" id="cart-updated"></a>

This event fires when the cart has been updated. The AJAX cart must be enabled for the event to be exposed. The cart object is passed within the detail object.

{% code overflow="wrap" %}

```jsx
document.addEventListener('flu:cart:updated', function (evt) {
  console.log('Cart updated', evt.detail.cart);
});
```

{% endcode %}

### Cart error <a href="#cart-error" id="cart-error"></a>

This event fires when an error occurs with adding an item to the cart. This error typically occurs when a product is unavailable due to insufficient stock. The error message is passed within the detail object.

{% code overflow="wrap" %}

```jsx
document.addEventListener('flu:cart:error', function (evt) {
  console.log(evt.detail.errorMessage, 'Cart error');
});
```

{% endcode %}

### Quick cart opened <a href="#quick-cart-opened" id="quick-cart-opened"></a>

This event fires when the quick cart is opened. The AJAX cart must be enabled for the event to be exposed. The cart object is passed within the detail object.

{% code overflow="wrap" %}

```jsx
document.addEventListener('flu:quick-cart:open', function (evt) {
  console.log('Quick cart opened', evt.detail.cart);
});
```

{% endcode %}

### Quick cart closed <a href="#quick-cart-closed" id="quick-cart-closed"></a>

This event fires when the quick cart is closed. The AJAX cart must be enabled for the event to be exposed.

{% code overflow="wrap" %}

```jsx
document.addEventListener('flu:quick-cart:close', function () {
  console.log('Quick cart closed');
});
```

{% endcode %}

### Product variant changed <a href="#product-variant-changed" id="product-variant-changed"></a>

This event fires when an a variant product is selected. A **Variant selector** block must be enabled on the product template or featured product section for the event to be exposed. The selected variant object is passed within the detail object.

{% code overflow="wrap" %}

```jsx
document.addEventListener('flu:product:variant-change', function (evt) {
  console.log('Product variant changed', evt.detail.variant);
});
```

{% endcode %}

### Product quantity updated <a href="#product-quantity-updated" id="product-quantity-updated"></a>

This event fires when the product quantity is updated. A **Quantity selector** block must be enabled on the product template or featured product section for the event to be exposed. The quantity and selected variant objects are passed within the detail object.

{% code overflow="wrap" %}

```jsx
document.addEventListener('flu:product:quantity-update', function (evt) {
  console.log(
    'Product quantity updated',
    evt.detail.quantity,
    evt.detail.variant
  );
});
```

{% endcode %}

### Quick view modal loaded

This event fires when the Quick view modal is opened. The '**Enable quick shop**' feature (Theme settings > Cart) must be enabled for the event to be exposed.

```jsx
document.addEventListener("quickview:loaded", function () {
  console.log("Quickview loaded");
});

```

<h2 align="center">Refresh quick cart and cart count</h2>

The following event can be used to refresh the quick cart and header cart count:

`apps:product-added-to-cart`

For example:

```javascript
var event = new CustomEvent('apps:product-added-to-cart');
document.dispatchEvent(event);
```

> **How's Lorenza working for you?**
>
> We built Lorenza with developers in mind. We're looking to create relationships with third-party developers using our themes and want to hear from you about what you're building. Please feel free to reach out with questions or introduce yourself by e-mailing us at <developers@fluorescent.co>

> **Related links**
>
> [Edit theme JavaScript](/lorenza/general/editing-themes.md)\
> [Custom Liquid](/lorenza/for-developers/custom-liquid.md)\
> [Custom CSS](/lorenza/for-developers/custom-css.md)\
> [Custom fonts](/lorenza/for-developers/custom-fonts.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.fluorescent.co/lorenza/for-developers/custom-javascript-events.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
