PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.14
Booktics – Appointment Booking Calendar for Service Businesses v1.0.14
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 / core / cart / cart-session.php

cart-session.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.14, at core/cart/cart-session.php

129 lines 3.1 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 /**
6 * Class Cart_Session
7 * Handles cart session management for the Booktics plugin
8 *
9 * @package Booktics\Cart
10 */
11 class Cart_Session {
12
13 /**
14 * Session key used to store cart data in a PHP session
15 *
16 * @var string
17 */
18 const SESSION_KEY = 'booktics_cart';
19
20 /**
21 * Get cart data from session
22 *
23 * @return array{items: array, total: float}
24 */
25 public static function get() {
26 $session_data = ! empty( $_SESSION[ self::SESSION_KEY ] ) ? $_SESSION[ self::SESSION_KEY ] : null;
27
28 if ( ! is_array( $session_data ) ) {
29 return array(
30 'items' => array(),
31 'total' => 0.00,
32 'coupon' => '',
33 );
34 }
35
36 return $session_data;
37 }
38
39 /**
40 * Set cart data in session
41 *
42 * @param array $cart Cart data to store in session
43 *
44 * @return void
45 */
46 public static function set( array $cart ) {
47 $_SESSION[ self::SESSION_KEY ] = $cart;
48 }
49
50 /**
51 * Clear cart data from session
52 *
53 * @return void
54 */
55 public static function clear() {
56 unset( $_SESSION[ self::SESSION_KEY ] );
57 }
58
59 /**
60 * Add an item to the cart session
61 *
62 * @param array $item
63 *
64 * @return array
65 */
66 public static function add_item( array $item ): array {
67 $cart = self::get();
68 $cart['items'][] = $item;
69 $cart['total'] += $item['subtotal'];
70 $cart = apply_filters( 'booktics_pro_cart_session', $cart );
71
72 self::set( $cart );
73
74 return end( $cart['items'] );
75 }
76
77 /**
78 * Delete an item from the cart session
79 *
80 * @param string $id
81 *
82 * @return void
83 */
84 public static function delete_item( string $id ) {
85 $cart = self::get();
86 $cart['items'] = array_values(
87 array_filter(
88 $cart['items'],
89 function ( $item ) use ( $id ) {
90 return $item['uuid'] != $id;
91 }
92 )
93 );
94 $cart['total'] = array_reduce(
95 $cart['items'], function ( $total, $item ) {
96 return $total + $item['price'];
97 }, 0.00
98 );
99
100 self::set( $cart );
101 }
102
103 /**
104 * Edit an item in the cart session
105 *
106 * @param string $uuid UUID of the item to edit
107 * @param array $new_data New data to update the item with
108 *
109 * @return void
110 */
111 public static function edit_item( string $uuid, array $new_data ) {
112 $cart = self::get();
113 $cart['total'] = 0;
114
115 $cart['items'] = array_map(
116 function ( $item ) use ( $uuid, $new_data, &$cart ) {
117 if ( $item['uuid'] === $uuid ) {
118 $item = array_merge( $item, $new_data );
119 }
120 $cart['total'] += $item['subtotal'];
121
122 return $item;
123 }, $cart['items']
124 );
125 $cart = apply_filters( 'booktics_pro_cart_session', $cart );
126 self::set( $cart );
127 }
128 }
129