Shopify Discounts Not Applying After Customer Login? Here's the Fix!

Hey there, fellow store owners!

Ever noticed a strange hiccup on your Shopify store where a customer logs in, and suddenly, those special discounts you set up just... disappear from their cart? Or rather, they don't appear when they should? It's a frustrating little quirk, and it's one that recently sparked a really helpful discussion in the Shopify community forums. I wanted to dive into it because it’s a common scenario, especially for those of us using customer tags for targeted promotions.

The Mystery of the Missing Discount

The issue, as brilliantly outlined by Mick_Smith in a recent thread titled "Discounts don't apply after customer login," goes something like this: You've got a fantastic discount set up for customers with a particular tag. A customer visits your store, adds items to their cart, and then decides to log into their account. Boom. The discount, which should now apply because they're a tagged customer, is nowhere to be seen on the cart page. Even hitting refresh doesn't fix it!

Mick_Smith pointed out a few key observations that really helped nail down the problem:

  • If the customer logs in first, then adds items, the discount applies perfectly.
  • The discount does eventually show up at the checkout stage, which is a relief, but not ideal for customer confidence on the cart page.
  • The real kicker: if the customer simply alters their cart contents – adds another item, removes one, changes a quantity – the discount magically appears.

This last point was the big clue. It suggested that something about a cart change was triggering the discount evaluation, but a simple login wasn't. Mick_Smith's investigation further revealed that the cart.cart_level_discount_applications Liquid object wasn't being repopulated after a customer logs in. This is crucial because it's what determines which discounts are displayed on the cart page.

Why Does This Happen? The Shopify Cart Update Logic

This is where the community really stepped in. SectionKit, another helpful voice in the thread, explained the underlying mechanism perfectly. Shopify, by design, evaluates customer tag discounts only when the cart is updated. A customer logging into their account, while a significant event for their session, doesn't actually count as a cart update in Shopify's eyes.

So, when a customer logs in, their new customer.id and associated tags become active, but the cart's discount eligibility isn't re-evaluated until an actual change to the cart's contents occurs. That's why adding or removing an item fixes it – it forces Shopify to take another look.

The Elegant Solution: A "Dummy" Cart Update

Knowing the "why" immediately points us to the "how." If logging in doesn't trigger a cart update, we need to manually trigger one. SectionKit suggested a "dummy cart update" after login. Mick_Smith then asked for documentation on how to perform this, and SectionKit pointed to the Shopify Cart API reference, which is always a good place to start for programmatic cart interactions.

But the real hero of the day was oscprofessional, who provided a concise and effective code snippet that does exactly this! This solution leverages the AJAX Cart API to make a minor, imperceptible change to the cart, specifically by updating a cart attribute. This small change is enough to tell Shopify, "Hey, something happened here, re-evaluate those discounts!"

Implementing the Fix: Step-by-Step

Here’s how you can implement this fix on your Shopify store. It involves adding a small piece of JavaScript code to your theme.

Step 1: Access Your Theme Code

  1. From your Shopify admin, go to Online Store > Themes.
  2. Find the theme you want to edit (it's always a good idea to duplicate your theme first for a backup!).
  3. Click Actions > Edit code.

Step 2: Add the Code Snippet

You'll want to add this code to your theme.liquid file, or specifically to your cart template if you prefer. Placing it in theme.liquid ensures it runs globally after login.

  1. In the left sidebar, under Layout, click on theme.liquid.
  2. Scroll down and paste the following code snippet just before the closing tag.
{% if customer and cart.item_count > 0 %}

{% endif %}

Step 3: Understand What the Code Does

Let's break down this snippet:

  • {% if customer and cart.item_count > 0 %}: This Liquid conditional ensures the script only runs if a customer is logged in AND there are items in their cart. No need to run it otherwise.
  • const customerId = "{{ customer.id }}";: It captures the logged-in customer's ID.
  • const alreadyRefreshed = {{ cart.attributes.discounts_refreshed_for | json }} === customerId;: This is a clever part! It checks if a cart attribute called discounts_refreshed_for already holds the current customer's ID. This acts as a flag to prevent the script from running unnecessarily every time the page loads after login. It ensures the "dummy update" only happens once per customer session after login.
  • if (!alreadyRefreshed) { ... }: If the flag isn't set (meaning the cart hasn't been refreshed for this customer's login yet), the script proceeds.
  • await fetch('/cart/update.js', { ... }): This is the core of the "dummy update." It sends a POST request to Shopify's AJAX Cart API (/cart/update.js). The body of the request simply updates a cart attribute (discounts_refreshed_for) with the customerId. This is a harmless change that triggers Shopify's cart re-evaluation.
  • window.location.reload();: After the cart update is sent, the page reloads. This is essential because it allows the Liquid engine to re-render the cart page with the now correctly evaluated cart.cart_level_discount_applications, finally displaying the discount to your customer.

This fix is a fantastic example of the power of the Shopify community. What started as a frustrating bug for Mick_Smith quickly turned into a clear explanation and a robust solution thanks to the collective knowledge of experts like SectionKit and oscprofessional. Implementing this small piece of code can significantly improve the customer experience on your store, ensuring that those hard-earned discounts are visible exactly when your customers expect them. It’s these kinds of subtle improvements that really make a difference in building trust and encouraging conversions.

Share:

Start with the tools

Explore migration tools

See options, compare methods, and pick the path that fits your store.

Explore migration tools