How the API Pattern Works
Most Upcart API scripts follow the same simple pattern:Where to Add Your Scripts
All scripts below go in: Upcart Editor → Settings → Custom HTML → Scripts (before load) Wrap each snippet in<script>...</script> tags and save. To test, open your browser’s Dev Tools console (F12) and look for any console.log messages you’ve added.
Example 1: Hide the Sticky Cart Button When the Cart is Empty
A common UX improvement: hide the floating cart button when there’s nothing in the cart, so it doesn’t clutter the page. How it works:window.upcartOnCartLoaded fires every time the cart is loaded. It receives Shopify’s Cart object, which includes item_count- the total number of items in the cart.
upcartOnCartLoadedis called every time the cart loads, passing in the Shopify cart object.- We grab the sticky button element by its ID (
#upCartStickyButton). - If
item_countis0, we hide it. Otherwise, we show it.
Example 2: Log When an Item is Added to the Cart
Useful for debugging, analytics, or triggering custom logic whenever a shopper adds a product.| Parameter | Description |
|---|---|
id | The variant ID of the added product |
quantity | Number of units added |
item | Full Shopify line item object |
Example 3: Integrate with a Third-Party Analytics App (e.g. TripleWhale)
Some apps need a small script to track cart activity through Upcart. Here’s how to fire a TripleWhaleAddToCart pixel event whenever a product is added:
Note: Each third-party app is different. Check with your app’s support team to confirm whether a script is needed and what the correct event format is. Upcart doesn’t provide support for third-party app scripts.
Example 4: Open the Cart Automatically After a Product is Added
Want the cart drawer to open immediately when a shopper adds an item? UseupcartOnAddToCart alongside the window.upcartOpen() function:
Quick Reference: Common API Functions
| Function / Event | When it fires / What it does |
|---|---|
window.upcartOnCartLoaded(cart) | Fires when the cart is loaded; receives cart object |
window.upcartOnAddToCart(id, qty, item) | Fires when an item is added to the cart |
window.upcartOpen() | Programmatically opens the cart drawer |
window.upcartClose() | Programmatically closes the cart drawer |
Troubleshooting
- Script not running? Double-check placement: it should be in Scripts (before load), not after load.
- Element not found? Make sure the selector (e.g.
#upCartStickyButton) matches the actual element ID in your cart. - Something broke? Comment out your script by adding
//to the start of each line, save, and refresh.