Addons.php
5 years ago
Admin.php
5 years ago
Ajax.php
5 years ago
Assets.php
5 years ago
Course.php
5 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
5 years ago
Instructor.php
5 years ago
Instructors_List.php
5 years ago
Lesson.php
5 years ago
Options.php
5 years ago
Post_types.php
5 years ago
Q_and_A.php
5 years ago
Question_Answers_List.php
5 years ago
Quiz.php
5 years ago
Quiz_Attempts_List.php
5 years ago
RestAPI.php
5 years ago
Rewrite_Rules.php
5 years ago
Shortcode.php
5 years ago
Student.php
5 years ago
Students_List.php
5 years ago
Taxonomies.php
5 years ago
Template.php
5 years ago
Theme_Compatibility.php
5 years ago
Tools.php
5 years ago
Tutor.php
5 years ago
TutorEDD.php
5 years ago
Tutor_Base.php
5 years ago
Tutor_List_Table.php
5 years ago
Tutor_Setup.php
5 years ago
Upgrader.php
5 years ago
User.php
5 years ago
Utils.php
5 years ago
Video_Stream.php
5 years ago
Withdraw.php
5 years ago
Withdraw_Requests_List.php
5 years ago
WooCommerce.php
5 years ago
WooCommerce.php
485 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 | public function notice_before_option() { |
| 76 | $has_wc = tutor_utils()->has_wc(); |
| 77 | if ($has_wc) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | ob_start(); |
| 82 | ?> |
| 83 | <div class="tutor-notice-warning"> |
| 84 | <p> |
| 85 | <?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 |
| 86 | WooCommerce plugin installed. Get back on this page after installing the plugin and enable the following feature to start selling |
| 87 | courses with Tutor.', 'tutor'); ?> |
| 88 | </p> |
| 89 | <p><?php _e('This notice will disappear after activating <strong>WooCommerce</strong>', 'tutor'); ?></p> |
| 90 | </div> |
| 91 | <?php |
| 92 | echo ob_get_clean(); |
| 93 | } |
| 94 | |
| 95 | public function is_course_purchasable($bool, $course_id) { |
| 96 | if (!tutor_utils()->has_wc()) { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | $course_id = tutor_utils()->get_post_id($course_id); |
| 101 | $has_product_id = get_post_meta($course_id, '_tutor_course_product_id', true); |
| 102 | if ($has_product_id) { |
| 103 | return true; |
| 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | public function get_tutor_course_price($price, $course_id) { |
| 109 | $price = null; |
| 110 | |
| 111 | if (tutor_utils()->is_course_purchasable($course_id)) { |
| 112 | if (tutor_utils()->has_wc()) { |
| 113 | $product_id = tutor_utils()->get_course_product_id($course_id); |
| 114 | $product = wc_get_product($product_id); |
| 115 | |
| 116 | if ($product) { |
| 117 | ob_start(); |
| 118 | ?> |
| 119 | <div class="price"> |
| 120 | <?php echo $product->get_price_html(); ?> |
| 121 | </div> |
| 122 | <?php |
| 123 | return ob_get_clean(); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return $price; |
| 129 | } |
| 130 | |
| 131 | public function tutor_course_sell_by() { |
| 132 | return 'woocommerce'; |
| 133 | } |
| 134 | |
| 135 | public function add_tutor_type_in_wc_product($types) { |
| 136 | $types['tutor_product'] = array( |
| 137 | 'id' => '_tutor_product', |
| 138 | 'wrapper_class' => 'show_if_simple', |
| 139 | 'label' => __('For Tutor', 'tutor'), |
| 140 | 'description' => __('This checkmark ensure that you will sell a specif course via this product.', 'tutor'), |
| 141 | 'default' => 'no', |
| 142 | ); |
| 143 | |
| 144 | return $types; |
| 145 | } |
| 146 | |
| 147 | public function register_meta_box() { |
| 148 | add_meta_box('tutor-attach-product', __('Add Product', 'tutor'), array($this, 'course_add_product_metabox'), $this->course_post_type, 'advanced', 'high'); |
| 149 | } |
| 150 | |
| 151 | public function course_add_product_metabox() { |
| 152 | include tutor()->path . 'views/metabox/course-add-product-metabox.php'; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @param $post_ID |
| 157 | * |
| 158 | * Save course meta for attaching product |
| 159 | */ |
| 160 | public function save_course_meta($post_ID) { |
| 161 | $product_id = tutor_utils()->avalue_dot('_tutor_course_product_id', $_POST); |
| 162 | |
| 163 | if ($product_id === '-1') { |
| 164 | delete_post_meta($post_ID, '_tutor_course_product_id'); |
| 165 | } else { |
| 166 | $product_id = (int) $product_id; |
| 167 | if ($product_id) { |
| 168 | update_post_meta($post_ID, '_tutor_course_product_id', $product_id); |
| 169 | //Mark product for woocommerce |
| 170 | update_post_meta($product_id, '_virtual', 'yes'); |
| 171 | update_post_meta($product_id, '_tutor_product', 'yes'); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | public function save_wc_product_meta($post_ID) { |
| 177 | $is_tutor_product = tutor_utils()->avalue_dot('_tutor_product', $_POST); |
| 178 | if ($is_tutor_product === 'on') { |
| 179 | update_post_meta($post_ID, '_tutor_product', 'yes'); |
| 180 | } else { |
| 181 | delete_post_meta($post_ID, '_tutor_product'); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * |
| 187 | * Take enrolled course action based on order status change |
| 188 | */ |
| 189 | public function enrolled_courses_status_change($order_id, $status_from, $status_to) { |
| 190 | if (!tutor_utils()->is_tutor_order($order_id)) { |
| 191 | return; |
| 192 | } |
| 193 | global $wpdb; |
| 194 | |
| 195 | $enrolled_ids_with_course = $this->get_course_enrolled_ids_by_order_id($order_id); |
| 196 | |
| 197 | if ($enrolled_ids_with_course) { |
| 198 | $enrolled_ids = wp_list_pluck($enrolled_ids_with_course, 'enrolled_id'); |
| 199 | |
| 200 | if (is_array($enrolled_ids) && count($enrolled_ids)) { |
| 201 | foreach ($enrolled_ids as $enrolled_id) { |
| 202 | |
| 203 | tutils()->course_enrol_status_change($enrolled_id, $status_to); |
| 204 | |
| 205 | // Invoke enrolled hook |
| 206 | if($status_to == 'completed'){ |
| 207 | $user_id = get_post_field('post_author', $enrolled_id); |
| 208 | $course_id = get_post_field('post_parent', $enrolled_id); |
| 209 | do_action('tutor_after_enrolled', $course_id, $user_id); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * @param $order_id |
| 218 | * |
| 219 | * @return array|bool |
| 220 | */ |
| 221 | public function get_course_enrolled_ids_by_order_id($order_id) { |
| 222 | global $wpdb; |
| 223 | //Getting all of courses ids within this order |
| 224 | |
| 225 | $courses_ids = $wpdb->get_results("SELECT * FROM {$wpdb->postmeta} WHERE post_id = {$order_id} AND meta_key LIKE '_tutor_order_for_course_id_%' "); |
| 226 | |
| 227 | if (is_array($courses_ids) && count($courses_ids)) { |
| 228 | $course_enrolled_by_order = array(); |
| 229 | foreach ($courses_ids as $courses_id) { |
| 230 | $course_id = str_replace('_tutor_order_for_course_id_', '', $courses_id->meta_key); |
| 231 | //array(order_id => array('course_id' => $course_id, 'enrolled_id' => enrolled_id, 'order_id' => $courses_id->post_id)) |
| 232 | $course_enrolled_by_order[] = array('course_id' => $course_id, 'enrolled_id' => $courses_id->meta_value, 'order_id' => $courses_id->post_id); |
| 233 | } |
| 234 | return $course_enrolled_by_order; |
| 235 | } |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Remove course |
| 241 | * |
| 242 | * TODO: right now it's unused |
| 243 | */ |
| 244 | public function remove_active_course($order_id) { |
| 245 | global $wpdb; |
| 246 | //Getting all of courses ids within this order |
| 247 | |
| 248 | $courses_ids = $wpdb->get_results("SELECT * FROM {$wpdb->postmeta} WHERE post_id = {$order_id} meta_key LIKE '_tutor_order_for_course_id_%' "); |
| 249 | } |
| 250 | |
| 251 | |
| 252 | /** |
| 253 | * @param $attr |
| 254 | * |
| 255 | * @return mixed |
| 256 | * |
| 257 | * Add option for WooCommerce settings |
| 258 | */ |
| 259 | public function add_options($attr) { |
| 260 | |
| 261 | $attr['woocommerce'] = array( |
| 262 | 'label' => __('WooCommerce', 'tutor'), |
| 263 | |
| 264 | 'sections' => array( |
| 265 | 'general' => array( |
| 266 | 'label' => __('General', 'tutor'), |
| 267 | 'desc' => __('WooCommerce Settings', 'tutor'), |
| 268 | 'fields' => array( |
| 269 | /*'enable_course_sell_by_woocommerce' => array( |
| 270 | 'type' => 'checkbox', |
| 271 | 'label' => __('Enable / Disable', 'tutor'), |
| 272 | 'label_title' => __('Enable WooComerce to sell course', 'tutor'), |
| 273 | 'desc' => __('By integrating WooCommerce, you can sell your course', 'tutor'), |
| 274 | ),*/ |
| 275 | 'enable_guest_course_cart' => array( |
| 276 | 'type' => 'checkbox', |
| 277 | 'label' => __('Enable / Disable', 'tutor'), |
| 278 | 'label_title' => __('Enable add to cart feature for guest users', 'tutor'), |
| 279 | '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'), |
| 280 | ), |
| 281 | ), |
| 282 | ), |
| 283 | ), |
| 284 | ); |
| 285 | |
| 286 | return $attr; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * @param $arr |
| 291 | * |
| 292 | * @return mixed |
| 293 | * |
| 294 | * Returning monetization options |
| 295 | * |
| 296 | * @since v.1.3.5 |
| 297 | */ |
| 298 | public function tutor_monetization_options($arr) { |
| 299 | $has_wc = tutils()->has_wc(); |
| 300 | if ($has_wc) { |
| 301 | $arr['wc'] = __('WooCommerce', 'tutor'); |
| 302 | } |
| 303 | return $arr; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * @param $item_id |
| 308 | * @param $item |
| 309 | * @param $order_id |
| 310 | * |
| 311 | * Adding Earning Data processing WooCommerce |
| 312 | * |
| 313 | * @since v.1.1.2 |
| 314 | */ |
| 315 | public function add_earning_data($item_id, $item, $order_id) { |
| 316 | global $wpdb; |
| 317 | $item = new \WC_Order_Item_Product($item); |
| 318 | |
| 319 | $product_id = $item->get_product_id(); |
| 320 | $if_has_course = tutor_utils()->product_belongs_with_course($product_id); |
| 321 | |
| 322 | if ($if_has_course) { |
| 323 | |
| 324 | $enable_tutor_earning = tutor_utils()->get_option('enable_tutor_earning'); |
| 325 | if (!$enable_tutor_earning) { |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | $course_id = $if_has_course->post_id; |
| 330 | $user_id = $wpdb->get_var("SELECT post_author FROM {$wpdb->posts} WHERE ID = {$course_id} "); |
| 331 | $order_status = $wpdb->get_var("SELECT post_status from {$wpdb->posts} where ID = {$order_id} "); |
| 332 | |
| 333 | $total_price = $item->get_total(); |
| 334 | |
| 335 | $fees_deduct_data = array(); |
| 336 | $tutor_earning_fees = tutor_utils()->get_option('tutor_earning_fees'); |
| 337 | $enable_fees_deducting = tutor_utils()->avalue_dot('enable_fees_deducting', $tutor_earning_fees); |
| 338 | |
| 339 | $course_price_grand_total = $total_price; |
| 340 | |
| 341 | if ($enable_fees_deducting) { |
| 342 | $fees_name = tutor_utils()->avalue_dot('fees_name', $tutor_earning_fees); |
| 343 | $fees_amount = (int) tutor_utils()->avalue_dot('fees_amount', $tutor_earning_fees); |
| 344 | $fees_type = tutor_utils()->avalue_dot('fees_type', $tutor_earning_fees); |
| 345 | |
| 346 | if ($fees_amount > 0) { |
| 347 | if ($fees_type === 'percent') { |
| 348 | $fees_amount = ($total_price * $fees_amount) / 100; |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | if ( $fees_type === 'fixed' ) { |
| 353 | $course_price_grand_total = $total_price - $fees_amount; |
| 354 | }*/ |
| 355 | |
| 356 | $course_price_grand_total = $total_price - $fees_amount; |
| 357 | } |
| 358 | |
| 359 | $fees_deduct_data = array( |
| 360 | 'deduct_fees_amount' => $fees_amount, |
| 361 | 'deduct_fees_name' => $fees_name, |
| 362 | 'deduct_fees_type' => $fees_type, |
| 363 | ); |
| 364 | } |
| 365 | |
| 366 | $instructor_rate = tutor_utils()->get_option('earning_instructor_commission'); |
| 367 | $admin_rate = tutor_utils()->get_option('earning_admin_commission'); |
| 368 | |
| 369 | $instructor_amount = 0; |
| 370 | if ($instructor_rate > 0) { |
| 371 | $instructor_amount = ($course_price_grand_total * $instructor_rate) / 100; |
| 372 | } |
| 373 | |
| 374 | $admin_amount = 0; |
| 375 | if ($admin_rate > 0) { |
| 376 | $admin_amount = ($course_price_grand_total * $admin_rate) / 100; |
| 377 | } |
| 378 | |
| 379 | $commission_type = 'percent'; |
| 380 | |
| 381 | // (Use Pro Filter - Start) |
| 382 | // The response must be same array structure. |
| 383 | // Do not change used variable names here, or change in both of here and pro plugin |
| 384 | $pro_arg = [ |
| 385 | 'user_id' => $user_id, |
| 386 | 'instructor_rate' => $instructor_rate, |
| 387 | 'admin_rate' => $admin_rate, |
| 388 | 'instructor_amount' => $instructor_amount, |
| 389 | 'admin_amount' => $admin_amount, |
| 390 | 'course_price_grand_total' => $course_price_grand_total, |
| 391 | 'commission_type' => $commission_type |
| 392 | ]; |
| 393 | $pro_calculation = apply_filters('tutor_pro_earning_calculator', $pro_arg); |
| 394 | extract($pro_calculation); |
| 395 | // (Use Pro Filter - End) |
| 396 | |
| 397 | $earning_data = array( |
| 398 | 'user_id' => $user_id, |
| 399 | 'course_id' => $course_id, |
| 400 | 'order_id' => $order_id, |
| 401 | 'order_status' => $order_status, |
| 402 | 'course_price_total' => $total_price, |
| 403 | 'course_price_grand_total' => $course_price_grand_total, |
| 404 | |
| 405 | 'instructor_amount' => $instructor_amount, |
| 406 | 'instructor_rate' => $instructor_rate, |
| 407 | 'admin_amount' => $admin_amount, |
| 408 | 'admin_rate' => $admin_rate, |
| 409 | |
| 410 | 'commission_type' => $commission_type, |
| 411 | 'process_by' => 'woocommerce', |
| 412 | 'created_at' => date('Y-m-d H:i:s', tutor_time()), |
| 413 | ); |
| 414 | $earning_data = apply_filters('tutor_new_earning_data', array_merge($earning_data, $fees_deduct_data)); |
| 415 | |
| 416 | $wpdb->insert($wpdb->prefix . 'tutor_earnings', $earning_data); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * @param $order_id |
| 422 | * @param $status_from |
| 423 | * @param $status_to |
| 424 | * |
| 425 | * Change Earning data status |
| 426 | * |
| 427 | * @since v.1.1.2 |
| 428 | */ |
| 429 | public function add_earning_data_status_change($order_id, $status_from, $status_to) { |
| 430 | if (!tutor_utils()->is_tutor_order($order_id)) { |
| 431 | return; |
| 432 | } |
| 433 | global $wpdb; |
| 434 | |
| 435 | $is_earning_data = (int) $wpdb->get_var("SELECT COUNT(earning_id) FROM {$wpdb->prefix}tutor_earnings WHERE order_id = {$order_id} "); |
| 436 | if ($is_earning_data) { |
| 437 | $wpdb->update($wpdb->prefix . 'tutor_earnings', array('order_status' => $status_to), array('order_id' => $order_id)); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Course placing order from admin |
| 443 | * |
| 444 | * @param $order_id |
| 445 | * @since v.1.6.7 |
| 446 | */ |
| 447 | public function course_placing_order_from_admin($order_id) { |
| 448 | if (!is_admin()) { |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | $order = wc_get_order( $order_id ); |
| 453 | foreach ($order->get_items() as $item) { |
| 454 | $product_id = $item->get_product_id(); |
| 455 | $if_has_course = tutor_utils()->product_belongs_with_course($product_id); |
| 456 | if ($if_has_course) { |
| 457 | $course_id = $if_has_course->post_id; |
| 458 | $customer_id = $order->get_customer_id(); |
| 459 | tutor_utils()->do_enroll($course_id, $order_id, $customer_id); |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Course placing order from customer |
| 466 | * |
| 467 | * @param $order_id |
| 468 | * @since v.1.6.7 |
| 469 | */ |
| 470 | public function course_placing_order_from_customer($item_id, $item, $order_id) { |
| 471 | if (is_admin()) { |
| 472 | return; |
| 473 | } |
| 474 | |
| 475 | $item = new \WC_Order_Item_Product($item); |
| 476 | $product_id = $item->get_product_id(); |
| 477 | $if_has_course = tutor_utils()->product_belongs_with_course($product_id); |
| 478 | |
| 479 | if ($if_has_course){ |
| 480 | $course_id = $if_has_course->post_id; |
| 481 | tutor_utils()->do_enroll($course_id, $order_id); |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 |