# VitaminsForLife_KlaviyoCustom — Magento 2 Module
## Klaviyo Custom Integration — Vitamins for Life

---

## 📦 Module Overview

This custom Magento 2 module handles:
1. **Contact Form Enquiry Sync** — Pushes contact form submissions to Klaviyo as profiles + events
2. **Historical Contact Form Sync** — CLI command to batch sync historical contact form data
3. **Pre-checked Consent Checkbox** — Adds JC-approved marketing consent checkbox at checkout (pre-checked by default)
4. **Order Consent Plugin** — Syncs consent status to Klaviyo when order is placed

---

## 📁 File Structure

```
VitaminsForLife/KlaviyoCustom/
├── registration.php                          # Module registration
├── composer.json                             # Composer config
├── README.md                                 # This file
├── etc/
│   ├── module.xml                            # Module declaration
│   ├── events.xml                            # Event observers
│   └── di.xml                               # Dependency injection config
├── Model/
│   └── KlaviyoApiService.php                 # Klaviyo API wrapper
├── Observer/
│   └── ContactFormSubmit.php                 # Contact form event observer
├── Plugin/
│   └── ConsentCheckboxPlugin.php             # Order placement plugin
├── Console/
│   └── SyncHistoricalContactForms.php        # CLI sync command
└── view/frontend/
    ├── layout/
    │   └── checkout_index_index.xml          # Checkout layout injection
    ├── templates/checkout/
    │   └── consent-checkbox.html             # Checkbox KO template
    └── web/
        ├── js/consent-checkbox.js            # KnockoutJS component
        └── css/consent.css                   # Checkbox styles
```

---

## 🚀 Installation

### Step 1 — Copy module to Magento:
```bash
cp -r VitaminsForLife /var/www/html/magento/app/code/
```

### Step 2 — Enable and deploy:
```bash
php bin/magento module:enable VitaminsForLife_KlaviyoCustom
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush
```

---

## 🧪 Testing on Staging

### Contact Form:
```bash
# Submit contact form on staging
# Check Klaviyo → Profiles for new profile
# Check profile timeline for "Submitted Contact Form" event
```

### Historical Sync (10 records for staging):
```bash
php bin/magento klaviyo:sync-contact-forms --limit=10
```

### Full Historical Sync (Live only):
```bash
php bin/magento klaviyo:sync-contact-forms
```

---

## ⚠️ Important Notes

1. **Pre-checked checkbox** — Per JC requirement, consent checkbox is checked by default
2. **Subscribe all** — All users subscribed by default except known unsubscribers
3. **Contact form table** — Update table name in `SyncHistoricalContactForms.php` if client uses custom table
4. **Never run full historical sync on staging** — Use `--limit=10` for testing
5. **Klaviyo private key** — Reads from `klaviyo_reclaim/general/private_api_key` config path

---

## 📋 Client Requirements Covered

| Requirement | File |
|-------------|------|
| Contact form → Klaviyo sync | Observer/ContactFormSubmit.php |
| Historical contact form sync | Console/SyncHistoricalContactForms.php |
| Pre-checked consent checkbox | view/frontend/web/js/consent-checkbox.js |
| JC's exact checkbox text | view/frontend/web/js/consent-checkbox.js |
| Subscribe all by default | Model/KlaviyoApiService.php |
| Order consent sync | Plugin/ConsentCheckboxPlugin.php |

---

## 📞 Support
Developer: Deep Gandhi
Project: Klaviyo Integration — Vitamins for Life
