# Add a product to the cart with JavaScript

URL: https://shoptet.dev/templates/add-to-cart-javascript

The function `shoptet.cartShared.addToCart` adds a product to the cart the same way the template does when a customer clicks the **Add to cart** button: it sends the request, updates the cart and fires the cart events.

```javascript
shoptet.cartShared.addToCart(payload, silent);
```

- `payload` – an object that identifies the item and, optionally, the amount (see below)
- `silent` – optional; `true` adds the item without showing the loading spinner and the advanced order window
The function is available in all 3rd generation templates (Techno, Tango, Waltz, Classic, Step, Disco and Samba) and in the 2nd generation templates.

You can identify the item in three ways: by its price ID, by its product code, or by the product ID together with parameter values. The IDs in the examples below are placeholders; use the IDs from your e-shop.

## priceId

```javascript
shoptet.cartShared.addToCart({priceId: 1745});
```

`priceId` is the unique ID of the product or variant in the price list. It is in the HTML of the page as a hidden input `<input type="hidden" name="priceId">` , for example in the product detail form and in the product forms in listings.

## productCode

```javascript
shoptet.cartShared.addToCart({productCode: '183/GSB'});
```

`productCode` is the unique code of a product or variant. It can be displayed in the template, it is in the HTML (for example in the `data-micro-sku` attribute, see [Structured data](/templates/structured-data/) ), and the Shoptet API returns it as `code` .

## productId and parameter values

```javascript
shoptet.cartShared.addToCart({productId: 183, parameterValueId: {78: 210, 10: 204}});
```

Use this way if you know the parameters the variants are made of and the values you want. Pass the `productId` of the parent product and the IDs of the selected parameter values in `parameterValueId` . The variant is matched by the parameter value IDs (the values of the object); the keys only tell the parameters apart. The product detail form itself numbers them by position: `parameterValueId[0]` , `parameterValueId[1]` , and so on.

If no variant matches the selected values, or not all parameters are selected, the item is not added and the customer sees an error message.

note
If you send more than one of these identifiers, parameter values take precedence over `priceId` , and `priceId` over `productCode` .

## Common options

With all three ways, you can send `amount` to add more than one piece of the product:

```javascript
shoptet.cartShared.addToCart({priceId: 1745, amount: 2});
```

Pass `true` as the second argument ( `silent` ) to add the item without the loading spinner and without opening the advanced order window (the window opens only if the e-shop has it enabled):

```javascript
shoptet.cartShared.addToCart({priceId: 1745}, true);
```

If the product has surcharge parameters with a required value, send the selected values as well, in the same format the product detail form uses ( `surchargeParameterValueId[parameterId] = valueId` ):

```javascript
shoptet.cartShared.addToCart({priceId: 1745, surchargeParameterValueId: {25: 301}});
```

## Reacting to the change

After the cart is updated, the template fires custom events that you can listen to. In 3rd generation templates, a call to `addToCart` dispatches them on `document` :

- `ShoptetCartAddCartItem` – the item was added
- `ShoptetCartUpdated` – the cart changed (fired after every cart action)
- `ShoptetDOMCartContentLoaded` – the cart content was reloaded (when the change was made on the cart page or with the cart widget open)
- `ShoptetCartAddCartItemFailed` – fired when `addToCart` is called with an invalid payload (not an object); the function then throws an error

```javascript
document.addEventListener('ShoptetCartAddCartItem', function () {
    // code to be executed after a product is added to the cart
});
```

Like every function registered in Shoptet Developers Tools, the call itself also fires the event `shoptet.cartShared.addToCart` . See [Shoptet Developers Tools](/templates/shoptet-developers-tools/) for these events and for the list of all available events.

## Related functions

The same object holds functions for the other cart actions:

- `shoptet.cartShared.removeFromCart({itemId: '610a403f6a8fa'})` removes an item from the cart, see [How to properly remove a product from the cart with JavaScript](/news/frontend/how-to-properly-remove-a-product-from-the-cart-with-javascript/)
- `shoptet.cartShared.updateQuantityInCart({itemId: '621deef58184c', priceId: 1485, amount: 3})` changes the amount of an item, see [How to properly update quantity in cart with JavaScript](/news/frontend/how-to-properly-update-quantity-in-cart-with-javascript/)
- `shoptet.cartShared.isDeliveryIntoBoxRestricted()` returns a promise that resolves to `true` if the cart cannot be delivered into a parcel box, see [Frontend news from February 25, 2026](/news/frontend/frontend-news-from-february-25-2026/)
The `itemId` of a cart item is in the cart form as a hidden input.
