EliteCart has built-in gift functionality through the Reward Bar that handles free gifts automatically — we recommend using that for most use cases. This Custom HTML approach is useful when you need something the built-in system doesn't cover, such as:
Adding a fourth gift beyond the three reward bar tiers
Offering a gift without showing the reward bar at all
Targeting gifts to a specific market (e.g., a gift only for German customers)
Automatically add a free gift product to the cart when a customer is shopping from a specific market (e.g., Germany). The gift is added once, enforced to a quantity of 1, and automatically removed if the customer switches to a different market, empties their cart, or if the gift ends up as the only item in the cart.
The script listens for cart changes using window.EliteCart.onCartChange and manages the gift product based on the customer's current Shopify market:
Market detection — checks window.Shopify.country to determine if the customer is in the target market
Auto-add — when a customer in the target market adds their first product, the gift is automatically added alongside it
Quantity enforcement — if the customer increases the gift quantity, it resets back to 1
Session memory — if the customer manually removes the gift, the script remembers this for the session and won't re-add it (clearing the cart resets this)
Market-aware cleanup — if the customer switches away from the target market, the gift is automatically removed from their cart
Lone gift removal — if all other products are removed and only the gift remains, it's removed too
Go to EliteCart -> Cart Designer -> Custom HTML
Enable the "Above cart" section (or any Custom HTML section)
Paste the following code
Replace the AUTO_VARIANT_ID with the variant ID of your gift product
<script>
(function () {
var AUTO_VARIANT_ID = 50721950335255;
var QUANTITY = 1;
var SESSION_KEY = 'elite_auto_removed_' + AUTO_VARIANT_ID;
var isAdding = false;
var hasAdded = false;
var wasInCart = false;
var initialized = false;
console.log('[AutoAdd] Script loaded. Target variant:', AUTO_VARIANT_ID);
function isGermanMarket() {
return (window.Shopify.country || '').toUpperCase() === 'DE';
}
function findAutoAddItem(cart) {
if (!cart || !cart.items) return null;
return cart.items.find(function (item) {
return String(item.variant_id) === String(AUTO_VARIANT_ID);
}) || null;
}
function addAutoProduct() {
if (isAdding) return;
isAdding = true;
var root = window.Shopify.routes.root;
fetch(root + 'cart/add.js?elitecart=1', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ items: [{ id: AUTO_VARIANT_ID, quantity: QUANTITY, properties: { __elite_custom_gift: 'true' } }] })
})
.then(function (res) {
if (res.ok) {
hasAdded = true;
return window.EliteCart.refreshCart();
}
})
.catch(function (err) { console.error('[AutoAdd] Fetch failed:', err); })
.finally(function () { isAdding = false; });
}
function removeAutoProduct(item) {
var root = window.Shopify.routes.root;
fetch(root + 'cart/change.js?elitecart=1', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id: item.key, quantity: 0 })
})
.then(function () { return window.EliteCart.refreshCart(); })
.catch(function (err) { console.error('[AutoAdd] Gift removal failed:', err); });
}
function enforceQuantity(item) {
if (!item || item.quantity <= QUANTITY) return;
var root = window.Shopify.routes.root;
fetch(root + 'cart/change.js?elitecart=1', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id: item.key, quantity: QUANTITY })
})
.then(function () { return window.EliteCart.refreshCart(); })
.catch(function (err) { console.error('[AutoAdd] Qty enforcement failed:', err); });
}
function handleCartChange(cart) {
var germanMarket = isGermanMarket();
var customerRemovedIt = sessionStorage.getItem(SESSION_KEY) === 'true';
var autoItem = findAutoAddItem(cart);
var currentlyInCart = !!autoItem;
var hasItems = cart && cart.item_count > 0;
// Customer removal detection (target market only)
if (germanMarket && initialized && wasInCart && !currentlyInCart && hasItems && !isAdding && !hasAdded) {
sessionStorage.setItem(SESSION_KEY, 'true');
customerRemovedIt = true;
}
// Empty cart — reset all state
if (!hasItems) {
wasInCart = false;
hasAdded = false;
initialized = true;
return;
}
// Gift is the only item in cart — remove it (any market)
if (currentlyInCart && cart.items.every(function (item) {
return String(item.variant_id) === String(AUTO_VARIANT_ID);
})) {
removeAutoProduct(autoItem);
return;
}
// Wrong market — gift shouldn't be here, remove it
if (!germanMarket && currentlyInCart) {
removeAutoProduct(autoItem);
return;
}
// Non-target market with no gift — nothing to do
if (!germanMarket) return;
// === Target market logic ===
wasInCart = currentlyInCart;
initialized = true;
if (hasItems && !currentlyInCart && !customerRemovedIt && !isAdding && !hasAdded) {
addAutoProduct();
} else if (currentlyInCart) {
enforceQuantity(autoItem);
}
}
var currentCart = window.EliteCart.getCurrentCart();
if (currentCart) {
wasInCart = !!findAutoAddItem(currentCart);
initialized = true;
handleCartChange(currentCart);
}
window.EliteCart.onCartChange(handleCartChange);
})();
</script>Add these tags to your gift product in Shopify admin (Products -> [your gift product] -> Tags) so it behaves correctly in the cart:
_elite_hide_quantity_selector — hides the quantity buttons on the gift line item, preventing customers from changing the quantity manually
_elite_exclude_from_reward_calculation — excludes the gift's price from the reward bar total, so it doesn't count towards free shipping or other reward thresholds
Use our integrated AI to adapt this script for your needs. Some ideas:
Change the target market — swap 'DE' for any other country code (e.g., 'US', 'FR', 'GB') or target multiple markets at once
Cart value threshold — only add the gift when the cart total exceeds a certain amount
Add multiple gifts — auto-add different gift products for different markets
Custom messaging — combine with a Custom HTML section above or below the cart to show a banner like "Free gift included!" when the gift is in the cart
The __elite_custom_gift line item property marks the gift so it can be identified by other EliteCart features (e.g., cart transform for $0 pricing on Shopify Plus).
To find a product's variant ID, go to the product in Shopify admin, click the variant, and copy the numeric ID from the URL.
The script uses sessionStorage to remember if the customer removed the gift, so the memory resets when they close the tab.
All cart operations include ?elitecart=1 in the URL so EliteCart can distinguish its own requests from other scripts on the page.
Need help adapting this? Use our integrated AI to adjust the target market, add conditions, or customize the behavior.