How to pass a discount code to cartCreate mutation when using "Buy It Now" button?

Short description of issue

cartCreate Mutation Ignoring Hidden Discount Input

Reproduction steps

input type=“hidden” name=“discount” value=“AXVNX”>

click “buy Now”

Additional info

I’m trying to implement a feature where a discount code is automatically applied when a customer clicks the “Buy It Now” button on the Product Detail Page.

My current approach: I added a hidden input field named discount inside the product form: ‘input type=“hidden” name=“discount” value=“AXVNX”>’

The issue: When clicking “Buy It Now”, the browser executes a cartCreate mutation to api/unstable/graphql.json. However, looking at the Network tab in DevTools, the discountCodes array in the payload remains empty: discountCodes: .

It seems the default Shopify scripts for the “Buy It Now” button (which usually triggers a dynamic checkout) do not pick up additional input fields from the form liquid to include them in the GraphQL mutation.

What I want to achieve: I need the discountCodes variable in the cartCreate mutation to be populated with my value (e.g., [“AXVNX”]) so the discount is applied immediately on the checkout page.

What type of topic is this

Troubleshooting

Upload screenshot(s) of issue


Only the update endpoint of the Cart AJAX API supports adding discounts. When you add a product to the cart it uses the add endpoint. This is why your hidden input doesn’t work.

The dynamic checkout button changes based on a shop’s settings and individual customer’s preferences. When customers see “Buy It Now” instead of an external payment method, you can still apply discounts by using a Checkout UI Extension app.

1 Like