Shopify Development

Shopify Discounts Not Applying After Login? The Essential Cart Page Fix

Hey fellow store owners and Shopify enthusiasts!

You know that feeling when you've meticulously set up a fantastic discount, perhaps one tied to customer tags, only to hear from a customer (or experience yourself) that it's just... not showing up? Specifically, it's those moments when a customer has items in their cart, then logs into their account, and BAM – the discount vanishes from the cart page. It's frustrating, right? You're thinking, "Did I set it up wrong?" "Is my theme broken?"

Well, as experts in Shopify migrations and development at Shopping Cart Mover, we often encounter these nuanced challenges. This exact scenario popped up in a Shopify Community forum discussion started by Mick_Smith titled "Discounts don't apply after customer login." And let me tell you, it's a classic example of how the community comes together to solve those tricky, head-scratching issues that impact customer experience and conversion.

Shopify theme code editor displaying the JavaScript fix for discount re-evaluation and a diagram of the AJAX Cart API process.
Shopify theme code editor displaying the JavaScript fix for discount re-evaluation and a diagram of the AJAX Cart API process.

The Annoying Discount Disappearing Act: When Login Breaks the Cart

Mick_Smith laid out the problem perfectly: "When you set up a discount for customers tagged with a specific tag and they then visit the website and add items to their cart, if then they proceed to log in, the discount does not show on the cart page. Even a page refresh does not solve the issue."

Sound familiar? The key here is the sequence of events: add items to cart FIRST, then log in. If a customer logs in before adding items, everything works as expected. This distinction is crucial for understanding the root cause. And here's the kicker: if the customer then performs any action that alters the cart (like changing a quantity, adding another item, or removing something), the discount suddenly reappears. Even more puzzling, Mick_Smith noted that if the customer just hits the checkout button, the discount does show up at the checkout stage. So, the discount isn't completely gone, it's just playing hide-and-seek on the cart page, particularly with the cart.cart_level_discount_applications Liquid object.

Why Does This Happen? Shopify's Cart Re-evaluation Logic

The core of this issue lies in how Shopify evaluates and applies discounts, especially those tied to customer tags. As SectionKit succinctly explained in the forum, "Shopify evaluates the customer’s tag discount only when the cart is updated. Since the login does not trigger the cart update, a discount is missed."

When a customer logs in, their session changes, and their associated tags become available. However, this login event itself doesn't automatically trigger a re-evaluation of the cart's contents against potential discounts. Shopify's system is designed to re-check discounts primarily when there's an explicit change to the cart's items or attributes. Without such a trigger, the cart page continues to display the cart state *before* the customer's login, thus missing any tag-based discounts.

The Elegant Solution: A "Dummy" Cart Update via AJAX

The good news is that the Shopify community, specifically oscprofessional, provided an elegant and effective solution: perform a "dummy" cart update using the Shopify AJAX Cart API immediately after a customer logs in. This subtle update acts as the necessary trigger for Shopify to re-evaluate the cart and apply any eligible discounts.

Implementing the Fix: Step-by-Step Guide

This solution involves adding a small JavaScript snippet to your Shopify theme. We recommend placing this code in your theme.liquid file, just before the closing tag, or directly within your cart template if you prefer a more localized approach. Always back up your theme before making any code changes!

{% if customer and cart.item_count > 0 %}

{% endif %}

Breaking Down the Code

  • {% if customer and cart.item_count > 0 %}: This Liquid condition ensures the script only runs if a customer is logged in AND there are items in their cart. This prevents unnecessary execution.
  • const customerId = "{{ customer.id }}";: We capture the logged-in customer's ID. This is crucial for the next step.
  • const alreadyRefreshed = {{ cart.attributes.discounts_refreshed_for | json }} === customerId;: This is a clever part of the solution. It checks for a custom cart attribute named discounts_refreshed_for. If this attribute's value matches the current customerId, it means the cart has already been refreshed for this specific customer during their current session, preventing an infinite loop or redundant updates.
  • if (!alreadyRefreshed) { ... }: The core logic executes only if the cart hasn't been refreshed for this customer yet.
  • await fetch('/cart/update.js', { ... }): This is an asynchronous JavaScript call to Shopify's AJAX Cart API.
    • method: 'POST': Specifies the HTTP method.
    • headers: { 'Content-Type': 'application/json' }: Informs the server that we're sending JSON data.
    • body: JSON.stringify({ attributes: { discounts_refreshed_for: customerId } }): This is the "dummy" update. We're not changing product quantities; instead, we're adding or updating a custom cart attribute. This small, non-intrusive change is enough to trigger Shopify's cart re-evaluation process.
  • window.location.reload();: After the cart update is successfully sent, the page is reloaded. This ensures that the Liquid objects (like cart.cart_level_discount_applications) are re-rendered with the newly applied discount information.

Why This Works So Effectively

By updating a seemingly insignificant cart attribute, we're effectively "poking" Shopify's cart system. This action is recognized as a cart modification, prompting the platform to re-run its discount evaluation algorithms. Because the customer is now logged in and their tags are recognized, any applicable tag-based discounts are then correctly identified and applied. The subsequent page reload ensures that the cart page visually reflects these changes, providing a seamless and correct experience for your customers.

Important Considerations and Best Practices

  • Theme Backup: Always, always, always create a duplicate of your live theme before implementing any code changes. This allows you to revert quickly if something goes wrong.
  • Testing: Thoroughly test this fix across different scenarios:
    • Customer adds items, then logs in.
    • Customer logs in, then adds items.
    • Customer logs in, then logs out and logs back in.
    • Test with and without applicable tag-based discounts.
  • Impact on Custom Cart Drawers/AJAX Carts: If your theme uses a highly customized AJAX cart or a cart drawer that doesn't fully reload the page, you might need to adapt the window.location.reload() part or ensure your custom cart logic correctly fetches the updated cart state after the dummy update.
  • Professional Help: If you're uncomfortable with code or encounter unexpected issues, don't hesitate to reach out to Shopify experts. At Shopping Cart Mover, we specialize in not just migrations but also optimizing Shopify stores for performance and user experience.

Conclusion: Enhancing Customer Experience, One Discount at a Time

This "disappearing discount" bug, while frustrating, highlights the intricate dance between customer sessions, cart states, and discount logic in Shopify. Thanks to the collaborative spirit of the Shopify Community, a robust solution is readily available. By implementing this simple AJAX Cart API "dummy update," you ensure that your customer-tagged discounts are always visible and correctly applied on the cart page, enhancing trust and preventing potential abandoned carts due to perceived pricing discrepancies.

A smooth and predictable shopping experience is paramount for conversion. If you're looking to optimize your Shopify store further, or considering a migration to Shopify, remember that Shopping Cart Mover is here to help you navigate the complexities and unlock your store's full potential.

Share:

Use cases

Explore use cases

Agencies, store owners, enterprise — find the migration path that fits.

Explore use cases