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.0 3.6.0 3.6.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 10 months ago BillingModel.php 1 year ago CartItemModel.php 10 months ago CartModel.php 17 hours ago CouponModel.php 17 hours ago CourseModel.php 17 hours ago EnrollmentModel.php 17 hours ago LessonModel.php 9 months ago OrderActivitiesModel.php 1 year ago OrderItemMetaModel.php 10 months ago OrderItemModel.php 10 months ago OrderMetaModel.php 1 year ago OrderModel.php 17 hours ago QuizModel.php 17 hours ago UserModel.php 1 year ago WithdrawModel.php 17 hours ago
CartModel.php
283 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\Cache\TutorCache;
14 use Tutor\Helpers\QueryHelper;
15
16 /**
17 * Cart model class for performing cart functionalities
18 */
19 class CartModel {
20
21 /**
22 * Cart table name
23 *
24 * @since 3.0.0
25 *
26 * @var string
27 */
28 private $table_name = 'tutor_carts';
29
30 /**
31 * Resolve props
32 *
33 * @since 3.0.0
34 */
35 public function __construct() {
36 $this->table_name = $GLOBALS['wpdb']->prefix . $this->table_name;
37 }
38
39 /**
40 * Add a course to the user's cart.
41 *
42 * @since 3.8.0 $item_type & $items_details Param added
43 *
44 * @param int $user_id User ID.
45 * @param int $course_id Course ID.
46 * @param string $item_type Cart item type.
47 * @param mixed $item_details Cart item details.
48 *
49 * @return array Array containing the result of the insert operation.
50 */
51 public function add_course_to_cart( $user_id, $course_id, $item_type = '', $item_details = '' ) {
52 global $wpdb;
53
54 $current_time = current_time( 'mysql', true );
55 $user_cart_id = 0;
56
57 $user_cart = QueryHelper::get_row(
58 "{$wpdb->prefix}tutor_carts",
59 array(
60 'user_id' => $user_id,
61 ),
62 'id'
63 );
64
65 if ( $user_cart ) {
66 $user_cart_id = $user_cart->id;
67 } else {
68 $user_cart_id = QueryHelper::insert(
69 "{$wpdb->prefix}tutor_carts",
70 array(
71 'user_id' => $user_id,
72 'created_at_gmt' => $current_time,
73 )
74 );
75 }
76
77 if ( 'gift' === $item_type ) {
78 $item_details = wp_json_encode( $item_details, JSON_UNESCAPED_UNICODE );
79 } else {
80 $item_details = wp_json_encode( $item_details );
81 }
82
83 return QueryHelper::insert(
84 "{$wpdb->prefix}tutor_cart_items",
85 array(
86 'cart_id' => $user_cart_id,
87 'course_id' => $course_id,
88 'item_type' => $item_type,
89 'item_details' => $item_details,
90 )
91 );
92 }
93
94 /**
95 * Get items from the user's cart.
96 *
97 * @since 3.8.0 $is_details param added.
98 *
99 * @param int $user_id User ID.
100 * @param bool $is_details If false then just cart items will be returned.
101 *
102 * @return array Array containing the cart items and their total count.
103 */
104 public function get_cart_items( $user_id, $is_details = true ) {
105 $cache_key = "tutor_get_cart_items_{$user_id}_{$is_details}";
106 $cached = TutorCache::get( $cache_key );
107
108 if ( $cached ) {
109 return $cached;
110 }
111
112 $cart_data = array(
113 'cart' => null,
114 'courses' => array(
115 'total_count' => 0,
116 'results' => array(),
117 ),
118 );
119
120 $user_cart = QueryHelper::get_row(
121 'tutor_carts',
122 array(
123 'user_id' => $user_id,
124 ),
125 'id'
126 );
127
128 if ( $user_cart ) {
129 $cart_data['cart'] = $user_cart;
130
131 $primary_table = 'tutor_cart_items AS item';
132 $joining_tables = array(
133 array(
134 'type' => 'LEFT',
135 'table' => 'posts AS post',
136 'on' => 'item.course_id = post.ID',
137 ),
138 );
139 $where = array(
140 'item.cart_id' => $user_cart->id,
141 'post.post_status' => 'publish',
142 );
143 $select_columns = array( 'post.*' );
144 $cart_data['courses'] = QueryHelper::get_joined_data(
145 $primary_table,
146 $joining_tables,
147 $select_columns,
148 $where,
149 array(),
150 'item.id'
151 );
152 }
153
154 $result = $is_details ? $cart_data : $cart_data['courses']['results'];
155 TutorCache::set( $cache_key, $result );
156
157 return $result;
158 }
159
160 /**
161 * Check if the user has any items in their cart.
162 *
163 * @param int $user_id User ID.
164 * @return bool True if the user has items in the cart, false otherwise.
165 */
166 public function has_item_in_cart( $user_id ) {
167 $get_cart = $this->get_cart_items( $user_id );
168 $courses = $get_cart['courses'];
169 $total_count = $courses['total_count'];
170
171 $has = (int) $total_count > 0;
172 return apply_filters( 'tutor_is_cart_empty', $has );
173 }
174
175 /**
176 * Delete cart item.
177 *
178 * @since 3.0.0
179 *
180 * @param int $id The ID of the cart.
181 *
182 * @return boolean
183 */
184 public function delete_cart_item( $id ) {
185 global $wpdb;
186
187 $delete = QueryHelper::delete( "{$wpdb->prefix}tutor_carts", array( 'id' => $id ) );
188
189 return $delete;
190 }
191
192 /**
193 * Delete a course from the user's cart.
194 *
195 * @since 3.0.0
196 *
197 * @param int $user_id User ID.
198 * @param int $course_id Course ID.
199 *
200 * @return boolean True if the course was successfully deleted, otherwise false.
201 */
202 public function delete_course_from_cart( $user_id, $course_id ) {
203 global $wpdb;
204
205 $user_cart = QueryHelper::get_row(
206 "{$wpdb->prefix}tutor_carts",
207 array(
208 'user_id' => $user_id,
209 ),
210 'id'
211 );
212
213 return QueryHelper::delete(
214 "{$wpdb->prefix}tutor_cart_items",
215 array(
216 'cart_id' => $user_cart->id,
217 'course_id' => $course_id,
218 )
219 );
220 }
221
222 /**
223 * Determine if a course is already added to the user's cart.
224 *
225 * @since 3.0.0
226 *
227 * @param int $user_id User ID.
228 * @param int $course_id Course ID.
229 *
230 * @return boolean True if the course is already in the cart, otherwise false.
231 */
232 public static function is_course_in_user_cart( $user_id, $course_id ) {
233 global $wpdb;
234
235 $cart_table = "{$wpdb->prefix}tutor_carts AS cart";
236 $item_table = "{$wpdb->prefix}tutor_cart_items AS item";
237
238 $join_conditions = array(
239 array(
240 'type' => 'LEFT',
241 'table' => $item_table,
242 'on' => 'cart.id = item.cart_id',
243 ),
244 );
245
246 $conditions = array(
247 'cart.user_id' => $user_id,
248 'item.course_id' => $course_id,
249 );
250
251 $select_columns = array( 'item.course_id' );
252
253 $cart_data = QueryHelper::get_joined_data(
254 $cart_table,
255 $join_conditions,
256 $select_columns,
257 $conditions,
258 array(),
259 'item.id'
260 );
261
262 return apply_filters( 'tutor_is_course_exists_in_cart', (bool) $cart_data['total_count'], $course_id );
263 }
264
265 /**
266 * Delete cart data using user id
267 *
268 * @since 3.0.0
269 *
270 * @param int $user_id User ID.
271 *
272 * @return boolean
273 */
274 public function clear_user_cart( $user_id ) {
275 return QueryHelper::delete(
276 "{$this->table_name}",
277 array(
278 'user_id' => $user_id,
279 )
280 );
281 }
282 }
283