Auto-apply discount code on add-to-cart
Automatically apply discount codes when specific products are added to cart.
Last updated
Configure products to automatically apply discount codes when customers add them to their cart, perfect for exclusive offers or test products.
Prerequisites
- EliteCart Professional account
- Admin access to create product metafields
Steps
1. Create the product metafield
- Go to Settings → Custom data in your Shopify admin
- Click Products → Add definition
- Configure the metafield exactly as follows:
- Name:
elitecart_auto_apply_discount_code - Namespace and key:
custom.elitecart_auto_apply_discount_code - Type: Single line text
- One value (not list of values)
- Storefront API access: Must be enabled (toggle ON)
- Name:
- Click Save (you can now save the name as well if you like)
Important: The namespace and key (red below) must be exactly custom.elitecart_auto_apply_discount_code for the feature to work.

2. Create your discount code
- Go to Discounts in your Shopify admin
- Create a new manual discount code
- Configure the discount settings (amount, products, usage limits)
- Save the discount code name for the next step
3. Add discount code to products
- Edit the product that should trigger the discount
- Scroll to Metafields section
- Find elitecart_auto_apply_discount_code
- Enter your discount code (e.g.,
TESTPRODUCT100) - Save the product
4. Test the feature
- Visit the product page
- Add the product to cart
- Open the cart drawer
- Verify the discount code is automatically applied
Use cases
Free sample products
Create a 100% discount that applies to specific sample products.
- Create discount code
FREE_SAMPLEwith 100% off - Limit to a specific product
- Add the code to that product’s metafield
The discount is now automatically applied when the product is added to cart and further limits the free samples per order to 1.
Product-specific promotions
Run promotions without banners or popups:
- Create time-limited discount code
- Add to featured products
- Discount applies seamlessly at checkout
How it works
When a customer adds a product to cart:
- EliteCart checks for the discount metafield
- If found, applies the code automatically
- Cart updates with discounted pricing
- No manual code entry required
Important notes
- Only one discount code per product
- Discount applies only when adding from product page
- Existing discount codes are preserved
- Works with all EliteCart features (upsells, rewards, etc.)
Troubleshooting
Discount not applying:
- Verify metafield key is exactly
custom.elitecart_auto_apply_discount_code - Ensure Storefront API access is enabled
- Check discount code is active and valid
- Confirm product page URL matches the product
Wrong discount applied:
- Check for typos in the discount code
- Verify discount settings in Shopify admin
- Test with a different discount code
Multiple products with different codes:
- Each product can have its own discount code
- All valid codes will be applied to cart
- Shopify's discount stacking rules apply
Still stuck?
Chat with us and we'll help you get your cart live. Or email contact@elitecart.app.