PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.2.1
Fluent Support – Helpdesk & Customer Support Ticket System v2.2.1
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / app / Services / Integrations / FluentCart / FluentCart.php

FluentCart.php in Fluent Support – Helpdesk & Customer Support Ticket System 2.2.1, at app/Services/Integrations/FluentCart/FluentCart.php

250 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\App\Services\Integrations\FluentCart;
4 use FluentCart\App\Models\Order;
5 use FluentSupport\App\Services\Helper;
6 use FluentSupport\Framework\Support\Arr;
7 use FluentSupport\App\Models\Customer;
8
9 class FluentCart
10 {
11 public function boot()
12 {
13 // add_filter('fluent_support/customer_extra_widgets', array($this, 'getFluentCartPurchaseWidgets'), 120, 2);
14 // add_filter('fluent_support/customer_extra_widgets', array($this, 'getFluentCartProLicenseWidget'), 125, 2);
15
16 if (
17 !apply_filters('fluent_support/disable_fc_menu', false) &&
18 Helper::getBusinessSettings('ticket_link_portal') === 'fluent_cart'
19 ) {
20 $this->renderCustomerPortalInFluentCartDashboard();
21 }
22 }
23
24 public function getFluentCartPurchaseWidgets($widgets, $customer)
25 {
26 $wpUserId = $customer->user_id;
27
28 // Get all order items for this customer, grouped by product
29 $orderItems = \FluentCart\App\Models\OrderItem::whereHas('order.customer', function ($query) use ($wpUserId) {
30 $query->where('user_id', $wpUserId);
31 })
32 ->with([
33 'order' => function ($query) {
34 $query->select(['id', 'status', 'created_at', 'total_amount', 'currency', 'invoice_no']);
35 }
36 ])
37 ->select(['id', 'order_id', 'post_id', 'post_title', 'title', 'payment_type', 'unit_price', 'subtotal', 'quantity'])
38 ->orderByDesc('id')
39 ->get();
40
41 if ($orderItems->isEmpty()) {
42 return $widgets;
43 }
44
45 // Create individual product entries for each order item (no grouping)
46 $formattedProducts = [];
47
48 foreach ($orderItems as $item) {
49 $licenseType = $this->getLicenseType($item);
50
51 $formattedProducts[] = [
52 'product_name' => $item->post_title,
53 'variation_name' => $item->title !== $item->post_title ? $item->title : null,
54 'license_type' => $licenseType,
55 'price' => $item->unit_price,
56 'currency' => $item->order->currency,
57 'formatted_price' => $item->formatted_total,
58 'status' => $this->getProductStatus($item),
59 'order' => [
60 'id' => $item->order->id,
61 'invoice_no' => $item->order->invoice_no,
62 'status' => $item->order->status,
63 'date' => $item->order->created_at->format('Y-m-d H:i:s'),
64 'currency' => $item->order->currency,
65 'total' => number_format($item->order->total_amount / 100, 2, '.', ''),
66 'quantity' => $item->quantity
67 ]
68 ];
69 }
70
71 // Sort products by most recent order date
72 usort($formattedProducts, function ($a, $b) {
73 return strtotime($b['order']['date']) - strtotime($a['order']['date']);
74 });
75
76 // Get FluentCart customer ID
77 $fluentCartCustomer = \FluentCart\App\Models\Customer::where('user_id', $wpUserId)->first();
78 if (!$fluentCartCustomer) {
79 return $widgets;
80 }
81
82 $widgets['fct_purchases'] = [
83 'title' => __('FluentCart Purchases', 'fluent-support'),
84 'products' => $formattedProducts,
85 'fluent_cart_customer_id' => $fluentCartCustomer->id,
86 'summary' => [
87 'lifetime_value' => \FluentCart\App\Helpers\Helper::toDecimal($fluentCartCustomer->ltv),
88 'currency' => Arr::get($formattedProducts, '0.currency', \FluentCart\Api\CurrencySettings::get('currency')),
89 'total_purchases' => $fluentCartCustomer->purchase_count,
90 'first_purchase' => $fluentCartCustomer->first_purchase_date ? gmdate('F j, Y', strtotime($fluentCartCustomer->first_purchase_date)) : null,
91 'last_purchase' => $fluentCartCustomer->last_purchase_date ? gmdate('F j, Y', strtotime($fluentCartCustomer->last_purchase_date)) : null,
92 ]
93 ];
94
95 return $widgets;
96 }
97
98 // public function getFluentCartProLicenseWidget($widgets, $customer)
99 // {
100 // // Add customer's product licenses if available
101 // $licenses = $this->getCustomerProductLicenses($customer);
102
103 // if ($licenses) {
104 // $widgets['fct_license'] = [
105 // 'header' => __('FluentCart Product Licenses', 'fluent-support'),
106 // 'licenses' => $licenses
107 // ];
108 // }
109
110 // return $widgets;
111 // }
112
113 /**
114 * Get customer's product licenses from FluentCart Pro
115 *
116 * @param Customer $customer
117 * @return array|null Structured license data or null if not available
118 */
119 private function getCustomerProductLicenses($customer)
120 {
121 // Check if FluentCart Pro License model is available
122 if (!class_exists('\FluentCartPro\App\Modules\Licensing\Models\License')) {
123 return null;
124 }
125
126 try {
127 $wpUserId = $customer->user_id;
128 if (!$wpUserId) {
129 return null;
130 }
131
132 // Get FluentCart customer ID
133 $fluentCartCustomer = \FluentCart\App\Models\Customer::where('user_id', $wpUserId)->first();
134 if (!$fluentCartCustomer) {
135 return null;
136 }
137
138 // Get licenses for this customer
139 $licenses = \FluentCartPro\App\Modules\Licensing\Models\License::where('customer_id', $fluentCartCustomer->id)
140 ->with(['product'])
141 ->orderByDesc('created_at')
142 ->get()
143 ->toArray();
144
145 if (empty($licenses)) {
146 return null;
147 }
148
149 $formattedLicenses = [];
150
151 foreach ($licenses as $license) {
152 // Get product name from the data structure
153 $productName = 'Unknown Product';
154 if (isset($license['product']['post_title'])) {
155 $productName = $license['product']['post_title'];
156 }
157
158
159
160 $status = $license['status'];
161 $expirationDate = $license['expiration_date'];
162
163 // Format expiration date for tooltip
164 $expirationTooltip = '';
165 if ($expirationDate) {
166 $expirationTooltip = 'Expires: ' . gmdate('M d, Y', strtotime($expirationDate));
167 } else {
168 $expirationTooltip = 'Expires: Lifetime';
169 }
170
171 $formattedLicenses[] = [
172 'id' => $license['id'],
173 'product_name' => $productName,
174 'status' => $status,
175 'expiration_tooltip' => $expirationTooltip,
176 'expiration_date' => $expirationDate
177 ];
178 }
179
180 return $formattedLicenses;
181
182 } catch (\Exception $e) {
183 return null;
184 }
185 }
186
187 /**
188 * Determine the license type based on order item payment type
189 *
190 * @param \FluentCart\App\Models\OrderItem $item
191 * @return string|null
192 */
193 private function getLicenseType($item)
194 {
195 switch ($item->payment_type) {
196 case 'subscription':
197 // Return 'Subscription' for internal logic but don't display it as license type
198 return 'Subscription';
199 case 'payment':
200 default:
201 // Check if it's a lifetime license based on product name or other criteria
202 if (stripos($item->post_title, 'lifetime') !== false || stripos($item->title, 'lifetime') !== false) {
203 return 'Lifetime License';
204 }
205 // Don't show license type for regular one-time purchases
206 return null;
207 }
208 }
209
210 /**
211 * Determine the product status based on order status and payment type
212 *
213 * @param \FluentCart\App\Models\OrderItem $item
214 * @return string
215 */
216 private function getProductStatus($item)
217 {
218 $orderStatus = $item->order->status;
219
220 // Return 'canceled' for multiple statuses
221 if (in_array($orderStatus, ['canceled', 'failed'])) {
222 return 'canceled';
223 }
224
225 // Return default statuses
226 $statusMap = [
227 'completed' => 'completed',
228 'processing' => 'processing',
229 'on-hold' => 'on-hold'
230 ];
231
232 return $statusMap[$orderStatus] ?? 'pending'; // Default to 'pending' if not found
233 }
234
235 private function renderCustomerPortalInFluentCartDashboard()
236 {
237 if (!function_exists('fluent_cart_api') || Helper::getBusinessSettings('ticket_link_portal') !== 'fluent_cart' ) {
238 return;
239 }
240
241 fluent_cart_api()->addCustomerDashboardEndpoint('fluent-support', [
242 'title' => __('Support', 'fluent-support'),
243 'render_callback' => function () {
244 echo do_shortcode('[fluent_support_portal]');
245 },
246 'priority' => 'high'
247 ]);
248 }
249 }
250