Shopify Cart Drawer Recommendations: Add Upsells With Liquid and the Section Rendering API
Add product recommendations and upsells to a Shopify cart drawer with Liquid, the recommendations endpoint and section rendering, with code you can adapt.
Share with Shopify developers — useful guides spread faster in theme dev communities.
What you are building
A cart drawer that shows a few related products under the cart items, lets shoppers add one with a single click, and updates the drawer without reloading the page. You need three parts: a drawer section, a recommendations source, and an add-to-cart request that returns fresh drawer HTML.
1. Make the drawer a section
Render the drawer from a section (for example sections/cart-drawer.liquid), so the Section Rendering API can return it as HTML.
<div id="cart-drawer" class="cart-drawer">
{% for item in cart.items %}
<div class="cart-item">
<a href="{{ item.url }}">{{ item.product.title | escape }}</a>
<span>{{ item.final_line_price | money }}</span>
</div>
{% endfor %}
<p>{{ cart.total_price | money }}</p>
</div>
2. Choose a recommendations source
- Manual: a
product_listsetting in the section schema, so merchants pick upsell products. Simple and predictable. - Automatic: the product recommendations endpoint,
/recommendations/products?product_id=ID&limit=4&intent=related, which returns related products for a product.
For manual picks, loop the setting and skip anything already in the cart:
{% for product in section.settings.upsells %}
{% assign in_cart = false %}
{% for item in cart.items %}
{% if item.product_id == product.id %}{% assign in_cart = true %}{% endif %}
{% endfor %}
{% if in_cart == false and product.available %}
<div class="upsell">
{{ product.featured_image | image_url: width: 200 | image_tag: loading: 'lazy' }}
<span>{{ product.title | escape }}</span>
<button data-variant-id="{{ product.selected_or_first_available_variant.id }}">Add</button>
</div>
{% endif %}
{% endfor %}
3. Add to cart and refresh the drawer in one request
The cart add request can ask Shopify to return rendered section HTML in the same response, so you do not need a second fetch.
async function addUpsell(variantId) {
const res = await fetch(window.Shopify.routes.root + 'cart/add.js', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
items: [{ id: variantId, quantity: 1 }],
sections: 'cart-drawer',
}),
});
const data = await res.json();
const html = data.sections['cart-drawer'];
const doc = new DOMParser().parseFromString(html, 'text/html');
document.querySelector('#cart-drawer').innerHTML =
doc.querySelector('#cart-drawer').innerHTML;
}
Use the section file name as the key. If the request fails, show a message instead of failing silently.
Make it fast and accurate
- Limit upsells to three or four, and lazy-load their images.
- Hide sold-out products with
product.available. - Use
routes.root-based URLs so the drawer works on every market and language. - Disable the button while the request runs, to avoid double adds.
- Re-read
cart.item_countafter every change so the header count stays correct.
Test checklist
- Empty cart, one item, many items.
- Out-of-stock and already-in-cart upsells are hidden.
- Add, change quantity and remove, then check totals and the header count.
- Mobile layout and keyboard focus inside the drawer.
Takeaway
Keep the drawer in its own section, pick upsells with a schema setting or the recommendations endpoint, and use sections in the cart request to refresh the drawer in one call. For debugging stale counts and app conflicts, read Shopify Cart Drawer Liquid Implementation Guide, and use the schema generator to build the settings.
Topic cluster
Shopify Sections
OS 2.0 sections, JSON templates, and section architecture.
Convert your HTML to Shopify Liquid
Paste a module, generate Liquid + schema, then copy the result into your theme. Free to try—no signup required for a sample conversion.
Share
Share this guide
Found this guide useful? Share it with other Shopify developers on LinkedIn, X, or Reddit.