# FINEART Certificates System

## 🎨 Overview

This system automatically creates digital Certificates of Authenticity for each artwork purchase when an order is marked as "Completed" in WooCommerce.

## 📋 Features

- **Automatic certificate generation** on order completion
- **Unique URLs** for each certificate (16-character random slugs)
- **QR codes** for physical display and verification
- **Black/minimalist design** matching FINEART branding
- **Print-optimized** certificate pages
- **Email notifications** with all certificates in one email
- **Automatic cleanup** when orders are refunded
- **Test mode** for safe testing on production

## 🚀 Installation

### 1. Install QR Code Library

The system needs a QR code library. Download phpqrcode from:
https://sourceforge.net/projects/phpqrcode/

Extract and copy `qrlib.php` to:
`/fineart-plugin/includes/lib/qr-code/qrlib.php`

**Alternative:** Use Composer in the plugin directory:
```bash
cd /path/to/fineart-plugin
composer require bacon/bacon-qr-code
```

Then update the require path in `class-fineart-certificate-generator.php` line 138.

### 2. Upload Plugin

Upload the updated plugin to your WordPress site or replace existing files.

### 3. Activate/Update

- If new: Activate the plugin in WordPress admin
- If updating: Reactivate or just clear cache

The system will automatically:
- Register the `fineart_certificate` custom post type
- Flush rewrite rules for clean URLs
- Hook into WooCommerce order completion

## 🧪 Testing Mode

**IMPORTANT:** The system starts in TEST MODE by default.

In `includes/certificates/class-fineart-certificate-generator.php` (lines 10-11):

```php
define('FINEART_CERTIFICATES_TEST_MODE', true);  // Set to false for production
define('FINEART_CERTIFICATES_TEST_EMAIL', 'loic.kernen@gmail.com'); // Your email
```

### Test Mode Behavior:

✅ **What happens:**
- Certificates ARE created for real orders
- QR codes ARE generated
- Certificate URLs work normally
- Email subject shows `[TEST MODE]`

⚠️ **What's different:**
- Emails go ONLY to `FINEART_CERTIFICATES_TEST_EMAIL`
- Customer's real email is shown in email for reference
- Red warning banner in email shows actual recipient

### Testing Workflow:

1. Keep `TEST_MODE = true`
2. Complete a real order
3. Check test email inbox
4. Click certificate links to verify
5. Test printing functionality
6. When satisfied, set to `false`

## 📊 Certificate Data Stored

Each certificate stores:
- Product ID
- Edition number (just the number, e.g., 127)
- Edition total (200)
- Artist name
- Artist ID
- Buyer name
- Buyer country
- Order ID
- Purchase date
- Frame size
- Frame color
- Artwork image ID
- Artist signature URL
- QR code path/URL

## 🔗 Certificate URLs

Format: `https://fineart.gallery/certificate/a8k3m9p2q7x1f5/`

- 16-character random slug
- Unpredictable (62^16 combinations)
- SEO: noindex, nofollow
- Public but not discoverable

## 📧 Email System

### Single Email Per Order

If customer buys 3 artworks → receives 1 email with 3 certificates

### Email Content:
- Personalized greeting
- Test mode warning (if active)
- Certificate list with CTA buttons
- Information about certificates
- Order number reference

## 🗑️ Refund Handling

When an order is refunded:
1. System finds all certificates linked to order
2. Deletes QR code image files
3. Permanently deletes certificate posts

## 🎨 Design

### Certificate Page:
- Black background (#000)
- White text
- Oswald font (FINEART standard)
- Large artwork image
- Prominent edition number
- Grid layout for details
- Artist signature (white version)
- QR code (fixed bottom-right)

### Print Styles:
- White background
- Black text
- Optimized for A4 paper
- All elements maintained

## 🔧 Customization

### Change Certificate Layout:
Edit: `templates/certificates/single-certificate.php`

### Change Email Design:
Edit: `templates/certificates/emails/certificate-email.php`

### Modify Certificate Logic:
Edit: `includes/certificates/class-fineart-certificate-generator.php`

## 📝 Database

### Custom Post Type:
- Type: `fineart_certificate`
- Public: Yes (for URLs)
- Admin UI: No (hidden)
- Search: Excluded

### Post Meta Keys:
All stored with `_cert_` prefix for privacy.

## 🐛 Troubleshooting

### Certificates not being created:
1. Check if order status is "Completed"
2. Verify edition number exists in order item meta
3. Check error logs for issues

### QR codes not showing:
1. Verify QR library is installed
2. Check file permissions on `/wp-content/uploads/certificates/`
3. Look for errors in PHP error log

### Certificate page 404:
1. Go to Settings → Permalinks
2. Click "Save Changes" (flushes rewrite rules)
3. Try accessing certificate again

### Emails not sending in test mode:
1. Verify test email is correct
2. Check WordPress mail function works
3. Check spam folder

## 🚦 Going Live

When ready for production:

1. **Test thoroughly** with test mode active
2. Verify all certificates display correctly
3. Test email delivery to your email
4. Check QR codes work
5. Test print functionality

Then:

```php
// In class-fineart-certificate-generator.php
define('FINEART_CERTIFICATES_TEST_MODE', false);  // ← Change to false
```

6. Save and upload file
7. Complete a test order
8. Verify customer receives email

## 📊 Monitoring

### View Certificates (Database):

```sql
SELECT * FROM wp_posts WHERE post_type = 'fineart_certificate';
```

### View Certificate Meta:

```sql
SELECT * FROM wp_postmeta WHERE post_id IN 
(SELECT ID FROM wp_posts WHERE post_type = 'fineart_certificate');
```

### Count Certificates:

```php
$cert_count = wp_count_posts('fineart_certificate');
echo $cert_count->publish . ' certificates created';
```

## 🔒 Security

- Certificates use crypto-random 16-char slugs
- No sequential IDs exposed
- URLs not guessable
- noindex/nofollow prevents indexing
- No admin list to browse all certificates

## 📞 Support

For issues or questions:
- Check error logs: `/wp-content/debug.log`
- Review order notes in WooCommerce
- Check certificate post meta in database

## 📝 Version

Certificate System v1.0
Compatible with: FINEART Plugin v3.0+
