Prompt
I want a recommendation system for a retail site that can do similar items, cart add-ons, and email cross-sells while excluding out-of-stock products.
Latest observation
Here’s a practical recommendation-system design for a retail site that supports:
- similar items
- cart add-ons
- email cross-sells
- excluding out-of-stock products
1) Core idea
Use a hybrid recommender:
- Candidate generation: pull likely products from multiple sources
- Filtering layer: remove out-of-stock and other business-rule violations
- Ranking layer: order the remaining items by relevance for the context
This gives you flexibility for different placements:
- PDP “similar items”
- Cart “add-ons”
- Email “you may also like”
2) Data you’ll need
Product catalog
- product_id
- title
- description
- category / subcategory
- brand
- attributes: color, size, material, etc.
- price
- margin
- image embeddings or text embeddings
- stock status / available inventory
- lifecycle status: active, discontinued, hidden
Behavioral data
- views, clicks, add-to-cart, purchases
- co-occurrence: items often bought together
- session sequences
- user affinities if available
Context data
- page type: PDP, cart, email
- current product(s)
- cart contents
- user segment / lifecycle stage
- inventory and shipping constraints
3) Recommendation types
A. Similar items
Best for product detail pages.
Signals:
- same category/subcategory
- similar embeddings from title/description/images
- similar price band
- similar brand or alternative brands
- high click similarity
Ranking examples:
- cosine similarity on item embeddings
- weighted similarity on structured attributes
- boost items with better availability or conversion rate
Good logic:
- “near substitutes” first
- “same category alternatives” next
- “premium/down-market variants” next
B. Cart add-ons
Best for cart or mini-cart.
Goal:
- increase basket size with complementary products
Signals:
- frequent co-purchase pairs
- category complements
- accessory relationships
- margin and attach rate
- delivery feasibility and stock
Examples:
- headphones → case, cable, adapter
- coffee machine → filters, descaler, pods
- phone → screen protector, charger
Ranking:
- co-purchase strength
- complement category match
- availability
- price compatibility with cart
C. Email cross-sells
Best for post-purchase or browse-abandon emails.
Goal:
- personalized cross-sell based on recent activity or purchase history
Signals:
- purchased items
- viewed items
- similar-category follow-ups
- lifecycle timing
- recipient segment
Examples:
- after buying a camera: memory card, bag, tripod
- after buying running shoes: socks, shorts, hydration belt
Ranking:
- historical purchase affinities
- user preferences
- category expansion
- margin and inventory
4) Excluding out-of-stock products
This should be enforced in the filtering layer, not just the ranking layer.
Hard filters
Exclude products that are:
- out of stock
- backordered, if you don’t want to show them
- inactive/discontinued
- not shippable to user location
- restricted by size/color availability
Optional availability scoring
If you want to be smarter than a binary filter:
- boost high-stock items
- deprioritize low-stock items
- avoid items likely to go out of stock soon
But the simplest rule is:
- never recommend items with zero available inventory
5) Suggested architecture
Step 1: Candidate generation
Generate 50–500 candidates per context from:
- embedding similarity search
- co-purchase graph
- category rules
- editorial/business lists
Step 2: Business-rule filtering
Remove products that are:
- out of stock
- inactive
- invalid for channel or region
- already in cart, if you don’t want duplicates
Step 3: Ranking model
Rank the filtered items using:
- context features
- item features
- user features
- interaction features
A learning-to-rank model like:
- XGBoost / LightGBM
- neural ranker
- two-tower retrieval + ranker
works well.
Step 4: Re-ranking
Apply final constraints:
- diversity across categories/brands
- price range distribution
- no duplicates
- business priorities
6) A good practical scoring formula
For a candidate item i in context c:
score(i, c) =
w1 * similarity_score
+ w2 * co_purchase_score
+ w3 * complement_score
+ w4 * user_affinity_score
+ w5 * margin_score
+ w6 * availability_score
- w7 * duplicate_penalty
Then apply:
if stock <= 0: exclude
if inactive: exclude
if region_not_shippable: exclude
For email cross-sells, replace some of the context weights with:
- recency of purchase
- purchase category transitions
- lifecycle timing
7) Example rules by placement
Similar items on PDP
- Only same or closely related category
- Must be in stock
- Prefer same brand and adjacent price points
- Show 4–8 items
Cart add-ons
- Must be complementary, not substitutes
- Must be in stock
- Avoid items already in cart
- Prefer low-friction add-ons under a threshold price
Email cross-sells
- In stock at send time
- Personalized by recent purchase
- Exclude items already purchased recently
- Optionally create fallback categories if no good matches
8) How to keep recommendations fresh
- refresh inventory in near real-time or hourly
- retrain behavioral models regularly
- update co-purchase stats weekly/daily
- maintain fallback rules when model confidence is low
9) Metrics to track
Offline
- precision@k
- recall@k
- MAP / NDCG
- attach-rate prediction quality
- stock-filter pass rate
Online
- CTR
- add-to-cart rate
- revenue per session/email
- conversion rate
- average order value
- out-of-stock impression rate
- email revenue per recipient
A key guardrail:
- zero or near-zero out-of-stock impressions
10) Best implementation pattern
If you want a strong first version:
- Build item embeddings from title, description, image, category
- Build a co-purchase graph from order history
- Create explicit complement rules for cart/email
- Add a hard inventory filter
- Rank with a simple weighted model
- Test and refine with A/B experiments
11) Recommendation for your use case
I’d suggest this setup:
- Retrieval
- item embeddings for similar items
- co-purchase graph for add-ons and cross-sells
- Filtering
- hard stock exclusion
- region/shipping exclusion
- Ranking
- context-aware learning-to-rank
- Re-ranking
- diversity + business constraints
This will cover all three use cases cleanly.
If you want, I can also provide:
- a system architecture diagram,
- a database/schema design, or
- sample pseudocode / SQL / Python for the recommender.