# Cart Abandonment Email Automation

## Overview
This implementation adds automatic cart abandonment reminder emails via MailerLite or Sendpulse. When users add courses to their cart (create pending payment records) but don't complete payment within the configured threshold (default 24 hours), they will receive reminder emails. Admins can select which email service to use via the admin settings page.

## Components

### 1. Console Command
**File:** `app/Console/Commands/CheckCartAbandonment.php`

The command runs every 6 hours and:
- Finds pending payments older than the configured threshold
- Adds users to the selected email service (MailerLite or Sendpulse)
- Includes course details (URL, title, amount) as custom fields
- Tracks trigger time to prevent duplicate emails (48-hour cooldown)
- Loads configuration from admin settings (email service, group/book IDs, threshold)

### 2. Database Migration
**File:** `database/migrations/2025_05_28_200000_add_cart_abandonment_triggered_at_to_payments_table.php`

Adds `cart_abandonment_triggered_at` timestamp column to payments table for tracking when abandonment emails were sent.

### 3. Model Updates
**File:** `app/Models/Payment.php`

Added `cart_abandonment_triggered_at` to fillable and casts arrays.

### 4. Schedule Configuration
**File:** `app/Console/Kernel.php`

Registered the command to run every 6 hours.

### 5. Admin Settings Page
**File:** `resources/views/admin/setting/notification.blade.php`

Added "Cart Abandonment" tab with settings for:
- Email service selection (MailerLite or Sendpulse)
- Sendpulse address book ID
- MailerLite group ID
- Abandonment threshold in hours

## Setup Instructions

### 1. Run Migration
```bash
php artisan migrate
```

### 2. Configure Email Service
Navigate to Admin → Settings → Notifications → Cart Abandonment tab:
- Select email service (MailerLite or Sendpulse)
- Enter the appropriate ID (group ID for MailerLite, address book ID for Sendpulse)
- Set abandonment threshold (default: 24 hours)

### 3. Test the Command Manually
```bash
php artisan cart:check-abandonment
```

### 4. Verify Cron is Running
Ensure the Laravel cron scheduler is configured on your server:
```bash
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
```

## MailerLite Configuration

The system uses the existing MailerLite integration with:
- **API Key:** Set in `.env` as `MAILERLITE_API_KEY`
- **Group ID:** Configurable in admin settings (default: 105499975 "Signed up but haven't paid")

### Custom Fields Sent to MailerLite
- `course_url`: URL to the abandoned course
- `course_title`: Title of the abandoned course
- `course_amount`: Price of the course
- `course_currency`: Currency symbol
- `cart_abandoned_at`: Timestamp when cart was abandoned

## Sendpulse Configuration

The system uses the existing Sendpulse integration with:
- **Client ID:** Set in `.env` as `SENDPULSE_CLIENT_ID`
- **Client Secret:** Set in `.env` as `SENDPULSE_CLIENT_SECRET`
- **Address Book ID:** Configurable in admin settings

### Variables Sent to Sendpulse
- `Name`: User's name
- `course_url`: URL to the abandoned course
- `course_title`: Title of the abandoned course
- `course_amount`: Price of the course
- `course_currency`: Currency symbol
- `cart_abandoned_at`: Timestamp when cart was abandoned

## MailerLite Automation Setup

To set up the actual email automation in MailerLite:

1. Log in to MailerLite
2. Navigate to Automations
3. Create a new automation
4. Set trigger: "Subscriber added to group" → Group configured in admin settings
5. Configure email sequence:
   - Email 1: Send immediately after trigger
   - Email 2: Send 24 hours later (if no purchase)
   - Email 3: Send 48 hours later (if no purchase)
6. Use custom fields in email templates:
   - `{{ course_title }}` - Course name
   - `{{ course_url }}` - Course link
   - `{{ course_amount }}` - Course price
7. Set exit condition: "Subscriber removed from group" (happens when payment is confirmed)

## Sendpulse Automation Setup

To set up the actual email automation in Sendpulse:

1. Log in to Sendpulse
2. Create an address book for cart abandonment (or use existing)
3. Note the address book ID and enter it in admin settings
4. Navigate to Automation360
5. Create a new automation
6. Set trigger: "Contact added to address book" → Address book configured in admin settings
7. Configure email sequence:
   - Email 1: Send immediately after trigger
   - Email 2: Send 24 hours later (if no purchase)
   - Email 3: Send 48 hours later (if no purchase)
8. Use variables in email templates:
   - `{Name}` - User name
   - `{course_title}` - Course name
   - `{course_url}` - Course link
   - `{course_amount}` - Course price
9. Set exit condition: "Contact removed from address book" (requires additional implementation)

## How It Works

1. **User adds course to cart:** Payment record created with status 'pending'
2. **Threshold hours pass:** Cron job runs and finds abandoned carts
3. **User added to email service:** Based on admin selection, user is added to MailerLite group or Sendpulse address book
4. **Automation triggered:** Email sequence begins in the selected service
5. **User completes payment:** `approvePayment()` helper removes user from MailerLite group 105499975 (existing behavior)
6. **Cooldown:** User won't receive another abandonment email for same course for 48 hours

## Monitoring

Check logs for command output:
```bash
php artisan cart:check-abandonment
```

The command outputs:
- Selected email service
- Abandonment threshold
- Number of abandoned carts found
- Number of emails triggered
- Any errors with email service APIs

## Customization

### Change Email Service
Navigate to Admin → Settings → Notifications → Cart Abandonment tab and select the desired service.

### Change Abandonment Threshold
Edit in admin settings or set via command line:
```php
// In CheckCartAbandonment.php
protected $abandonmentThresholdHours = 24; // Or load from options
```

### Change Check Frequency
Edit `Kernel.php`:
```php
$schedule->command('cart:check-abandonment')->hourly(); // or daily, etc.
```

### Change Cooldown Period
Edit `CheckCartAbandonment.php`:
```php
if ($lastAdded && $lastAdded->diffInHours(now()) < 48) { // Change 48 to desired hours
```
