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 $this->model->get_cart_items( $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 |