Side Cart Drawer Not Opening or Updating After Add to Cart (Code fix: Advanced)

Modified on Wed, 18 Feb at 9:29 AM

Advanced

Applies to: Shopify themes using AJAX side cart drawers • SellUp On-Page Offers

The Problem


When a customer clicks "Add" on a SellUp on-page upsell offer (e.g. a gift box add-on), one of two things typically goes wrong:


  1. The side cart drawer doesn't open at all. The page does a full form submission or silently adds the item with no visual feedback. The customer has no idea the product was added.
  2. The drawer opens but appears empty. The item has been added to the cart on the server, but the drawer is showing stale HTML from before the addition. A full page refresh reveals the item is actually in the cart.


Why This Happens

Most modern Shopify themes intercept the native product form's submit event, send the request via fetch or XMLHttpRequest to /cart/add.js, then re-render and open the cart drawer using the Shopify Section Rendering API. 


This is how the main "Add to Cart" button works seamlessly with the drawer.

SellUp injects its own <form> element with its own submit button. 


This form is outside the theme's event listener scope, so when it submits:

  • The theme's AJAX interception never fires.
  • The form either does a standard full-page POST (breaking the experience) or SellUp handles it with its own AJAX call that doesn't know how to talk to your theme's cart drawer.
  • Even if the item lands in the cart, the drawer HTML is never refreshed, so it shows empty or stale content.

In short: SellUp's add-to-cart bypasses the theme's cart drawer lifecycle entirely. The item gets added server-side, but the front-end drawer is never told to update and open.


The Solution


We intercept clicks on SellUp's add-to-cart button, prevent the default form behaviour, manually post to /cart/add.js, fetch the freshly rendered cart drawer HTML via the Section Rendering API, inject it into the DOM, and then open the drawer.

Implementation Steps

1
Create the script file
In your Shopify admin, go to Online Store → Themes → Edit Code. Under the Assets folder, click Add a new asset and create a file called sellup-cart-drawer.js.
2
Paste the following code into the file:
document.addEventListener('DOMContentLoaded', function() {
  document.addEventListener('click', function(e) {
    var sellupBtn = e.target.closest('.launchtip_add_to_cart');
    if (!sellupBtn) return;

    e.preventDefault();
    e.stopPropagation();

    var form = sellupBtn.closest('form');
    if (!form) return;

    var formData = new FormData(form);

    // Step 1: Add the item to the cart via AJAX
    fetch('/cart/add.js', {
      method: 'POST',
      body: formData
    })
    .then(function(res) { return res.json(); })
    .then(function() {
      // Step 2: Fetch the updated cart drawer HTML
      return fetch('/?sections=cart-drawer');
    })
    .then(function(res) { return res.json(); })
    .then(function(sections) {
      var cartDrawerHTML = sections['cart-drawer'];
      if (cartDrawerHTML) {
        var cartDrawer = document.querySelector('cart-drawer');
        if (cartDrawer) {
          // Step 3: Replace drawer contents with fresh HTML
          var parser = new DOMParser();
          var doc = parser.parseFromString(cartDrawerHTML, 'text/html');
          var newDrawerContent = doc.querySelector('cart-drawer');
          if (newDrawerContent) {
            cartDrawer.innerHTML = newDrawerContent.innerHTML;
          }
        }
      }

      // Step 4: Open the drawer
      var drawer = document.querySelector('cart-drawer');
      if (drawer) {
        if (typeof drawer.open === 'function') {
          drawer.open();
        } else {
          var cartTrigger = document.querySelector(
            '[data-cart-toggle], .header__icon--cart a, cart-icon-bubble a'
          );
          if (cartTrigger) cartTrigger.click();
        }
      }
    })
    .catch(function(err) {
      console.error('SellUp cart drawer error:', err);
    });
  }, true);
});
3
Load the script in your product template
Open your product section file (usually sections/main-product.liquid) and add the following line near the bottom, before the closing </section> or {% endschema %}:
<script src="{{ 'sellup-cart-drawer.js' | asset_url }}" defer></script>
4
Save and test. Click the SellUp upsell "Add" button. The side cart drawer should open with the item visible.

Theme-Specific Adjustments


The script above targets the most common Shopify drawer pattern (a <cart-drawer> custom element with a section called cart-drawer). If your theme uses a different structure, you'll need to adjust two things:


What to CheckHow to Find ItWhat to Change
Section nameLook in sections/ for your cart drawer file (e.g. cart-drawer.liquid, mini-cart.liquid, ajax-cart.liquid)Update the fetch URL: /?sections=your-section-name
Drawer elementRight-click the drawer in your browser → Inspect. Note the tag or ID (e.g. <div id="cart-drawer">)Update querySelector('cart-drawer') to match (e.g. querySelector('#cart-drawer'))
Open methodSearch the theme JS for how the drawer is opened (class toggle, custom method, event dispatch)Update the "open the drawer" block accordingly


Important: If SellUp updates their app and changes the class name .launchtip_add_to_cart on their button, the selector in the script will need to be updated to match. If the fix stops working after a SellUp app update, inspect the upsell button to check the current class name.


How It Works (Technical Summary)


The script follows a four-stage process that mirrors what the theme does natively for its own add-to-cart button:


  1. Intercept — A delegated click listener on document catches clicks on any element with the class .launchtip_add_to_cart and prevents the default form submission.
  2. Add to cart — The form data (variant ID, quantity, properties) is posted to /cart/add.js via fetch, ensuring the item is added without a page reload.
  3. Refresh the drawer — A second fetch call hits /?sections=cart-drawer (Shopify's Section Rendering API), which returns the fully rendered HTML of the cart drawer section reflecting the updated cart. This HTML is parsed and injected into the existing drawer element in the DOM.
  4. Open the drawer — The drawer's .open() method is called (or the cart icon is programmatically clicked as a fallback), presenting the customer with the updated cart.


No app or theme files are modified. This is a standalone script that layers on top of both the SellUp app and your theme. It can be removed at any time by deleting the asset file and the script tag.


Difficulty: Advanced • Tested with: SellUp On-Page Offers, Shopify 2.0 themes with AJAX cart drawers

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article