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