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