PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 2.3.0
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v2.3.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 1.7.2 All 33 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.3.0, at app/Services/Integrations/FluentCart/Http/Controller/FluentCartController.php

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