This JavaScript code creates a smart cart restriction system for Shopify stores that enforces a minimum order value ($150) when certain specified products are in the cart. Here’s a complete explanation of how it works and how to implement it.
What This Script Does
- Product Specific Minimum Order: When any of the specified products are in the cart, the customer must meet a $150 minimum order to checkout.
- Automatic Detection: The script automatically detects when restricted products are added to cart.
- Visual Feedback:
- Disables checkout buttons when conditions aren’t met
- Shows clear error messages
- Works on both the cart page and the mini-cart/drawer
- Real-time Updates: Responds immediately when cart contents change
Where to Add This Code?
Go to Shopify Admin → Online Store → Themes → Edit Code → theme.liquid
Paste this entire script just before the closing </body> tag.
Customization Needed
- Replace
specialProductIdswith your actual Shopify product IDs. - Change
minAmountif you want a different minimum (value is in cents, so15000 = $150).
<script>
document.addEventListener('DOMContentLoaded', function() {
// STEP 1: LIST YOUR PRODUCT IDs & MINIMUM AMOUNT (in cents)
const specialProductIds = [8267144626348, 8267123425452, 8267123065004, 8267122376876, 8267122049196, 8267121426604, 8267120443564]; // Replace with your product IDs
const minAmount = 15000; // $150.00 (15000 cents)
// STEP 2: MAIN FUNCTION TO CHECK CART & APPLY RULES
function applyCartRestrictions() {
fetch('/cart.js')
.then(response => response.json())
.then(cart => {
const hasSpecialProduct = cart.items.some(item => specialProductIds.includes(item.product_id));
const meetsMinimum = cart.total_price >= minAmount;
// Function to disable/enable checkout buttons
const handleCheckoutButton = (button, errorContainer, errorMessage, isMiniCart = false) => {
if (!button) return;
const existingError = errorContainer.querySelector('.custom-cart-error');
if (hasSpecialProduct && !meetsMinimum) {
// Condition FAILED: Disable checkout & show error
if (isMiniCart) {
button.style.pointerEvents = 'none';
} else {
button.disabled = true;
}
button.style.opacity = '0.5';
button.style.cursor = 'not-allowed';
if (!existingError) {
const errorMsg = document.createElement('p');
errorMsg.className = 'custom-cart-error';
errorMsg.style.color = 'red';
errorMsg.style.textAlign = 'center';
errorMsg.style.margin = '10px 0';
errorMsg.textContent = errorMessage;
errorContainer.insertBefore(errorMsg, button);
}
} else {
// Condition PASSED: Enable checkout & remove error
if (isMiniCart) {
button.style.pointerEvents = '';
} else {
button.disabled = false;
}
button.style.opacity = '';
button.style.cursor = '';
if (existingError) {
existingError.remove();
}
}
};
// CHECK CART PAGE CHECKOUT BUTTON
if (window.location.pathname.includes('/cart')) {
const checkoutBtn = document.querySelector('form[action="/cart"] [name="checkout"], form.cart [name="checkout"]');
if(checkoutBtn) {
handleCheckoutButton(checkoutBtn, checkoutBtn.parentNode, 'Minimum order for these products is $150.');
}
}
// CHECK MINI-CART / DRAWER CHECKOUT BUTTON
const cartDrawer = document.querySelector('.minicart-inner-content'); // Adjust if your theme uses a different class
if (cartDrawer) {
const miniCartCheckout = cartDrawer.querySelector('a[href="/checkout"], [name="checkout"]');
const errorContainer = miniCartCheckout ? miniCartCheckout.parentElement : cartDrawer.querySelector('.drawer__footer');
if(miniCartCheckout && errorContainer){
handleCheckoutButton(miniCartCheckout, errorContainer, 'Minimum $150 required.', true);
}
}
});
}
// WATCH FOR CART CHANGES (FOR DYNAMIC UPDATES)
function setupDrawerObserver() {
const drawerObserverTarget = document.querySelector('.minicart-inner-content'); // Adjust if needed
if (!drawerObserverTarget) return;
const observer = new MutationObserver(() => {
setTimeout(applyCartRestrictions, 50);
});
observer.observe(drawerObserverTarget, { childList: true, subtree: true });
}
// RUN THE SCRIPT
applyCartRestrictions(); // Check on page load
setupDrawerObserver(); // Watch for cart changes
document.addEventListener('cart:updated', applyCartRestrictions); // Shopify cart update event
});
</script>
Want to increase order values in your Shopify store? This custom script enforces a minimum order amount ($150 or more) when specific products are in the cart. It automatically blocks checkout if the condition isn’t met and shows a clear error message, encouraging customers to add more items.
The system works invisibly in the background until needed, then provides crystal-clear guidance to shoppers. Unlike app solutions, it won’t slow down your store or create subscription fees. The customizable error message lets you maintain brand voice while enforcing policies. For seasonal campaigns, simply adjust the minimum amount or product list as needed.
Key Benefits
No expensive apps needed – Lightweight code solution
Works on cart page + mini-cart drawer
Real-time validation – Updates instantly when cart changes
Customizable – Set your own product IDs and minimum amount
