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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 / ecommerce / CartController.php
tutor / ecommerce Last commit date
PaymentGateways 1 year ago AdminMenu.php 1 year ago BillingController.php 1 year ago CartController.php 1 year ago CheckoutController.php 1 year ago CouponController.php 1 year ago Ecommerce.php 1 year ago EmailController.php 1 year ago HooksHandler.php 1 year ago OptionKeys.php 1 year ago OrderActivitiesController.php 1 year ago OrderController.php 1 year ago PaymentHandler.php 1 year ago Settings.php 1 year ago Tax.php 1 year ago currency.php 1 year ago
CartController.php
276 lines
1 <?php
2 /**
3 * Manage Cart
4 *
5 * @package Tutor\Ecommerce
6 * @author Themeum
7 * @link https://themeum.com
8 * @since 3.0.0
9 */
10
11 namespace Tutor\Ecommerce;
12
13 use Tutor\Helpers\HttpHelper;
14 use TUTOR\Input;
15 use Tutor\Models\CartModel;
16 use Tutor\Traits\JsonResponse;
17
18 if ( ! defined( 'ABSPATH' ) ) {
19 exit;
20 }
21
22 /**
23 * CartController class
24 *
25 * @since 3.0.0
26 */
27 class CartController {
28
29 /**
30 * Page slug for cart page
31 *
32 * @since 3.0.0
33 *
34 * @var string
35 */
36 public const PAGE_SLUG = 'cart';
37
38 /**
39 * Page slug for cart page
40 *
41 * @since 3.0.0
42 *
43 * @var string
44 */
45 public const PAGE_ID_OPTION_NAME = 'tutor_cart_page_id';
46
47 /**
48 * Cart model
49 *
50 * @since 3.0.0
51 *
52 * @var CartModel
53 */
54 private $model;
55
56 /**
57 * Trait for sending JSON response
58 */
59 use JsonResponse;
60
61 /**
62 * Constructor.
63 *
64 * Initializes the Cart class, sets the page title, and optionally registers
65 * hooks for handling AJAX requests related to cart data, bulk actions, cart updates,
66 * and cart deletions.
67 *
68 * @param bool $register_hooks Whether to register hooks for handling requests. Default is true.
69 *
70 * @since 3.0.0
71 *
72 * @return void
73 */
74 public function __construct( $register_hooks = true ) {
75 $this->model = new CartModel();
76
77 if ( $register_hooks ) {
78 /**
79 * Handle AJAX request for adding course to cart
80 *
81 * @since 3.0.0
82 */
83 add_action( 'wp_ajax_tutor_add_course_to_cart', array( $this, 'add_course_to_cart' ) );
84
85 /**
86 * Handle AJAX request for deleting course from cart
87 *
88 * @since 3.0.0
89 */
90 add_action( 'wp_ajax_tutor_delete_course_from_cart', array( $this, 'delete_course_from_cart' ) );
91 }
92 }
93
94 /**
95 * Create cart page
96 *
97 * @since 3.0.0
98 *
99 * @return void
100 */
101 public static function create_cart_page() {
102 $page_id = self::get_page_id();
103 if ( ! $page_id ) {
104 $args = array(
105 'post_title' => ucfirst( self::PAGE_SLUG ),
106 'post_content' => '',
107 'post_type' => 'page',
108 'post_status' => 'publish',
109 );
110
111 $page_id = wp_insert_post( $args );
112 tutor_utils()->update_option( self::PAGE_ID_OPTION_NAME, $page_id );
113 }
114 }
115
116 /**
117 * Get cart page url
118 *
119 * @since 3.0.0
120 *
121 * @return string
122 */
123 public static function get_page_url() {
124 return get_post_permalink( self::get_page_id() );
125 }
126
127 /**
128 * Get cart page ID
129 *
130 * @since 3.0.0
131 *
132 * @return string
133 */
134 public static function get_page_id() {
135 return (int) tutor_utils()->get_option( self::PAGE_ID_OPTION_NAME );
136 }
137
138 /**
139 * Get cart items
140 *
141 * @since 3.0.0
142 *
143 * @return array
144 */
145 public function get_cart_items() {
146 $user_id = tutils()->get_user_id();
147 return apply_filters( 'tutor_cart_items', $this->model->get_cart_items( $user_id ), $user_id );
148 }
149
150 /**
151 * Get cart count.
152 *
153 * @since 3.0.0
154 *
155 * @param int $user_id logged in user_id.
156 *
157 * @return int
158 */
159 public function get_user_cart_item_count( $user_id = 0 ) {
160 if ( ! $user_id ) {
161 $user_id = tutils()->get_user_id();
162 }
163 $cart_items = $this->model->get_cart_items( $user_id );
164 $cart_count = $cart_items['courses']['total_count'];
165 return $cart_count;
166 }
167
168 /**
169 * Add course to cart
170 *
171 * @since 3.0.0
172 *
173 * @return void JSON response
174 */
175 public function add_course_to_cart() {
176 if ( ! tutor_utils()->is_nonce_verified() ) {
177 $this->json_response(
178 tutor_utils()->error_message( 'nonce' ),
179 null,
180 HttpHelper::STATUS_BAD_REQUEST
181 );
182 }
183
184 $user_id = tutils()->get_user_id();
185 $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
186
187 if ( ! $course_id ) {
188 $this->json_response(
189 __( 'Invalid course id.', 'tutor' ),
190 null,
191 HttpHelper::STATUS_BAD_REQUEST
192 );
193 }
194
195 // Check if the course already exists in the cart or not.
196 $is_course_in_user_cart = $this->model->is_course_in_user_cart( $user_id, $course_id );
197 if ( $is_course_in_user_cart ) {
198 $this->json_response(
199 __( 'The course is already in the cart.', 'tutor' ),
200 null,
201 HttpHelper::STATUS_BAD_REQUEST
202 );
203 }
204
205 $response = $this->model->add_course_to_cart( $user_id, $course_id );
206
207 if ( $response ) {
208 $this->json_response(
209 __( 'The course was added to the cart successfully.', 'tutor' ),
210 array(
211 'cart_page_url' => self::get_page_url(),
212 'cart_count' => self::get_user_cart_item_count( $user_id ),
213 ),
214 HttpHelper::STATUS_CREATED
215 );
216 } else {
217 $this->json_response(
218 __( 'Failed to add to cart.', 'tutor' ),
219 null,
220 HttpHelper::STATUS_BAD_REQUEST
221 );
222 }
223 }
224
225 /**
226 * Delete course from cart
227 *
228 * @since 3.0.0
229 *
230 * @return void JSON response
231 */
232 public function delete_course_from_cart() {
233 if ( ! tutor_utils()->is_nonce_verified() ) {
234 $this->json_response(
235 tutor_utils()->error_message( 'nonce' ),
236 null,
237 HttpHelper::STATUS_BAD_REQUEST
238 );
239 }
240
241 $user_id = tutils()->get_user_id();
242 $course_id = Input::post( 'course_id', 0, Input::TYPE_INT );
243
244 if ( ! $course_id ) {
245 $this->json_response(
246 __( 'Invalid course id.', 'tutor' ),
247 null,
248 HttpHelper::STATUS_BAD_REQUEST
249 );
250 }
251
252 $response = $this->model->delete_course_from_cart( $user_id, $course_id );
253
254 if ( $response ) {
255 ob_start();
256 tutor_load_template( 'ecommerce.cart' );
257 $cart_template = ob_get_clean();
258 $data = array(
259 'cart_template' => $cart_template,
260 'cart_count' => self::get_user_cart_item_count( $user_id ),
261 );
262 $this->json_response(
263 __( 'The course was removed successfully.', 'tutor' ),
264 $data,
265 HttpHelper::STATUS_OK
266 );
267 } else {
268 $this->json_response(
269 __( 'Course remove failed.', 'tutor' ),
270 null,
271 HttpHelper::STATUS_BAD_REQUEST
272 );
273 }
274 }
275 }
276