A Simple Shopify Cart Chatbot in 30 Lines of JavaScript

Most shoppers don't abandon a cart because of the product. They leave over small doubts about delivery, COD, or a coupon. Here's a tiny bot that answers those on the cart page.

  1. Read the cart

Shopify exposes the live cart as JSON at /cart.js.

javascript
async function describeCart() {
const cart = await (await fetch('/cart.js')).json();
if (!cart.item_count) return 'Your cart is empty.';
const items = cart.items.map(i => ${i.quantity} x ${i.product_title}).join(', ');
return You have ${items}. Total: ₹${cart.total_price / 100}.;
}

Prices are in the smallest currency unit, so divide by 100.

  1. Answer common questions javascript const intents = [ { match: /deliver|arrive/i, reply: 'Delivery takes 3 to 5 business days.' }, { match: /cod|cash/i, reply: 'Cash on Delivery is available.' }, { match: /cart/i, handler: describeCart }, ];

async function answer(text) {
const hit = intents.find(i => i.match.test(text));
if (!hit) return 'Not sure about that. Want to talk to our team?';
return hit.handler ? hit.handler() : hit.reply;
}

Swap in your store's real policies.

  1. Apply a discount code javascript const applyDiscount = code => (location.href = /discount/${encodeURIComponent(code)}?redirect=/cart); Tips Keep replies short and always offer a human handoff. Shopify's checkout is locked down, so put the bot on the cart page. Log questions the bot can't answer and review them weekly. Wrap-up

A useful cart bot is just three things: read the cart, answer the top questions, and apply codes. Start small and let real customer questions guide what you add.