/** * Add course to cart * * @param int $course_id * @param int $quantity * @param array */ function add_to_cart($course_id, $quantity = 1, $item_data = array()) { if (!learn_press_is_enable_cart()) { $this->empty_cart(); } $course = learn_press_get_course($course_id); /* if ( !empty( $this->_cart_content['items'][$course_id] ) ) { $quantity += $this->_cart_content['items'][$course_id]['quantity']; }*/ $quantity = 1; $this->_cart_content['items'][$course_id] = apply_filters('learn_press_add_cart_item', array('item_id' => $course_id, 'quantity' => $quantity, 'subtotal' => $course->get_price() * $quantity, 'total' => $course->get_price() * $quantity, 'data' => $item_data)); do_action('learn_press_add_to_cart', $course_id, $quantity, $item_data, $this); $button = ''; if (learn_press_is_enable_cart()) { if (LP()->settings->get('redirect_after_add') == 'yes') { $redirect = learn_press_get_page_link('cart'); $button = sprintf('<a href="%s">%s</a>', get_the_permalink($course_id), __('Back to class', 'learn_press')); } else { $redirect = get_the_permalink($course_id); $button = sprintf('<a href="%s">%s</a>', learn_press_get_page_link('cart'), __('View cart', 'learn_press')); } } else { $redirect = learn_press_get_page_link('checkout'); } $redirect = apply_filters('learn_press_add_to_cart_redirect', $redirect, $course_id); learn_press_add_notice(sprintf(__('<strong>%s</strong> has been added to your cart. %s', 'learn_press'), get_the_title($course_id), $button)); if (is_ajax()) { learn_press_send_json(array('redirect' => $redirect, 'result' => 'success', 'messages' => learn_press_get_notices(true))); } else { wp_redirect($redirect); die; } }
/** * Process checkout * * @throws Exception */ function process_checkout() { try { if (strtolower($_SERVER['REQUEST_METHOD']) != 'post') { return; } // Prevent timeout @set_time_limit(0); do_action('learn_press_before_checkout_process'); $success = true; if (LP()->cart->is_empty()) { learn_press_send_json(array('result' => 'success', 'redirect' => learn_press_get_page_link('checkout'))); } if (empty($_REQUEST['payment_method'])) { $success = false; learn_press_add_notice(__('Please select a payment method', 'learn_press'), 'error'); } else { $this->payment_method = $_REQUEST['payment_method']; if ($this->checkout_fields) { foreach ($this->checkout_fields as $name => $field) { if (!apply_filters('learn_press_checkout_validate_field', true, array('name' => $name, 'text' => $field), $this)) { $success = false; } } } if (isset($this->checkout_fields['user_login']) && isset($this->checkout_fields['user_password'])) { $creds = array(); $creds['user_login'] = $_POST['user_login']; $creds['user_password'] = $_POST['user_password']; $creds['remember'] = true; $user = wp_signon($creds, false); if (is_wp_error($user)) { learn_press_add_notice($user->get_error_message(), 'error'); $success = false; } } LP()->session->set('chosen_payment_method', $this->payment_method); } if ($success && LP()->cart->needs_payment()) { // Payment Method $available_gateways = LP_Gateways::instance()->get_available_payment_gateways(); if (!isset($available_gateways[$this->payment_method])) { $this->payment_method = ''; learn_press_add_notice(__('Invalid payment method.', 'learn_press'), 'error'); } else { $this->payment_method = $available_gateways[$this->payment_method]; $success = $this->payment_method->validate_fields(); } } else { $available_gateways = array(); } $order_id = $this->create_order(); if ($success && $this->payment_method && $order_id) { LP()->session->order_awaiting_payment = $order_id; // Process Payment $result = $this->payment_method->process_payment($order_id); $success = !empty($result['result']) ? $result['result'] == 'success' : false; // Redirect to success/confirmation/payment page if ($success) { $result = apply_filters('learn_press_payment_successful_result', $result, $order_id); //LP()->cart->empty_cart(); if (is_ajax()) { learn_press_send_json($result); } else { wp_redirect($result['redirect']); exit; } } } } catch (Exception $e) { if (!empty($e)) { learn_press_add_notice($e->getMessage(), 'error'); } $success = false; } $error_messages = ''; if (!$success) { ob_start(); learn_press_print_notices(); $error_messages = ob_get_clean(); } $result = array('result' => $success ? 'success' : 'fail', 'messages' => $error_messages, 'redirect' => ''); return $result; }
/** * Display a message immediately with out push into queue * * @param $message * @param string $type */ function learn_press_display_message($message, $type = 'success') { // get all notices added into queue $notices = LP_Session::get('notices'); LP_Session::set('notices', null); // add new notice and display learn_press_add_notice($message, $type); echo learn_press_get_notices(true); // store back notices LP_Session::set('notices', $notices); }
/** * Enroll this user to a course * * @param $course_id * * @return int|void * @throws Exception */ function enroll($course_id) { if (!$this->can('enroll-course', $course_id)) { learn_press_add_notice(__('Sorry! You can not enroll this course. Please try again or contact site admin'), 'error'); return; } global $wpdb; $inserted = 0; do_action('learn_press_before_enroll_course', $this, $course_id); if ($wpdb->insert($wpdb->learnpress_user_courses, array('user_id' => $this->id, 'course_id' => $course_id, 'start_time' => current_time('mysql'), 'status' => 'enrolled', 'end_time' => null, 'order_id' => $this->get_course_order($course_id)), array('%d', '%d', '%s', '%s', '%s'))) { $inserted = $wpdb->insert_id; do_action('learn_press_user_enrolled_course', $this, $course_id, $inserted); } else { do_action('learn_press_user_enroll_course_failed', $this, $course_id, $inserted); } return $inserted; }
static function do_ajax($var) { if (!defined('DOING_AJAX')) { define('DOING_AJAX', true); } LP_Gateways::instance()->get_available_payment_gateways(); $result = false; switch ($var) { case 'checkout': $result = LP()->checkout->process_checkout(); break; case 'enroll-course': $course_id = learn_press_get_request('enroll-course'); if (!$course_id) { throw new Exception(__('Invalid course', 'learn_press')); } $insert_id = LP()->user->enroll($course_id); $response = array('result' => 'fail', 'redirect' => apply_filters('learn_press_enroll_course_failure_redirect_url', get_the_permalink($course_id))); if ($insert_id) { $response['result'] = 'success'; $response['redirect'] = apply_filters('learn_press_enrolled_course_redirect_url', get_the_permalink($course_id)); $message = apply_filters('learn_press_enrolled_course_message', sprintf(__('Congrats! You have enrolled <strong>%s</strong>', 'learn_press'), get_the_title($course_id)), $course_id, LP()->user->id); learn_press_add_notice($message); } else { $message = apply_filters('learn_press_enroll_course_failed_message', sprintf(__('Sorry! The course <strong>%s</strong> you want to enroll has failed! Please contact site\'s administrator for more information.', 'learn_press'), get_the_title($course_id)), $course_id, LP()->user->id); learn_press_add_notice($message, 'error'); } if (is_ajax()) { learn_press_send_json($response); } if ($response['redirect']) { wp_redirect($response['redirect']); } break; case 'checkout-login': $result = array('result' => 'success'); ob_start(); if (empty($_REQUEST['user_login'])) { $result['result'] = 'fail'; learn_press_add_notice(__('Please enter username', 'learn_press'), 'error'); } if (empty($_REQUEST['user_password'])) { $result['result'] = 'fail'; learn_press_add_notice(__('Please enter password', 'learn_press'), 'error'); } if ($result['result'] == 'success') { $creds = array(); $creds['user_login'] = $_REQUEST['user_login']; $creds['user_password'] = $_REQUEST['user_password']; $creds['remember'] = true; $user = wp_signon($creds, false); if (is_wp_error($user)) { $result['result'] = 'fail'; learn_press_add_notice($user->get_error_message(), 'error'); } else { $result['redirect'] = learn_press_get_page_link('checkout'); } } learn_press_print_notices(); $messages = ob_get_clean(); if ($result['result'] == 'fail') { $result['messages'] = $messages; } break; case 'add-to-cart': $result = LP()->cart->add_to_cart(learn_press_get_request('add-course-to-cart')); } learn_press_send_json($result); }
<?php /** * @author ThimPress * @package LearnPress/Templates * @version 1.0 */ if (!defined('ABSPATH')) { exit; } learn_press_add_notice(__('Your cart is currently empty.', 'learn_press'), 'error'); learn_press_print_notices(); ?> <a href="<?php echo learn_press_get_page_link('courses'); ?> "><?php _e('Back to class', 'learn_press'); ?> </a>