Addons.php
4 years ago
Admin.php
4 years ago
Ajax.php
4 years ago
Assets.php
4 years ago
Course.php
4 years ago
Course_Filter.php
4 years ago
Course_Settings_Tabs.php
5 years ago
Course_Widget.php
5 years ago
Custom_Validation.php
5 years ago
Dashboard.php
5 years ago
Email.php
5 years ago
FormHandler.php
5 years ago
Frontend.php
5 years ago
Gutenberg.php
4 years ago
Instructor.php
4 years ago
Instructors_List.php
4 years ago
Lesson.php
4 years ago
Options.php
4 years ago
Post_types.php
4 years ago
Private_Course_Access.php
5 years ago
Q_and_A.php
5 years ago
Question_Answers_List.php
4 years ago
Quiz.php
4 years ago
Quiz_Attempts_List.php
4 years ago
RestAPI.php
5 years ago
Rewrite_Rules.php
4 years ago
Shortcode.php
4 years ago
Student.php
4 years ago
Students_List.php
4 years ago
Taxonomies.php
5 years ago
Template.php
4 years ago
Theme_Compatibility.php
5 years ago
Tools.php
5 years ago
Tutor.php
4 years ago
TutorEDD.php
5 years ago
Tutor_Base.php
5 years ago
Tutor_List_Table.php
4 years ago
Tutor_Setup.php
4 years ago
Upgrader.php
5 years ago
User.php
4 years ago
Utils.php
4 years ago
Video_Stream.php
5 years ago
Withdraw.php
5 years ago
Withdraw_Requests_List.php
4 years ago
WooCommerce.php
4 years ago
WooCommerce.php
592 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Created by PhpStorm. |
| 5 | * User: themeum |
| 6 | * Date: 1/10/18 |
| 7 | * Time: 3:01 PM |
| 8 | */ |
| 9 | |
| 10 | namespace TUTOR; |
| 11 | |
| 12 | if (!defined('ABSPATH')) |
| 13 | exit; |
| 14 | |
| 15 | class WooCommerce extends Tutor_Base { |
| 16 | |
| 17 | public function __construct() { |
| 18 | parent::__construct(); |
| 19 | |
| 20 | add_action('tutor_options_before_woocommerce', array($this, 'notice_before_option')); |
| 21 | |
| 22 | //Add option settings |
| 23 | add_filter('tutor_monetization_options', array($this, 'tutor_monetization_options')); |
| 24 | add_filter('tutor/options/attr', array($this, 'add_options')); |
| 25 | |
| 26 | $monetize_by = tutils()->get_option('monetize_by'); |
| 27 | if ($monetize_by !== 'wc') { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Is Course Purchasable |
| 33 | */ |
| 34 | add_filter('is_course_purchasable', array($this, 'is_course_purchasable'), 10, 2); |
| 35 | add_filter('get_tutor_course_price', array($this, 'get_tutor_course_price'), 10, 2); |
| 36 | add_filter('tutor_course_sell_by', array($this, 'tutor_course_sell_by')); |
| 37 | |
| 38 | add_filter('product_type_options', array($this, 'add_tutor_type_in_wc_product')); |
| 39 | |
| 40 | add_action('add_meta_boxes', array($this, 'register_meta_box')); |
| 41 | add_action('save_post_' . $this->course_post_type, array($this, 'save_course_meta')); |
| 42 | add_action('save_post_product', array($this, 'save_wc_product_meta')); |
| 43 | |
| 44 | add_action('tutor_course/single/before/enroll', 'wc_print_notices'); |
| 45 | |
| 46 | /** |
| 47 | * After place new order |
| 48 | */ |
| 49 | add_action('woocommerce_new_order', array($this, 'course_placing_order_from_admin'), 10, 3); |
| 50 | add_action('woocommerce_new_order_item', array($this, 'course_placing_order_from_customer'), 10, 3); |
| 51 | |
| 52 | /** |
| 53 | * Order Status Hook |
| 54 | * |
| 55 | * Remove course from active courses if an order is cancelled or refunded |
| 56 | */ |
| 57 | add_action('woocommerce_order_status_changed', array($this, 'enrolled_courses_status_change'), 10, 3); |
| 58 | |
| 59 | /** |
| 60 | * Add Earning Data |
| 61 | */ |
| 62 | add_action('woocommerce_new_order_item', array($this, 'add_earning_data'), 10, 3); |
| 63 | add_action('woocommerce_order_status_changed', array($this, 'add_earning_data_status_change'), 10, 3); |
| 64 | |
| 65 | /** |
| 66 | * WC Print Notices After Enroll |
| 67 | * @since v.1.3.5 |
| 68 | */ |
| 69 | if (tutils()->has_wc()) { |
| 70 | add_action('tutor_course/single/before/inner-wrap', 'wc_print_notices', 10); |
| 71 | add_action('tutor_course/single/enrolled/before/inner-wrap', 'wc_print_notices', 10); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Manage WooCommerce plugin dependency |
| 76 | * @since v.1.7.8 |
| 77 | */ |
| 78 | $woocommerce_path = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'woocommerce' . DIRECTORY_SEPARATOR . 'woocommerce.php'; |
| 79 | register_deactivation_hook( $woocommerce_path, array($this, 'disable_tutor_monetization') ); |
| 80 | /** |
| 81 | * Redirect student on enrolled courses after course |
| 82 | * enrollment complete |
| 83 | * @since 1.9.0 |
| 84 | */ |
| 85 | add_action( 'woocommerce_thankyou', array($this, 'redirect_to_enrolled_courses') ); |
| 86 | |
| 87 | /** |
| 88 | * Change woo commerce cart product link if it is tutor product |
| 89 | */ |
| 90 | add_filter( 'woocommerce_cart_item_permalink', array( $this, 'tutor_update_product_url' ), 10, 2 ); |
| 91 | } |
| 92 | |
| 93 | public function notice_before_option() { |
| 94 | $has_wc = tutor_utils()->has_wc(); |
| 95 | if ($has_wc) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | ob_start(); |
| 100 | ?> |
| 101 | <div class="tutor-notice-warning"> |
| 102 | <p> |
| 103 | <?php _e(' Seems like you don’t have WooCommerce plugin installed on your site. In order to use this functionality, you need to have the |
| 104 | WooCommerce plugin installed. Get back on this page after installing the plugin and enable the following feature to start selling |
| 105 | courses with Tutor.', 'tutor'); ?> |
| 106 | </p> |
| 107 | <p><?php _e('This notice will disappear after activating <strong>WooCommerce</strong>', 'tutor'); ?></p> |
| 108 | </div> |
| 109 | <?php |
| 110 | echo ob_get_clean(); |
| 111 | } |
| 112 | |
| 113 | public function is_course_purchasable($bool, $course_id) { |
| 114 | if (!tutor_utils()->has_wc()) { |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | $course_id = tutor_utils()->get_post_id($course_id); |
| 119 | $has_product_id = get_post_meta($course_id, '_tutor_course_product_id', true); |
| 120 | if ($has_product_id) { |
| 121 | return true; |
| 122 | } |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | public function get_tutor_course_price($price, $course_id) { |
| 127 | $price = null; |
| 128 | |
| 129 | if (tutor_utils()->is_course_purchasable($course_id)) { |
| 130 | if (tutor_utils()->has_wc()) { |
| 131 | $product_id = tutor_utils()->get_course_product_id($course_id); |
| 132 | $product = wc_get_product($product_id); |
| 133 | |
| 134 | if ($product) { |
| 135 | ob_start(); |
| 136 | ?> |
| 137 | <div class="price"> |
| 138 | <?php echo $product->get_price_html(); ?> |
| 139 | </div> |
| 140 | <?php |
| 141 | return ob_get_clean(); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return $price; |
| 147 | } |
| 148 | |
| 149 | public function tutor_course_sell_by() { |
| 150 | return 'woocommerce'; |
| 151 | } |
| 152 | |
| 153 | public function add_tutor_type_in_wc_product($types) { |
| 154 | $types['tutor_product'] = array( |
| 155 | 'id' => '_tutor_product', |
| 156 | 'wrapper_class' => 'show_if_simple', |
| 157 | 'label' => __('For Tutor', 'tutor'), |
| 158 | 'description' => __('This checkmark ensure that you will sell a specif course via this product.', 'tutor'), |
| 159 | 'default' => 'no', |
| 160 | ); |
| 161 | |
| 162 | return $types; |
| 163 | } |
| 164 | |
| 165 | public function register_meta_box() { |
| 166 | add_meta_box('tutor-attach-product', __('Add Product', 'tutor'), array($this, 'course_add_product_metabox'), $this->course_post_type, 'advanced', 'high'); |
| 167 | } |
| 168 | |
| 169 | public function course_add_product_metabox() { |
| 170 | include tutor()->path . 'views/metabox/course-add-product-metabox.php'; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * @param $post_ID |
| 175 | * |
| 176 | * Save course meta for attaching product |
| 177 | */ |
| 178 | public function save_course_meta($post_ID) { |
| 179 | $product_id = (int)sanitize_text_field( tutor_utils()->avalue_dot('_tutor_course_product_id', $_POST, 0) ) ; |
| 180 | |
| 181 | if ($product_id === -1) { |
| 182 | delete_post_meta($post_ID, '_tutor_course_product_id'); |
| 183 | } else if ($product_id) { |
| 184 | update_post_meta($post_ID, '_tutor_course_product_id', $product_id); |
| 185 | //Mark product for woocommerce |
| 186 | update_post_meta($product_id, '_virtual', 'yes'); |
| 187 | update_post_meta($product_id, '_tutor_product', 'yes'); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | public function save_wc_product_meta($post_ID) { |
| 192 | $is_tutor_product = sanitize_text_field( tutor_utils()->avalue_dot('_tutor_product', $_POST) ); |
| 193 | if ($is_tutor_product === 'on') { |
| 194 | update_post_meta($post_ID, '_tutor_product', 'yes'); |
| 195 | } else { |
| 196 | delete_post_meta($post_ID, '_tutor_product'); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * |
| 202 | * Take enrolled course action based on order status change |
| 203 | */ |
| 204 | public function enrolled_courses_status_change($order_id, $status_from, $status_to) { |
| 205 | if (!tutor_utils()->is_tutor_order($order_id)) { |
| 206 | return; |
| 207 | } |
| 208 | global $wpdb; |
| 209 | |
| 210 | $enrolled_ids_with_course = $this->get_course_enrolled_ids_by_order_id($order_id); |
| 211 | |
| 212 | if ($enrolled_ids_with_course) { |
| 213 | $enrolled_ids = wp_list_pluck($enrolled_ids_with_course, 'enrolled_id'); |
| 214 | |
| 215 | if (is_array($enrolled_ids) && count($enrolled_ids)) { |
| 216 | foreach ($enrolled_ids as $enrolled_id) { |
| 217 | |
| 218 | tutils()->course_enrol_status_change($enrolled_id, $status_to); |
| 219 | |
| 220 | // Invoke enrolled hook |
| 221 | if($status_to == 'completed'){ |
| 222 | $user_id = get_post_field('post_author', $enrolled_id); |
| 223 | $course_id = get_post_field('post_parent', $enrolled_id); |
| 224 | do_action('tutor_after_enrolled', $course_id, $user_id, $enrolled_id); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * @param $order_id |
| 233 | * |
| 234 | * @return array|bool |
| 235 | */ |
| 236 | public function get_course_enrolled_ids_by_order_id($order_id) { |
| 237 | global $wpdb; |
| 238 | //Getting all of courses ids within this order |
| 239 | |
| 240 | $courses_ids = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key LIKE '_tutor_order_for_course_id_%' ", $order_id)); |
| 241 | |
| 242 | if (is_array($courses_ids) && count($courses_ids)) { |
| 243 | $course_enrolled_by_order = array(); |
| 244 | foreach ($courses_ids as $courses_id) { |
| 245 | $course_id = str_replace('_tutor_order_for_course_id_', '', $courses_id->meta_key); |
| 246 | //array(order_id => array('course_id' => $course_id, 'enrolled_id' => enrolled_id, 'order_id' => $courses_id->post_id)) |
| 247 | $course_enrolled_by_order[] = array('course_id' => $course_id, 'enrolled_id' => $courses_id->meta_value, 'order_id' => $courses_id->post_id); |
| 248 | } |
| 249 | return $course_enrolled_by_order; |
| 250 | } |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Remove course |
| 256 | * |
| 257 | * TODO: right now it's unused |
| 258 | */ |
| 259 | public function remove_active_course($order_id) { |
| 260 | global $wpdb; |
| 261 | //Getting all of courses ids within this order |
| 262 | |
| 263 | $courses_ids = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->postmeta} WHERE post_id = %d meta_key LIKE '_tutor_order_for_course_id_%' ", $order_id)); |
| 264 | } |
| 265 | |
| 266 | |
| 267 | /** |
| 268 | * @param $attr |
| 269 | * |
| 270 | * @return mixed |
| 271 | * |
| 272 | * Add option for WooCommerce settings |
| 273 | */ |
| 274 | public function add_options($attr) { |
| 275 | |
| 276 | $attr['woocommerce'] = array( |
| 277 | 'label' => __('WooCommerce', 'tutor'), |
| 278 | |
| 279 | 'sections' => array( |
| 280 | 'general' => array( |
| 281 | 'label' => __('General', 'tutor'), |
| 282 | 'desc' => __('WooCommerce Settings', 'tutor'), |
| 283 | 'fields' => array( |
| 284 | /*'enable_course_sell_by_woocommerce' => array( |
| 285 | 'type' => 'checkbox', |
| 286 | 'label' => __('Enable / Disable', 'tutor'), |
| 287 | 'label_title' => __('Enable WooComerce to sell course', 'tutor'), |
| 288 | 'desc' => __('By integrating WooCommerce, you can sell your course', 'tutor'), |
| 289 | ),*/ |
| 290 | 'enable_guest_course_cart' => array( |
| 291 | 'type' => 'checkbox', |
| 292 | 'label' => __('Enable / Disable', 'tutor'), |
| 293 | 'label_title' => __('Enable add to cart feature for guest users', 'tutor'), |
| 294 | 'desc' => __('Enabling this will let an unregistered user purchase any course from the Course Details page. Head over to Documentation to know how to configure this setting.', 'tutor'), |
| 295 | ), |
| 296 | ), |
| 297 | ), |
| 298 | ), |
| 299 | ); |
| 300 | |
| 301 | return $attr; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * @param $arr |
| 306 | * |
| 307 | * @return mixed |
| 308 | * |
| 309 | * Returning monetization options |
| 310 | * |
| 311 | * @since v.1.3.5 |
| 312 | */ |
| 313 | public function tutor_monetization_options($arr) { |
| 314 | $has_wc = tutils()->has_wc(); |
| 315 | if ($has_wc) { |
| 316 | $arr['wc'] = __('WooCommerce', 'tutor'); |
| 317 | } |
| 318 | return $arr; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * @param $item_id |
| 323 | * @param $item |
| 324 | * @param $order_id |
| 325 | * |
| 326 | * Adding Earning Data processing WooCommerce |
| 327 | * |
| 328 | * @since v.1.1.2 |
| 329 | */ |
| 330 | public function add_earning_data($item_id, $item, $order_id) { |
| 331 | global $wpdb; |
| 332 | $item = new \WC_Order_Item_Product($item); |
| 333 | |
| 334 | $product_id = $item->get_product_id(); |
| 335 | $if_has_course = tutor_utils()->product_belongs_with_course($product_id); |
| 336 | |
| 337 | if ($if_has_course) { |
| 338 | |
| 339 | $enable_tutor_earning = tutor_utils()->get_option('enable_tutor_earning'); |
| 340 | if (!$enable_tutor_earning) { |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | $course_id = $if_has_course->post_id; |
| 345 | $user_id = $wpdb->get_var($wpdb->prepare("SELECT post_author FROM {$wpdb->posts} WHERE ID = %d ", $course_id)); |
| 346 | $order_status = $wpdb->get_var($wpdb->prepare("SELECT post_status from {$wpdb->posts} where ID = %d ", $order_id)); |
| 347 | |
| 348 | /** |
| 349 | * Return here if already added this product from this order |
| 350 | * @since v1.9.7 |
| 351 | */ |
| 352 | $exist_count = (int)$wpdb->get_var($wpdb->prepare( |
| 353 | "SELECT COUNT(earning_id) |
| 354 | FROM {$wpdb->prefix}tutor_earnings |
| 355 | WHERE course_id=%d |
| 356 | AND order_id=%d |
| 357 | AND user_id=%d", |
| 358 | $course_id, |
| 359 | $order_id, |
| 360 | $user_id |
| 361 | )); |
| 362 | |
| 363 | if($exist_count>0) { |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | $total_price = $item->get_total(); |
| 368 | |
| 369 | $fees_deduct_data = array(); |
| 370 | $tutor_earning_fees = tutor_utils()->get_option('tutor_earning_fees'); |
| 371 | $enable_fees_deducting = tutor_utils()->avalue_dot('enable_fees_deducting', $tutor_earning_fees); |
| 372 | |
| 373 | $course_price_grand_total = $total_price; |
| 374 | |
| 375 | if ($enable_fees_deducting) { |
| 376 | $fees_name = tutor_utils()->avalue_dot('fees_name', $tutor_earning_fees); |
| 377 | $fees_amount = (int) tutor_utils()->avalue_dot('fees_amount', $tutor_earning_fees); |
| 378 | $fees_type = tutor_utils()->avalue_dot('fees_type', $tutor_earning_fees); |
| 379 | |
| 380 | if ($fees_amount > 0) { |
| 381 | if ($fees_type === 'percent') { |
| 382 | $fees_amount = ($total_price * $fees_amount) / 100; |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | if ( $fees_type === 'fixed' ) { |
| 387 | $course_price_grand_total = $total_price - $fees_amount; |
| 388 | }*/ |
| 389 | |
| 390 | $course_price_grand_total = $total_price - $fees_amount; |
| 391 | } |
| 392 | |
| 393 | $fees_deduct_data = array( |
| 394 | 'deduct_fees_amount' => $fees_amount, |
| 395 | 'deduct_fees_name' => $fees_name, |
| 396 | 'deduct_fees_type' => $fees_type, |
| 397 | ); |
| 398 | } |
| 399 | |
| 400 | $instructor_rate = tutor_utils()->get_option('earning_instructor_commission'); |
| 401 | $admin_rate = tutor_utils()->get_option('earning_admin_commission'); |
| 402 | |
| 403 | $instructor_amount = 0; |
| 404 | if ($instructor_rate > 0) { |
| 405 | $instructor_amount = ($course_price_grand_total * $instructor_rate) / 100; |
| 406 | } |
| 407 | |
| 408 | $admin_amount = 0; |
| 409 | if ($admin_rate > 0) { |
| 410 | $admin_amount = ($course_price_grand_total * $admin_rate) / 100; |
| 411 | } |
| 412 | |
| 413 | $commission_type = 'percent'; |
| 414 | |
| 415 | // (Use Pro Filter - Start) |
| 416 | // The response must be same array structure. |
| 417 | // Do not change used variable names here, or change in both of here and pro plugin |
| 418 | $pro_arg = [ |
| 419 | 'user_id' => $user_id, |
| 420 | 'instructor_rate' => $instructor_rate, |
| 421 | 'admin_rate' => $admin_rate, |
| 422 | 'instructor_amount' => $instructor_amount, |
| 423 | 'admin_amount' => $admin_amount, |
| 424 | 'course_price_grand_total' => $course_price_grand_total, |
| 425 | 'commission_type' => $commission_type |
| 426 | ]; |
| 427 | $pro_calculation = apply_filters('tutor_pro_earning_calculator', $pro_arg); |
| 428 | extract($pro_calculation); |
| 429 | // (Use Pro Filter - End) |
| 430 | |
| 431 | $earning_data = array( |
| 432 | 'user_id' => $user_id, |
| 433 | 'course_id' => $course_id, |
| 434 | 'order_id' => $order_id, |
| 435 | 'order_status' => $order_status, |
| 436 | 'course_price_total' => $total_price, |
| 437 | 'course_price_grand_total' => $course_price_grand_total, |
| 438 | |
| 439 | 'instructor_amount' => $instructor_amount, |
| 440 | 'instructor_rate' => $instructor_rate, |
| 441 | 'admin_amount' => $admin_amount, |
| 442 | 'admin_rate' => $admin_rate, |
| 443 | |
| 444 | 'commission_type' => $commission_type, |
| 445 | 'process_by' => 'woocommerce', |
| 446 | 'created_at' => date('Y-m-d H:i:s', tutor_time()), |
| 447 | ); |
| 448 | $earning_data = apply_filters('tutor_new_earning_data', array_merge($earning_data, $fees_deduct_data)); |
| 449 | |
| 450 | $wpdb->insert($wpdb->prefix . 'tutor_earnings', $earning_data); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * @param $order_id |
| 456 | * @param $status_from |
| 457 | * @param $status_to |
| 458 | * |
| 459 | * Change Earning data status |
| 460 | * |
| 461 | * @since v.1.1.2 |
| 462 | */ |
| 463 | public function add_earning_data_status_change($order_id, $status_from, $status_to) { |
| 464 | if (!tutor_utils()->is_tutor_order($order_id)) { |
| 465 | return; |
| 466 | } |
| 467 | global $wpdb; |
| 468 | |
| 469 | $is_earning_data = (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(earning_id) FROM {$wpdb->prefix}tutor_earnings WHERE order_id = %d ", $order_id)); |
| 470 | if ($is_earning_data) { |
| 471 | $wpdb->update($wpdb->prefix . 'tutor_earnings', array('order_status' => $status_to), array('order_id' => $order_id)); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Course placing order from admin |
| 477 | * |
| 478 | * @param $order_id |
| 479 | * @since v.1.6.7 |
| 480 | */ |
| 481 | public function course_placing_order_from_admin($order_id) { |
| 482 | if (!is_admin()) { |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | $order = wc_get_order( $order_id ); |
| 487 | foreach ($order->get_items() as $item) { |
| 488 | $product_id = $item->get_product_id(); |
| 489 | $if_has_course = tutor_utils()->product_belongs_with_course($product_id); |
| 490 | if ($if_has_course) { |
| 491 | $course_id = $if_has_course->post_id; |
| 492 | $customer_id = $order->get_customer_id(); |
| 493 | tutor_utils()->do_enroll($course_id, $order_id, $customer_id); |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * Course placing order from customer |
| 500 | * |
| 501 | * @param $order_id |
| 502 | * @since v.1.6.7 |
| 503 | */ |
| 504 | public function course_placing_order_from_customer($item_id, $item, $order_id) { |
| 505 | if (is_admin()) { |
| 506 | return; |
| 507 | } |
| 508 | |
| 509 | $item = new \WC_Order_Item_Product($item); |
| 510 | $product_id = $item->get_product_id(); |
| 511 | $if_has_course = tutor_utils()->product_belongs_with_course($product_id); |
| 512 | |
| 513 | if ($if_has_course){ |
| 514 | $course_id = $if_has_course->post_id; |
| 515 | tutor_utils()->do_enroll($course_id, $order_id); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Disable course monetization on woocommerce deactivation |
| 521 | * @since v.1.7.8 |
| 522 | */ |
| 523 | public function disable_tutor_monetization() { |
| 524 | tutils()->update_option('monetize_by', 'free'); |
| 525 | update_option('tutor_show_woocommerce_notice', true); |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * Redirect student on enrolled courses after course |
| 530 | * enrollment complete if course is purchasable |
| 531 | * @param $order_id | int |
| 532 | * @since 1.9.0 |
| 533 | */ |
| 534 | public function redirect_to_enrolled_courses( $order_id ) { |
| 535 | if(!tutils()->get_option( 'wc_automatic_order_complete_redirect_to_courses' )) { |
| 536 | // Since 1.9.1 |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | //get woo order details |
| 541 | $order = wc_get_order( $order_id ); |
| 542 | $tutor_product = false; |
| 543 | $url = tutor_utils()->tutor_dashboard_url().'enrolled-courses/'; |
| 544 | foreach ($order->get_items() as $item) { |
| 545 | $product_id = $item->get_product_id(); |
| 546 | //check if product associated with tutor course |
| 547 | $if_has_course = tutor_utils()->product_belongs_with_course($product_id); |
| 548 | if( $if_has_course ) { |
| 549 | $tutor_product = true; |
| 550 | } |
| 551 | } |
| 552 | //if tutor product & order status completed |
| 553 | if ( $order->has_status( 'completed' ) && $tutor_product ) { |
| 554 | wp_safe_redirect( $url ); |
| 555 | exit; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Change product url on cart page if product is tutor course |
| 561 | * |
| 562 | * @since 1.9.8 |
| 563 | */ |
| 564 | function tutor_update_product_url( $permalink, $cart_item ) { |
| 565 | |
| 566 | $woo_product_id = $cart_item['product_id']; |
| 567 | $product_meta = get_post_meta( $woo_product_id ); |
| 568 | |
| 569 | if( isset( $product_meta['_tutor_product'] ) && $product_meta['_tutor_product'][0] ) { |
| 570 | |
| 571 | global $wpdb; |
| 572 | $table = $wpdb->base_prefix . 'postmeta'; |
| 573 | $post_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$table} WHERE meta_key = '_tutor_course_product_id' AND meta_value = %d ", $woo_product_id)); |
| 574 | |
| 575 | if( $post_id ) { |
| 576 | $data = get_post_permalink( $post_id ); |
| 577 | return $data; |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | |
| 584 | add_action('admin_notices', function() { |
| 585 | |
| 586 | $show = get_option( 'tutor_show_woocommerce_notice' ) && tutils()->get_option('monetize_by', 'free')=='free'; |
| 587 | |
| 588 | if($show) { |
| 589 | $message = __('Since WooCommerce is disabled, your monetized courses have been set to free. Please make sure to enable Tutor LMS monetization if you decide to re-enable WooCommerce.', 'tutor'); |
| 590 | echo '<div class="notice notice-error"><p>' . $message . '</p></div>'; |
| 591 | } |
| 592 | }); |