Marketing Automation
email marketing
How to Automate Abandoned Cart Email Sequences
Build smart abandoned cart recovery emails that adapt based on cart value.
Jay Banlasan
The AI Systems Guy
I built this to automate abandoned cart email sequences that adapt based on cart value. A $20 cart should not get the same treatment as a $500 cart. This adjusts urgency, offers, and follow-up count automatically.
Value-based abandoned cart sequences recover more revenue than one-size-fits-all approaches.
What You Need Before Starting
- Python 3.8+ with requests
- Anthropic API key
- Ecommerce API (Shopify, WooCommerce)
- ESP with trigger capability
Step 1: Capture Abandoned Carts
import requests
import os
def get_abandoned(shop_url, token):
url = f"https://{shop_url}/admin/api/2024-01/checkouts.json"
headers = {"X-Shopify-Access-Token": token}
checkouts = requests.get(url, headers=headers).json().get("checkouts", [])
return [c for c in checkouts if not c.get("completed_at")]
Step 2: Segment by Value
def segment_cart(cart):
total = float(cart.get("total_price", 0))
if total >= 200: return "high"
if total >= 50: return "medium"
return "low"
CONFIGS = {
"high": {"emails": 5, "offer": "10% off", "timing": [1, 4, 24, 48, 72]},
"medium": {"emails": 3, "offer": "5% off", "timing": [1, 24, 48]},
"low": {"emails": 2, "offer": "free shipping", "timing": [4, 24]},
}
Step 3: Generate Cart Emails
import anthropic
from dotenv import load_dotenv
load_dotenv()
client = anthropic.Anthropic()
def generate_cart_email(cart, segment, email_num):
config = CONFIGS[segment]
items = ", ".join([i["title"] for i in cart.get("line_items", [])])
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=512,
system=f"Abandoned cart email #{email_num}/{config['emails']}. Offer: {config['offer']}. Conversational, not pushy.",
messages=[{"role": "user", "content": f"Items: {items}. Total: ${cart['total_price']}."}]
)
return message.content[0].text
Step 4: Schedule the Sequence
def schedule_sequence(cart, segment):
config = CONFIGS[segment]
for i, hours in enumerate(config["timing"]):
email = generate_cart_email(cart, segment, i + 1)
queue_send(cart["email"], email, delay_hours=hours)
What to Build Next
Add real-time monitoring so the sequence triggers within minutes of abandonment. The first hour has the highest recovery rate.
Related Reading
- Prompt: Write a Product Launch Email Sequence - how AI transforms email marketing operations
- AI for Email Marketing Automation - how AI transforms email marketing operations
- From Overwhelmed to Automated - framework for deciding what to automate first
Want this system built for your business?
Get a free assessment. We will map every system your business needs and show you the ROI.
Get Your Free Assessment