PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 2.5.0
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v2.5.0
2.5.0 2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 All 34 releases
fluent-booking / app / Services / Integrations / FluentCart / Http / Controller / FluentCartController.php

FluentCartController.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 2.5.0, at app/Services/Integrations/FluentCart/Http/Controller/FluentCartController.php

203 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBooking\App\Services\Integrations\FluentCart\Http\Controller;
4
5 use FluentBooking\App\Models\CalendarSlot;
6 use FluentBooking\App\Http\Controllers\Controller;
7 use FluentBooking\Framework\Http\Request\Request;
8 use FluentCart\App\Models\Product as CartProduct;
9 use FluentCart\App\Models\ProductDetail as CartProductDetail;
10 use FluentCart\App\Models\ProductVariation as CartProductVariant;
11 use FluentCart\App\Services\Permission\PermissionManager as CartPermission;
12 use FluentBooking\App\Services\CurrenciesHelper;
13 use FluentBooking\App\Services\Integrations\FluentCart\CartHelper;
14 use FluentBooking\Framework\Support\Arr;
15
16 class FluentCartController extends Controller
17 {
18 public function createProduct(Request $request)
19 {
20 if (!CartPermission::hasPermission('products/create')) {
21 return $this->sendError([
22 'message' => __('You do not have permission to create products', 'fluent-booking')
23 ], 422);
24 }
25
26 $title = $request->getSafe('title');
27 $price = $request->getSafe('price', 'floatval');
28
29 $postData = [
30 'post_title' => $title,
31 'post_name' => sanitize_title($title),
32 'post_status' => 'publish',
33 'post_type' => \FluentCart\App\CPT\FluentProducts::CPT_NAME,
34 ];
35
36 $createdPostId = wp_insert_post($postData);
37
38 if (is_wp_error($createdPostId)) {
39 return $this->sendError([
40 'code' => 403,
41 'message' => $createdPostId->get_error_message()
42 ]);
43 }
44
45 $detailData = [
46 'post_id' => $createdPostId,
47 'fulfillment_type' => 'digital',
48 'min_price' => CurrenciesHelper::toCents($price),
49 'max_price' => CurrenciesHelper::toCents($price),
50 ];
51
52 $productDetail = CartProductDetail::create($detailData);
53
54 $variationData = [
55 'post_id' => $createdPostId,
56 'serial_index' => 1,
57 'variation_title' => $title,
58 'stock_status' => 'in-stock',
59 'payment_type' => 'onetime',
60 'total_stock' => 1,
61 'available' => 1,
62 'fulfillment_type' => 'digital',
63 'item_price' => CurrenciesHelper::toCents($price),
64 'other_info' => [
65 'description' => '',
66 'payment_type' => 'onetime',
67 'times' => '',
68 'repeat_interval' => '',
69 'trial_days' => '',
70 'billing_summary' => '',
71 'manage_setup_fee' => 'no',
72 'signup_fee_name' => '',
73 'signup_fee' => '',
74 'setup_fee_per_item' => 'no',
75 ]
76 ];
77
78 $variation = CartProductVariant::create($variationData);
79
80 if (!$productDetail || !$variation) {
81 return $this->sendError([
82 'code' => 403,
83 'message' => __('Failed to create product', 'fluent-booking')
84 ]);
85 }
86
87 $product = CartProduct::where('ID', $createdPostId)->first();
88
89 $product->updateProductMeta('created_from', 'fluent_booking');
90
91 $formattedProduct = CartHelper::getProductVariationOptions($product);
92
93 return [
94 'message' => __('Product has been created', 'fluent-booking'),
95 'product' => $formattedProduct,
96 'variant' => $variation
97 ];
98 }
99
100 public function getCartProducts(Request $request)
101 {
102 $search = trim($request->getSafe('search'));
103 $includeId = $request->getSafe('include_id', 'intval');
104
105 $products = CartProduct::query()
106 ->select('ID', 'post_title')
107 ->whereIn('post_status', ['publish', 'private'])
108 ->whereHas('detail')
109 ->whereHas('variants')
110 ->when($search, function ($query) use ($search) {
111 $query->where('post_title', 'LIKE', '%' . $search . '%');
112 })
113 ->with(['detail', 'variants' => function ($query) {
114 $query->select('id', 'post_id', 'item_price', 'variation_title');
115 }])
116 ->limit(20)
117 ->get();
118
119 $includeProductId = $includeId ? CartProductVariant::where('id', $includeId)->value('post_id') : null;
120
121 $isIncluded = false;
122 $formattedProducts = [];
123 foreach ($products as $product) {
124 if ($product->ID == $includeProductId) {
125 $isIncluded = true;
126 }
127 $formattedProduct = CartHelper::getProductVariationOptions($product);
128 $formattedProducts[] = $formattedProduct;
129 }
130
131 if (!$isIncluded && $includeProductId) {
132 $product = CartProduct::where('ID', $includeProductId)
133 ->select('ID', 'post_title')
134 ->whereIn('post_status', ['publish', 'private'])
135 ->whereHas('detail')
136 ->whereHas('variants')
137 ->with(['detail', 'variants' => function ($query) {
138 $query->select('id', 'post_id', 'item_price', 'variation_title');
139 }])->first();
140 $formattedProduct = CartHelper::getProductVariationOptions($product);
141 $formattedProducts[] = $formattedProduct;
142 }
143
144 return [
145 'items' => array_filter($formattedProducts)
146 ];
147 }
148
149 public function saveEventCartSettings(Request $request, $calendarId, $eventId)
150 {
151 $calendarEvent = CalendarSlot::where('calendar_id', $calendarId)->findOrFail($eventId);
152
153 $data = $request->validate([
154 'settings' => 'required|array',
155 ]);
156
157 $data = $data['settings'];
158
159 $isEnabled = Arr::get($data, 'enabled', 'no') === 'yes';
160 $isMultiEnabled = Arr::get($data, 'multi_payment_enabled', 'no') === 'yes';
161
162 $eventType = $isEnabled ? 'cart' : 'free';
163
164 $settings = $calendarEvent->getPaymentSettings();
165
166 $settings['enabled'] = $isEnabled ? 'yes' : 'no';
167 $settings['multi_payment_enabled'] = $isMultiEnabled ? 'yes' : 'no';
168 $settings['driver'] = $isEnabled ? 'cart' : $settings['driver'];
169
170 if (!$isMultiEnabled) {
171 $product = CartHelper::getProduct(Arr::get($data, 'cart_product_id'));
172 if (!$product) {
173 return $this->sendError([
174 'message' => __('Please select a product', 'fluent-booking')
175 ], 422);
176 }
177
178 $settings['cart_product_id'] = Arr::get($data, 'cart_product_id');
179 }
180
181 if ($isMultiEnabled) {
182 $productIds = array_map('intval', Arr::get($data, 'multi_payment_cart_ids'));
183 $products = CartProductVariant::whereIn('id', array_values($productIds))->exists();
184 if (!$products) {
185 return $this->sendError([
186 'message' => __('Please select products', 'fluent-booking')
187 ], 422);
188 }
189
190 $settings['multi_payment_cart_ids'] = Arr::get($data, 'multi_payment_cart_ids');
191 }
192
193 $calendarEvent->type = $eventType;
194 $calendarEvent->save();
195
196 $calendarEvent->updateMeta('payment_settings', $settings);
197
198 return [
199 'message' => __('Settings have been saved', 'fluent-booking')
200 ];
201 }
202 }
203