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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
BaseModel.php 11 months ago BillingModel.php 1 year ago CartModel.php 1 year ago CouponModel.php 11 months ago CourseModel.php 10 months ago LessonModel.php 10 months ago OrderActivitiesModel.php 1 year ago OrderMetaModel.php 1 year ago OrderModel.php 10 months ago QuizModel.php 10 months ago UserModel.php 1 year ago WithdrawModel.php 1 year ago
CartModel.php
257 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 $has = (int) $total_count > 0;
145 return apply_filters( 'tutor_is_cart_empty', $has );
146 }
147
148 /**
149 * Delete cart item.
150 *
151 * @since 3.0.0
152 *
153 * @param int $id The ID of the cart.
154 *
155 * @return boolean
156 */
157 public function delete_cart_item( $id ) {
158 global $wpdb;
159
160 $delete = QueryHelper::delete( "{$wpdb->prefix}tutor_carts", array( 'id' => $id ) );
161
162 return $delete;
163 }
164
165 /**
166 * Delete a course from the user's cart.
167 *
168 * @since 3.0.0
169 *
170 * @param int $user_id User ID.
171 * @param int $course_id Course ID.
172 *
173 * @return boolean True if the course was successfully deleted, otherwise false.
174 */
175 public function delete_course_from_cart( $user_id, $course_id ) {
176 global $wpdb;
177
178 $user_cart = QueryHelper::get_row(
179 "{$wpdb->prefix}tutor_carts",
180 array(
181 'user_id' => $user_id,
182 ),
183 'id'
184 );
185
186 return QueryHelper::delete(
187 "{$wpdb->prefix}tutor_cart_items",
188 array(
189 'cart_id' => $user_cart->id,
190 'course_id' => $course_id,
191 )
192 );
193 }
194
195 /**
196 * Determine if a course is already added to the user's cart.
197 *
198 * @since 3.0.0
199 *
200 * @param int $user_id User ID.
201 * @param int $course_id Course ID.
202 *
203 * @return boolean True if the course is already in the cart, otherwise false.
204 */
205 public static function is_course_in_user_cart( $user_id, $course_id ) {
206 global $wpdb;
207
208 $cart_table = "{$wpdb->prefix}tutor_carts AS cart";
209 $item_table = "{$wpdb->prefix}tutor_cart_items AS item";
210
211 $join_conditions = array(
212 array(
213 'type' => 'LEFT',
214 'table' => $item_table,
215 'on' => 'cart.id = item.cart_id',
216 ),
217 );
218
219 $conditions = array(
220 'cart.user_id' => $user_id,
221 'item.course_id' => $course_id,
222 );
223
224 $select_columns = array( 'item.course_id' );
225
226 $cart_data = QueryHelper::get_joined_data(
227 $cart_table,
228 $join_conditions,
229 $select_columns,
230 $conditions,
231 array(),
232 'item.id'
233 );
234
235 return apply_filters( 'tutor_is_course_exists_in_cart', (bool) $cart_data['total_count'], $course_id );
236 }
237
238 /**
239 * Delete cart data using user id
240 *
241 * @since 3.0.0
242 *
243 * @param int $user_id User ID.
244 *
245 * @return boolean
246 */
247 public function clear_user_cart( $user_id ) {
248 return QueryHelper::delete(
249 "{$this->table_name}",
250 array(
251 'user_id' => $user_id,
252 )
253 );
254 }
255
256 }
257