| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Database\Seeder; |
| 4 |
|
| 5 |
use FluentCart\App\Models\Coupon; |
| 6 |
use FluentCart\App\Services\DateTime\DateTime; |
| 7 |
|
| 8 |
class CouponSeeder |
| 9 |
{ |
| 10 |
public static function seed($count = 10) |
| 11 |
{ |
| 12 |
$types = ['fixed', 'percentage']; |
| 13 |
|
| 14 |
$titles = [ |
| 15 |
'New Year Discount', |
| 16 |
'Summer Sale', |
| 17 |
'Black Friday Deal', |
| 18 |
'Cyber Monday', |
| 19 |
'Holiday Special', |
| 20 |
'Clearance Offer', |
| 21 |
'Buy More Save More', |
| 22 |
'Exclusive Member Offer', |
| 23 |
'Flash Deal', |
| 24 |
'Welcome Gift' |
| 25 |
]; |
| 26 |
|
| 27 |
$codes = [ |
| 28 |
'NEWYEAR2025', |
| 29 |
'SUMMER25', |
| 30 |
'BLACKFRIDAY', |
| 31 |
'CYBERMON', |
| 32 |
'HOLIDAY22', |
| 33 |
'CLEAR50', |
| 34 |
'BUYMORE10', |
| 35 |
'MEMBEREX', |
| 36 |
'FLASH10', |
| 37 |
'WELCOME5' |
| 38 |
]; |
| 39 |
|
| 40 |
$coupons = []; |
| 41 |
|
| 42 |
if (defined('WP_CLI') && WP_CLI) { |
| 43 |
$progress = \WP_CLI\Utils\make_progress_bar('%CSeeding Coupons', count($titles)); |
| 44 |
} |
| 45 |
|
| 46 |
for ($i = 0; $i < count($titles); $i++) { |
| 47 |
$type = $types[array_rand($types)]; |
| 48 |
|
| 49 |
// Random start_date within ±100 days |
| 50 |
$startDate = DateTime::gmtNow()->addDays(wp_rand(-100, 100)); |
| 51 |
$endDate = (clone $startDate)->addDays(wp_rand(1, 60)); |
| 52 |
|
| 53 |
// Determine status based on end date |
| 54 |
$status = $endDate->isPast() ? 'expired' : 'active'; |
| 55 |
|
| 56 |
$amount = $type === 'fixed' ? wp_rand(1, 100) : wp_rand(1, 50); |
| 57 |
$min_purchase_amount = $type === 'fixed' ? wp_rand($amount + 1, 150) : 0; |
| 58 |
$max_discount_amount = $type === 'percentage' ? wp_rand(10, 20) * 100 : 0; |
| 59 |
|
| 60 |
$max_uses = wp_rand(1, 100); |
| 61 |
$max_per_customer = wp_rand(1, $max_uses); |
| 62 |
|
| 63 |
$conditions = [ |
| 64 |
'min_purchase_amount' => $min_purchase_amount, |
| 65 |
'max_purchase_amount' => 0, |
| 66 |
'apply_to_whole_cart' => 'yes', |
| 67 |
'max_per_customer' => $max_per_customer, |
| 68 |
'max_discount_amount' => $max_discount_amount, |
| 69 |
'max_uses' => $max_uses |
| 70 |
]; |
| 71 |
|
| 72 |
$coupons[] = [ |
| 73 |
'title' => $titles[$i], |
| 74 |
'code' => $codes[$i], |
| 75 |
'status' => $status, |
| 76 |
'type' => $type, |
| 77 |
'notes' => '', |
| 78 |
'priority' => wp_rand(0, 9), |
| 79 |
'amount' => $type === 'fixed' ? $amount * 100 : $amount, |
| 80 |
'stackable' => wp_rand(0, 1) ? 'yes' : 'no', |
| 81 |
'conditions' => json_encode($conditions), |
| 82 |
'start_date' => $startDate, |
| 83 |
'end_date' => $endDate |
| 84 |
]; |
| 85 |
|
| 86 |
if (defined('WP_CLI') && WP_CLI) { |
| 87 |
$progress->tick(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
Coupon::query()->insert($coupons); |
| 92 |
|
| 93 |
if (defined('WP_CLI') && WP_CLI) { |
| 94 |
$progress->finish(); |
| 95 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 96 |
echo \WP_CLI::colorize('%n'); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|