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