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