Can I apply discount codes in the cart using the StoreFront API?

Topic summary

Main issue: applying a discount code directly to the cart via the Storefront API. The original poster also hit a CORS error when calling /api/2021-01/graphql.json from the cart template with jQuery and asked for a workaround.

Key options suggested:

  • Shopify Plus: auto-apply discounts using checkout.js, and use Script Editor to create line-item discounts that appear on the cart page.
  • Non‑Plus: append the discount code as a URL parameter to the checkout so it auto-applies there, and optionally mimic the discount display on the cart page before redirecting.
  • Third‑party/Custom: tools like KartDiscount scrape the checkout to read discounts but can trigger bot-like timeouts. One team built a server‑hosted service to compute discounts (e.g., by tags/collections) and reflect them in the cart UI before applying at checkout.

Unresolved points: no confirmed method to apply a discount to the cart via Storefront API, and no clear solution to the CORS error from front-end jQuery. A linked how-to video was shared but later reported unavailable.

Status: no definitive API-based solution; workarounds depend on plan (Plus vs. non‑Plus) and custom/server-side logic.

Summarized with AI on January 8. AI used: gpt-5.

is it possible to apply discount codes in the cart, using the StoreFront API?

I’ve searched a lot for this, but didn’t find a proper solution. Maybe create a checkout and modify it via the API?

When I try to call /api/2021-01/graphql.json with jQuery (from the cart liquid template), it gives me CORS error. how can I avoid that?

noone?

I just need to apply discount on the cart with a private app/ or something

If your store has a Shopify Plus Plan, you can apply a discount automatically by using javascript in the checkout.js file.

If not, you can add a discount code as a url paramenter. The discount will be automatically applied when the user reaches the checkout.

We’ve tried a couple different solutions (including KartDiscount).

KartDiscount essentially does a call to the checkout page and uses selectors to pull the discounted amount. This works for certain stores, but will eventually time the customer out due to Bot like activity.

We ended up just creating our own solution that hosted on our own server which takes in products & calculates the discount based on however you do discounts (tags/collections)

If you have Shopify plus then you can use the script editor for line item discount which will show up on the cart page. If you don’t have Shopify plus then you can manipulate the cart page to display a discount and have a script that will auto-applied the discount code at the checkout page.

Hi there! Here’s an answer to your question: https://app.hopps.io/videos/how-do-i-apply-a-discount-on-the-cart-page-178663a7e21. If it doesn’t make sense or you still have questions, one of the experts can walk you through the solution.

Looks like we couldn’t find this video