| @@ -9,9 +9,8 @@ | ||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | namespace Tutor\Models; |
| 12 | 12 | |
| 13 | -use Tutor\Cache\TutorCache; | |
| 14 | 13 | use Tutor\Helpers\QueryHelper; |
| 15 | 14 | |
| 16 | 15 | /** |
| 17 | 16 | * Cart model class for performing cart functionalities |
| @@ -45,9 +44,9 @@ | ||
| 45 | 44 | * @param int $course_id Course ID. |
| 46 | 45 | * @param string $item_type Cart item type. |
| 47 | 46 | * @param mixed $item_details Cart item details. |
| 48 | 47 | * |
| 49 | - * @return int Inserted row ID on success, 0 otherwise. | |
| 48 | + * @return array Array containing the result of the insert operation. | |
| 50 | 49 | */ |
| 51 | 50 | public function add_course_to_cart( $user_id, $course_id, $item_type = '', $item_details = '' ) { |
| 52 | 51 | global $wpdb; |
| 53 | 52 | |
| @@ -73,14 +72,8 @@ | ||
| 73 | 72 | ) |
| 74 | 73 | ); |
| 75 | 74 | } |
| 76 | 75 | |
| 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 | 76 | return QueryHelper::insert( |
| 84 | 77 | "{$wpdb->prefix}tutor_cart_items", |
| 85 | 78 | array( |
| 86 | 79 | 'cart_id' => $user_cart_id, |
| @@ -85,9 +78,9 @@ | ||
| 85 | 78 | array( |
| 86 | 79 | 'cart_id' => $user_cart_id, |
| 87 | 80 | 'course_id' => $course_id, |
| 88 | 81 | 'item_type' => $item_type, |
| 89 | - 'item_details' => $item_details, | |
| 82 | + 'item_details' => $item_details ? wp_json_encode( $item_details ) : null, | |
| 90 | 83 | ) |
| 91 | 84 | ); |
| 92 | 85 | } |
| 93 | 86 | |
| @@ -101,15 +94,10 @@ | ||
| 101 | 94 | * |
| 102 | 95 | * @return array Array containing the cart items and their total count. |
| 103 | 96 | */ |
| 104 | 97 | 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 ); | |
| 98 | + global $wpdb; | |
| 107 | 99 | |
| 108 | - if ( $cached ) { | |
| 109 | - return $cached; | |
| 110 | - } | |
| 111 | - | |
| 112 | 100 | $cart_data = array( |
| 113 | 101 | 'cart' => null, |
| 114 | 102 | 'courses' => array( |
| 115 | 103 | 'total_count' => 0, |
| @@ -117,9 +105,9 @@ | ||
| 117 | 105 | ), |
| 118 | 106 | ); |
| 119 | 107 | |
| 120 | 108 | $user_cart = QueryHelper::get_row( |
| 121 | - 'tutor_carts', | |
| 109 | + "{$wpdb->prefix}tutor_carts", | |
| 122 | 110 | array( |
| 123 | 111 | 'user_id' => $user_id, |
| 124 | 112 | ), |
| 125 | 113 | 'id' |
| @@ -127,20 +115,17 @@ | ||
| 127 | 115 | |
| 128 | 116 | if ( $user_cart ) { |
| 129 | 117 | $cart_data['cart'] = $user_cart; |
| 130 | 118 | |
| 131 | - $primary_table = 'tutor_cart_items AS item'; | |
| 119 | + $primary_table = "{$wpdb->prefix}tutor_cart_items AS item"; | |
| 132 | 120 | $joining_tables = array( |
| 133 | 121 | array( |
| 134 | 122 | 'type' => 'LEFT', |
| 135 | - 'table' => 'posts AS post', | |
| 123 | + 'table' => "{$wpdb->prefix}posts AS post", | |
| 136 | 124 | 'on' => 'item.course_id = post.ID', |
| 137 | 125 | ), |
| 138 | 126 | ); |
| 139 | - $where = array( | |
| 140 | - 'item.cart_id' => $user_cart->id, | |
| 141 | - 'post.post_status' => 'publish', | |
| 142 | - ); | |
| 127 | + $where = array( 'item.cart_id' => $user_cart->id ); | |
| 143 | 128 | $select_columns = array( 'post.*' ); |
| 144 | 129 | $cart_data['courses'] = QueryHelper::get_joined_data( |
| 145 | 130 | $primary_table, |
| 146 | 131 | $joining_tables, |
| @@ -146,17 +131,13 @@ | ||
| 146 | 131 | $joining_tables, |
| 147 | 132 | $select_columns, |
| 148 | 133 | $where, |
| 149 | 134 | array(), |
| 150 | - 'item.id', | |
| 151 | - -1 | |
| 135 | + 'item.id' | |
| 152 | 136 | ); |
| 153 | 137 | } |
| 154 | 138 | |
| 155 | - $result = $is_details ? $cart_data : $cart_data['courses']['results']; | |
| 156 | - TutorCache::set( $cache_key, $result ); | |
| 157 | - | |
| 158 | - return $result; | |
| 139 | + return $is_details ? $cart_data : $cart_data['courses']['results']; | |
| 159 | 140 | } |
| 160 | 141 | |
| 161 | 142 | /** |
| 162 | 143 | * Check if the user has any items in their cart. |
| @@ -279,5 +260,6 @@ | ||
| 279 | 260 | 'user_id' => $user_id, |
| 280 | 261 | ) |
| 281 | 262 | ); |
| 282 | 263 | } |
| 264 | + | |
| 283 | 265 | } |