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 |