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 |