Auto-apply a discount code on add-to-cart
Apply a discount code automatically when a shopper adds a specific product to the cart from its product page.
Last updated
You can auto-apply a discount code on add-to-cart: when a shopper adds a specific product from its product page, EliteCart adds the code to the cart, with no code to type. This suits free samples, product-specific promotions and test products.
Set it up
Create the product metafield
In your Shopify admin, go to Settings → Custom data → Products and click Add definition. Set Namespace and key to exactly
custom.elitecart_auto_apply_discount_code, the type to Single line text and One value. Save. You can name itelitecart_auto_apply_discount_code.Create the discount code
In Discounts, create a discount code with the amount, eligible products and usage limits you want, and note the code.
Add the code to the product
Open the product that should trigger the discount, find the elitecart_auto_apply_discount_code metafield and enter the code, for example
TESTPRODUCT100. Save the product.Test it
Open the product page in your store, add the product to the cart and check that the discount shows in EliteCart and at checkout.

How it works
- The code is applied after the shopper adds that product from its own product page. Adding it from a collection page, as an upsell, addon or gift doesn't apply the code.
- If the code is already applied, nothing changes. Codes already in the cart are kept.
- Each product holds one code. Different products can hold different codes.
- Whether several codes work together is decided by your discounts' combination settings in Shopify.
Examples
Free samples. Create the code FREE_SAMPLE with 100% off, limited to the sample product, and use the discount's own settings to limit how often it can be used. Add the code to the sample product's metafield.
Product-specific promotions. Create a time-limited code and add it to the featured products. Shoppers get the discount without a banner or popup.
Troubleshooting
- The code isn't applied. Check that the namespace and key are exactly
custom.elitecart_auto_apply_discount_code, that the discount is active, and that you added the product from its product page. - The code shows but gives no discount. Check the discount's eligible products, minimum requirements and combination settings in Shopify.
- The wrong discount applies. Check the metafield value for typos.
Still stuck?
Chat with us and we'll help you get your cart live. Or email contact@elitecart.app.