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 |