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.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / models / CartModel.php
tutor / models Last commit date
BillingModel.php 1 year ago CartModel.php 1 year ago CouponModel.php 1 year ago CourseModel.php 1 year ago LessonModel.php 1 year ago OrderActivitiesModel.php 1 year ago OrderMetaModel.php 1 year ago OrderModel.php 1 year ago QuizModel.php 1 year ago UserModel.php 1 year ago WithdrawModel.php 1 year ago
CartModel.php
256 lines
1 <?php
2 /**
3 * Cart Model
4 *
5 * @package Tutor\Models
6 * @author Themeum <support@themeum.com>
7 * @link https://themeum.com
8 * @since 3.0.0
9 */
10
11 namespace Tutor\Models;
12
13 use Tutor\Helpers\QueryHelper;
14
15 /**
16 * Cart model class for performing cart functionalities
17 */
18 class CartModel {
19
20 /**
21 * Cart table name
22 *
23 * @since 3.0.0
24 *
25 * @var string
26 */
27 private $table_name = 'tutor_carts';
28
29 /**
30 * Resolve props
31 *
32 * @since 3.0.0
33 */
34 public function __construct() {
35 $this->table_name = $GLOBALS['wpdb']->prefix . $this->table_name;
36 }
37
38 /**
39 * Add a course to the user's cart.
40 *
41 * @param int $user_id User ID.
42 * @param int $course_id Course ID.
43 *
44 * @return array Array containing the result of the insert operation.
45 */
46 public function add_course_to_cart( $user_id, $course_id ) {
47 global $wpdb;
48
49 $current_time = current_time( 'mysql', true );
50 $user_cart_id = 0;
51
52 $user_cart = QueryHelper::get_row(
53 "{$wpdb->prefix}tutor_carts",
54 array(
55 'user_id' => $user_id,
56 ),
57 'id'
58 );
59
60 if ( $user_cart ) {
61 $user_cart_id = $user_cart->id;
62 } else {
63 $user_cart_id = QueryHelper::insert(
64 "{$wpdb->prefix}tutor_carts",
65 array(
66 'user_id' => $user_id,
67 'created_at_gmt' => $current_time,
68 )
69 );
70 }
71
72 return QueryHelper::insert(
73 "{$wpdb->prefix}tutor_cart_items",
74 array(
75 'cart_id' => $user_cart_id,
76 'course_id' => $course_id,
77 )
78 );
79 }
80
81 /**
82 * Get items from the user's cart.
83 *
84 * @param int $user_id User ID.
85 *
86 * @return array Array containing the cart items and their total count.
87 */
88 public function get_cart_items( $user_id ) {
89 global $wpdb;
90
91 $cart_data = array(
92 'cart' => null,
93 'courses' => array(
94 'total_count' => 0,
95 'results' => array(),
96 ),
97 );
98
99 $user_cart = QueryHelper::get_row(
100 "{$wpdb->prefix}tutor_carts",
101 array(
102 'user_id' => $user_id,
103 ),
104 'id'
105 );
106
107 if ( $user_cart ) {
108 $cart_data['cart'] = $user_cart;
109
110 $primary_table = "{$wpdb->prefix}tutor_cart_items AS item";
111 $joining_tables = array(
112 array(
113 'type' => 'LEFT',
114 'table' => "{$wpdb->prefix}posts AS post",
115 'on' => 'item.course_id = post.ID',
116 ),
117 );
118 $where = array( 'item.cart_id' => $user_cart->id );
119 $select_columns = array( 'post.*' );
120 $cart_data['courses'] = QueryHelper::get_joined_data(
121 $primary_table,
122 $joining_tables,
123 $select_columns,
124 $where,
125 array(),
126 'item.id'
127 );
128 }
129
130 return $cart_data;
131 }
132
133 /**
134 * Check if the user has any items in their cart.
135 *
136 * @param int $user_id User ID.
137 * @return bool True if the user has items in the cart, false otherwise.
138 */
139 public function has_item_in_cart( $user_id ) {
140 $get_cart = $this->get_cart_items( $user_id );
141 $courses = $get_cart['courses'];
142 $total_count = $courses['total_count'];
143
144 return (int) $total_count > 0;
145 }
146
147 /**
148 * Delete cart item.
149 *
150 * @since 3.0.0
151 *
152 * @param int $id The ID of the cart.
153 *
154 * @return boolean
155 */
156 public function delete_cart_item( $id ) {
157 global $wpdb;
158
159 $delete = QueryHelper::delete( "{$wpdb->prefix}tutor_carts", array( 'id' => $id ) );
160
161 return $delete;
162 }
163
164 /**
165 * Delete a course from the user's cart.
166 *
167 * @since 3.0.0
168 *
169 * @param int $user_id User ID.
170 * @param int $course_id Course ID.
171 *
172 * @return boolean True if the course was successfully deleted, otherwise false.
173 */
174 public function delete_course_from_cart( $user_id, $course_id ) {
175 global $wpdb;
176
177 $user_cart = QueryHelper::get_row(
178 "{$wpdb->prefix}tutor_carts",
179 array(
180 'user_id' => $user_id,
181 ),
182 'id'
183 );
184
185 return QueryHelper::delete(
186 "{$wpdb->prefix}tutor_cart_items",
187 array(
188 'cart_id' => $user_cart->id,
189 'course_id' => $course_id,
190 )
191 );
192 }
193
194 /**
195 * Determine if a course is already added to the user's cart.
196 *
197 * @since 3.0.0
198 *
199 * @param int $user_id User ID.
200 * @param int $course_id Course ID.
201 *
202 * @return boolean True if the course is already in the cart, otherwise false.
203 */
204 public static function is_course_in_user_cart( $user_id, $course_id ) {
205 global $wpdb;
206
207 $cart_table = "{$wpdb->prefix}tutor_carts AS cart";
208 $item_table = "{$wpdb->prefix}tutor_cart_items AS item";
209
210 $join_conditions = array(
211 array(
212 'type' => 'LEFT',
213 'table' => $item_table,
214 'on' => 'cart.id = item.cart_id',
215 ),
216 );
217
218 $conditions = array(
219 'cart.user_id' => $user_id,
220 'item.course_id' => $course_id,
221 );
222
223 $select_columns = array( 'item.course_id' );
224
225 $cart_data = QueryHelper::get_joined_data(
226 $cart_table,
227 $join_conditions,
228 $select_columns,
229 $conditions,
230 array(),
231 'item.id'
232 );
233
234 return (bool) $cart_data['total_count'];
235 }
236
237 /**
238 * Delete cart data using user id
239 *
240 * @since 3.0.0
241 *
242 * @param int $user_id User ID.
243 *
244 * @return boolean
245 */
246 public function clear_user_cart( $user_id ) {
247 return QueryHelper::delete(
248 "{$this->table_name}",
249 array(
250 'user_id' => $user_id,
251 )
252 );
253 }
254
255 }
256