PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / database / Seeder / SubscriptionSeeder.php

SubscriptionSeeder.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at database/Seeder/SubscriptionSeeder.php

127 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\Database\Seeder;
4
5 use FluentCart\App\Helpers\Status;
6 use FluentCart\App\Models\Order;
7 use FluentCart\App\Models\OrderItem;
8 use FluentCart\App\Models\OrderTransaction;
9 use FluentCart\App\Models\ProductVariation;
10 use FluentCart\App\Models\Subscription;
11 use FluentCart\App\Services\DateTime\DateTime;
12 use FluentCart\Faker\Factory;
13
14 class SubscriptionSeeder
15 {
16 public static function seed($count, $assoc_args = [])
17 {
18 $faker = Factory::create();
19
20 // Get active orders with subscription items
21 $orderItems = OrderItem::query()
22 ->where('payment_type', 'subscription')
23 ->get();
24
25 if (defined('WP_CLI') && WP_CLI) {
26 $progress = \WP_CLI\Utils\make_progress_bar('%CSeeding Subscriptions', $count);
27 }
28
29 $subscriptions = [];
30
31 if (empty($orderItems)) {
32 return;
33 }
34
35 foreach ($orderItems as $orderItem) {
36
37 $order = Order::find($orderItem->order_id);
38 if (!$order) {
39 continue;
40 }
41
42 $variation = ProductVariation::find($orderItem->object_id);
43 if (!$variation || $variation->payment_type !== 'subscription') {
44
45 continue;
46 }
47
48 $otherInfo = $variation->other_info;
49 $createdDate = $order->created_at;
50 $createdDateGmt = DateTime::anyTimeToGmt($createdDate);
51
52 $intervalMap = [
53 'daily' => 'day',
54 'weekly' => 'week',
55 'monthly' => 'month',
56 'yearly' => 'year',
57 ];
58
59 $interval = $intervalMap[$otherInfo['repeat_interval']] ?? 'month';
60
61 $nextBillingDate = (new DateTime($createdDateGmt))
62 ->modify('+1 ' . $interval)
63 ->format('Y-m-d H:i:s');
64 // Set subscription status based on order payment status
65 $status = Status::SUBSCRIPTION_ACTIVE;
66 if ($order->payment_status === Status::PAYMENT_PAID) {
67 $status = Status::SUBSCRIPTION_ACTIVE;
68 } else if ($order->payment_status === Status::PAYMENT_FAILED) {
69 $status = Status::SUBSCRIPTION_FAILING;
70 } else if ($order->payment_status === Status::PAYMENT_REFUNDED) {
71 $status = Status::SUBSCRIPTION_CANCELED;
72 } else if ($order->payment_status === Status::PAYMENT_PENDING) {
73 $status = Status::SUBSCRIPTION_PENDING;
74 }
75
76 $subscriptions[] = [
77 'parent_order_id' => $order->id,
78 'customer_id' => $order->customer_id,
79 'product_id' => $orderItem->post_id,
80 'item_name' => $orderItem->title,
81 'variation_id' => $variation->id,
82 'status' => $status,
83 'billing_interval' => $otherInfo['repeat_interval'],
84 'recurring_amount' => $orderItem['unit_price'],
85 'recurring_total' => $orderItem['unit_price'],
86 'bill_times' => $otherInfo['times'],
87 'next_billing_date' => $nextBillingDate,
88 'created_at' => $createdDateGmt,
89 'updated_at' => $createdDateGmt
90 ];
91
92
93 if (defined('WP_CLI') && WP_CLI) {
94 $progress->tick();
95 }
96 }
97
98 // Insert subscriptions in chunks
99 foreach (array_chunk($subscriptions, 100) as $chunk) {
100 Subscription::query()->insert($chunk);
101 }
102
103 self::updateTransactions();
104
105 if (defined('WP_CLI') && WP_CLI) {
106 $progress->finish();
107 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
108 echo \WP_CLI::colorize('%n');
109 }
110 }
111
112 public static function updateTransactions()
113 {
114 $subscriptions = Subscription::query()->get();
115
116 foreach ($subscriptions as $subscription) {
117 OrderTransaction::query()
118 ->where('order_id', $subscription->parent_order_id)
119 ->where('order_type', 'subscription')
120 ->update([
121 'subscription_id' => $subscription->id,
122 'transaction_type' => Status::TRANSACTION_TYPE_CHARGE
123 ]);
124 }
125 }
126 }
127