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.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.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 11 months ago BillingModel.php 3 weeks ago CartItemModel.php 11 months ago CartModel.php 3 days ago CouponModel.php 1 month ago CourseModel.php 3 days ago EnrollmentModel.php 3 weeks ago LessonModel.php 10 months ago OrderActivitiesModel.php 1 year ago OrderItemMetaModel.php 11 months ago OrderItemModel.php 11 months ago OrderMetaModel.php 1 year ago OrderModel.php 1 month ago QuizModel.php 3 weeks ago UserModel.php 1 year ago WithdrawModel.php 1 month ago
CartModel.php
284 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 int Inserted row ID on success, 0 otherwise.
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 -1
152 );
153 }
154
155 $result = $is_details ? $cart_data : $cart_data['courses']['results'];
156 TutorCache::set( $cache_key, $result );
157
158 return $result;
159 }
160
161 /**
162 * Check if the user has any items in their cart.
163 *
164 * @param int $user_id User ID.
165 * @return bool True if the user has items in the cart, false otherwise.
166 */
167 public function has_item_in_cart( $user_id ) {
168 $get_cart = $this->get_cart_items( $user_id );
169 $courses = $get_cart['courses'];
170 $total_count = $courses['total_count'];
171
172 $has = (int) $total_count > 0;
173 return apply_filters( 'tutor_is_cart_empty', $has );
174 }
175
176 /**
177 * Delete cart item.
178 *
179 * @since 3.0.0
180 *
181 * @param int $id The ID of the cart.
182 *
183 * @return boolean
184 */
185 public function delete_cart_item( $id ) {
186 global $wpdb;
187
188 $delete = QueryHelper::delete( "{$wpdb->prefix}tutor_carts", array( 'id' => $id ) );
189
190 return $delete;
191 }
192
193 /**
194 * Delete a course from the user's cart.
195 *
196 * @since 3.0.0
197 *
198 * @param int $user_id User ID.
199 * @param int $course_id Course ID.
200 *
201 * @return boolean True if the course was successfully deleted, otherwise false.
202 */
203 public function delete_course_from_cart( $user_id, $course_id ) {
204 global $wpdb;
205
206 $user_cart = QueryHelper::get_row(
207 "{$wpdb->prefix}tutor_carts",
208 array(
209 'user_id' => $user_id,
210 ),
211 'id'
212 );
213
214 return QueryHelper::delete(
215 "{$wpdb->prefix}tutor_cart_items",
216 array(
217 'cart_id' => $user_cart->id,
218 'course_id' => $course_id,
219 )
220 );
221 }
222
223 /**
224 * Determine if a course is already added to the user's cart.
225 *
226 * @since 3.0.0
227 *
228 * @param int $user_id User ID.
229 * @param int $course_id Course ID.
230 *
231 * @return boolean True if the course is already in the cart, otherwise false.
232 */
233 public static function is_course_in_user_cart( $user_id, $course_id ) {
234 global $wpdb;
235
236 $cart_table = "{$wpdb->prefix}tutor_carts AS cart";
237 $item_table = "{$wpdb->prefix}tutor_cart_items AS item";
238
239 $join_conditions = array(
240 array(
241 'type' => 'LEFT',
242 'table' => $item_table,
243 'on' => 'cart.id = item.cart_id',
244 ),
245 );
246
247 $conditions = array(
248 'cart.user_id' => $user_id,
249 'item.course_id' => $course_id,
250 );
251
252 $select_columns = array( 'item.course_id' );
253
254 $cart_data = QueryHelper::get_joined_data(
255 $cart_table,
256 $join_conditions,
257 $select_columns,
258 $conditions,
259 array(),
260 'item.id'
261 );
262
263 return apply_filters( 'tutor_is_course_exists_in_cart', (bool) $cart_data['total_count'], $course_id );
264 }
265
266 /**
267 * Delete cart data using user id
268 *
269 * @since 3.0.0
270 *
271 * @param int $user_id User ID.
272 *
273 * @return boolean
274 */
275 public function clear_user_cart( $user_id ) {
276 return QueryHelper::delete(
277 "{$this->table_name}",
278 array(
279 'user_id' => $user_id,
280 )
281 );
282 }
283 }
284