Skip to content

Auto Preferences - Quick Start Guide

What It Does

Automatically sets items to "NO" for customers who keep substituting them out. If a customer subs out an item in the current week AND has subbed it out before, we automatically set that product classification to "NO" in their preferences.

How to Run (Weekly Process)

Run this BEFORE starting a new week:

# 1. Test first (dry run - no changes made)
php command-line-tools.php process-auto-preferences --dry-run

# 2. Review the output to see what will happen

# 3. Run for real (makes changes, sends emails)
php command-line-tools.php process-auto-preferences

What Happens

  1. Finds customers with orders in current week who made substitutions
  2. Checks history - was this item subbed before?
  3. Sets to NO - Updates or creates custy_likes record with pref = 0
  4. Notifies customer:
  5. Sends email with list of items
  6. Creates popup for next login

First Time Setup

1. Run Migration

Visit https://localhost/klcode/kiv/migrate.php to create: - custy_preference_notifications table - Email template "Auto Preferences Updated"

2. Verify Setup

Check tables exist:

SHOW TABLES LIKE 'custy_preference_notifications';
SELECT * FROM email_templates WHERE name = 'Auto Preferences Updated';

Files Added

  • /kiv/helpers/helper.autoPreferences.php - Main logic
  • /home/auto-preference-notification.php - Popup display
  • /home/dismiss-auto-pref-notification.php - AJAX handler
  • Command added to /command-line-tools.php
  • Include added to /home/welcome.php
  • Migrations added to /kiv/migrate.php

Command Options

# Dry run (no changes)
php command-line-tools.php process-auto-preferences --dry-run

# Full run
php command-line-tools.php process-auto-preferences

# Test single customer
php command-line-tools.php process-auto-preferences --dry-run --customer 12345

# Test single customer for real
php command-line-tools.php process-auto-preferences --customer 12345

Example Output

=== Auto Preferences Processor ===

Processing for week ID: 456

Found 5 customers with substitutions this week

Processing customer ID: 12345
  Item 'Organic Bananas' (classify_id: 78) subbed 2 time(s) before
    ✓ Created new preference set to NO
  ✓ Email sent
  ✓ Popup notification created

=== SUMMARY ===
Customers processed: 5
Customers with changes: 1
Total preferences set to NO: 1
Emails sent: 1

What Customers See

1. Email

Subject: "We updated your preferences based on your substitution history" - Lists items set to NO - Link to preferences page

2. Popup on Login

  • Modal showing items set to NO
  • "Update Preferences" button → goes to preferences page
  • "Got It" button → dismisses
  • Only shows once

Troubleshooting

No customers found? - Check current week has orders with subs - Run dry run to see if detection works

Emails not sending? - Check email template exists - Verify customer emails valid - Check email logs

Popup not showing? - Run migration to create table - Check notification created in database - Verify welcome.php includes notification file

Quick Verification

After running, check database:

-- See preferences that were set to NO
SELECT cl.customer, pc.name, cl.pref
FROM custy_likes cl
JOIN product_classification pc ON cl.product_class = pc.id
WHERE cl.pref = 0
ORDER BY cl.id DESC
LIMIT 10;

-- See notifications created
SELECT * FROM custy_preference_notifications
ORDER BY created_at DESC
LIMIT 10;

Database Tables Used

  • Read: cust_order, cust_order_contents, menu, week, custy, email_templates, product_classification
  • Write: custy_likes, custy_preference_notifications, emails (via email helper)

Important Notes

  1. Timing: Run BEFORE starting new week
  2. Testing: Always dry-run first to see impact
  3. Customer Control: Customers can change preferences back if desired
  4. No Undo: Once run (not dry-run), changes are made - but customers can manually change back
  5. Email Volume: If many customers affected, consider batching or reviewing before sending

Integration Points

  • Uses existing custy_likes table (customer preferences)
  • Uses existing email system (sendEmailPHPMailer)
  • Uses existing email templates system
  • Follows existing command-line-tools pattern
  • Integrates with customer portal (welcome page)