PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.3
Booktics – Appointment Booking Calendar for Service Businesses v1.0.3
1.0.29 1.0.28 1.0.27 1.0.26 1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.2 1.0.3 All 31 releases
booktics / trunk / core / cart / cart-manager.php

cart-manager.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.3, at trunk/core/cart/cart-manager.php

338 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Booktics\Cart;
4
5 use Booktics\Abstracts\Booktics_Database;
6
7 /**
8 * Class Cart_Manager
9 * Manages shopping cart operations including adding items, updating quantities, and calculating totals
10 *
11 * @package Booktics\Cart
12 */
13 class Cart_Manager {
14 /**
15 * @var Booktics_Database The database instance
16 */
17 protected $db;
18
19 /**
20 * @var string The carts table name
21 */
22 protected $carts_table;
23
24 /**
25 * @var string The cart items table name
26 */
27 protected $cart_items_table;
28
29 /**
30 * Initialize CartManager with database connection
31 */
32 public function __construct() {
33 $this->db = new Booktics_Database();
34 $this->carts_table = booktics_get_table_name( 'booktics_carts' );
35 $this->cart_items_table = booktics_get_table_name( 'booktics_cart_items' );
36 }
37
38 /**
39 * Add item to the cart for guest users
40 *
41 * @param array $item
42 *
43 * @return array
44 */
45 public function add_to_cart_guest( array $item ): array {
46 return Cart_Session::add_item( $item );
47 }
48
49 /**
50 * Add item to the cart for both guest and registered users
51 *
52 * @param array $params Cart item parameters
53 *
54 * @return array Added cart item
55 */
56 public function add_to_cart( array $params ): array {
57 $user_id = get_current_user_id();
58 $params['uuid'] = wp_generate_uuid4();
59 if ( $user_id ) {
60 return $this->add_to_cart_user( $user_id, $params );
61 }
62
63 return $this->add_to_cart_guest( $params );
64 }
65
66 /**
67 * Add item to the cart for registered users
68 *
69 * @param int $user_id
70 * @param array $item
71 *
72 * @return array
73 */
74 public function add_to_cart_user( int $user_id, array $item ): array {
75 $carts = $this->db->select(
76 $this->carts_table, array(
77 'user_id' => $user_id,
78 )
79 );
80
81 if ( ! count( $carts ) ) {
82 $uuid = wp_generate_uuid4();
83 $this->db->insert_batch(
84 $this->carts_table, array(
85 array(
86 'uuid' => $uuid,
87 'user_id' => $user_id,
88 'total' => 0,
89 'created_at' => current_time( 'mysql' ),
90 'updated_at' => current_time( 'mysql' ),
91 ),
92 )
93 );
94
95 $carts = $this->db->select(
96 $this->carts_table, array(
97 'user_id' => $user_id,
98 )
99 );
100 }
101
102 // Check if the service_id already exists in the cart items for this cart
103 if ( count( $carts ) ) {
104 $cart = $carts[0];
105 $existing_items = $this->db->select(
106 $this->cart_items_table,
107 array(
108 'cart_id' => $cart->id,
109 'service_id' => $item['service_id'],
110 )
111 );
112 if ( ! empty( $existing_items ) ) {
113 // Delete existing items with the same service_id for this cart
114 foreach ( $existing_items as $existing_item ) {
115 $this->db->delete(
116 $this->cart_items_table,
117 array(
118 'id' => $existing_item->id,
119 )
120 );
121 }
122 }
123 }
124 $cart = count( $carts ) ? $carts[0] : array();
125 $cart_item_data = array(
126 'uuid' => $item['uuid'],
127 'cart_id' => $cart->id,
128 'team_member_id' => $item['team_member_id'],
129 'service_id' => $item['service_id'],
130 'date' => $item['date'],
131 'start_time' => $item['start_time'],
132 'end_time' => $item['end_time'],
133 'price' => $item['price'] ?? 0,
134 'subtotal' => $item['subtotal'] ?? $item['price'],
135 'total' => $item['total'] ?? $item['subtotal'],
136 'additional_duration_id' => $item['additional_duration_id'] ?? '',
137 'created_at' => current_time( 'mysql' ),
138 'updated_at' => current_time( 'mysql' ),
139 );
140
141 $cart_item_data = apply_filters( 'booktics_pro_cart_db', $cart_item_data, $item );
142 $this->db->insert_batch( $this->cart_items_table, array( $cart_item_data ) );
143 if ( isset( $cart->id ) ) {
144 $this->update_cart_total( $cart->id );
145 }
146 $cart_item_data['extra_services'] = json_decode( $cart_item_data['extra_services'], true );
147
148 return $cart_item_data;
149 }
150
151 /**
152 * Synchronize session cart items to a database
153 *
154 * @param int $user_id
155 */
156 public function sync_session_to_db( int $user_id ): void {
157 $session_cart = Cart_Session::get();
158 if ( empty( $session_cart['items'] ) ) {
159 return;
160 }
161
162 foreach ( $session_cart['items'] as $item ) {
163 $this->add_to_cart_user( $user_id, $item );
164 }
165
166 Cart_Session::clear();
167 }
168
169 /**
170 * Update cart total based on items
171 *
172 * @param int $cart_id
173 */
174 protected function update_cart_total( int $cart_id ): void {
175 $items = $this->db->select(
176 $this->cart_items_table, array(
177 'cart_id' => $cart_id,
178 )
179 );
180
181 if ( empty( $items ) ) {
182 $this->db->delete( $this->carts_table, array( 'id' => $cart_id ) );
183
184 return;
185 }
186
187 $total = 0;
188 foreach ( $items as $item ) {
189 $total += (float) ( $item->total ?? $item->subtotal ?? 0 );
190 }
191
192 $this->db->update(
193 $this->carts_table,
194 array(
195 'total' => $total,
196 'updated_at' => current_time( 'mysql' ),
197 ),
198 array( 'id' => $cart_id )
199 );
200 }
201
202 /**
203 * Get a cart and its items for a user
204 *
205 * @return array
206 */
207 public function get_cart(): array {
208 $user_id = get_current_user_id();
209
210 if ( ! $user_id ) {
211 return Cart_Session::get();
212 }
213
214 $carts = $this->db->select(
215 $this->carts_table, array(
216 'user_id' => $user_id,
217 )
218 );
219 $cart = count( $carts ) ? $carts[0] : null;
220
221 if ( ! $cart ) {
222 return array( 'items' => array() );
223 }
224
225 $items = $this->db->select(
226 $this->cart_items_table, array(
227 'cart_id' => $cart->id,
228 )
229 );
230 foreach ( $items as $i => $item ) {
231 if ( isset( $item->extra_services ) ) {
232 $item->extra_services = json_decode( $item->extra_services, true );
233 }
234 }
235
236 return array_merge( (array) $cart, array( 'items' => $items ) );
237 }
238
239 /**
240 * Remove item from the cart
241 *
242 * @param string|null $id
243 */
244 public function remove_cart_item( $id = null ): void {
245 $user_id = get_current_user_id();
246 if ( ! $user_id ) {
247 if ( ! $id ) {
248 Cart_Session::clear();
249 } else {
250 Cart_Session::delete_item( $id );
251 }
252 }
253
254 $carts = $this->db->select(
255 $this->carts_table, array(
256 'user_id' => $user_id,
257 )
258 );
259
260 $cart = count( $carts ) ? $carts[0] : null;
261
262 if ( ! $cart ) {
263 return;
264 }
265
266 $conditions = array( 'cart_id' => $cart->id );
267 if ( ! empty( $id ) ) {
268 $conditions['uuid'] = $id;
269 }
270
271 $this->db->delete( $this->cart_items_table, $conditions );
272
273 $this->update_cart_total( $cart->id );
274 }
275
276 /**
277 * Edit item in the cart
278 *
279 * @param string $uuid
280 * @param array $new_data
281 *
282 * @return array Updated item data or empty array if item not found
283 */
284 public function edit_cart_item( string $uuid, array $new_data ): array {
285 if ( ! is_user_logged_in() ) {
286 Cart_Session::edit_item( $uuid, $new_data );
287 $cart = Cart_Session::get();
288 foreach ( $cart['items'] as $item ) {
289 if ( $item['uuid'] === $uuid ) {
290 return $item;
291 }
292 }
293
294 return array();
295 }
296
297 $user_id = get_current_user_id();
298 $carts = $this->db->select(
299 $this->carts_table, array(
300 'user_id' => $user_id,
301 )
302 );
303
304 $cart = count( $carts ) ? $carts[0] : null;
305
306 if ( ! $cart ) {
307 return array();
308 }
309
310 $cart_item_data = array_merge( $new_data, array( 'updated_at' => current_time( 'mysql' ) ) );
311 $cart_item_data = apply_filters( 'booktics_pro_cart_db', $cart_item_data, $new_data );
312 $this->db->update(
313 $this->cart_items_table, $cart_item_data,
314 array(
315 'cart_id' => $cart->id,
316 'uuid' => $uuid,
317 )
318 );
319
320 $this->update_cart_total( $cart->id );
321
322 $updated_item = $this->db->select(
323 $this->cart_items_table, array(
324 'cart_id' => $cart->id,
325 'uuid' => $uuid,
326 )
327 );
328
329 foreach ( $updated_item as $i => $item ) {
330 if ( isset( $item->extra_services ) ) {
331 $item->extra_services = json_decode( $item->extra_services, true );
332 }
333 }
334
335 return count( $updated_item ) ? (array) $updated_item[0] : array();
336 }
337 }
338