Addons.php
6 years ago
Admin.php
6 years ago
Ajax.php
6 years ago
Assets.php
6 years ago
Course.php
5 years ago
Course_Settings_Tabs.php
6 years ago
Course_Widget.php
6 years ago
Dashboard.php
6 years ago
Email.php
5 years ago
FormHandler.php
6 years ago
Frontend.php
6 years ago
Gutenberg.php
6 years ago
Instructor.php
6 years ago
Instructors_List.php
6 years ago
Lesson.php
6 years ago
Options.php
5 years ago
Post_types.php
5 years ago
Q_and_A.php
6 years ago
Question_Answers_List.php
7 years ago
Quiz.php
6 years ago
Quiz_Attempts_List.php
6 years ago
RestAPI.php
6 years ago
Rewrite_Rules.php
6 years ago
Shortcode.php
6 years ago
Student.php
6 years ago
Students_List.php
7 years ago
Taxonomies.php
7 years ago
Template.php
6 years ago
Theme_Compatibility.php
7 years ago
Tools.php
6 years ago
Tutor.php
6 years ago
TutorEDD.php
6 years ago
Tutor_Base.php
5 years ago
Tutor_List_Table.php
5 years ago
Tutor_Setup.php
6 years ago
Upgrader.php
6 years ago
User.php
6 years ago
Utils.php
5 years ago
Video_Stream.php
5 years ago
Withdraw.php
6 years ago
Withdraw_Requests_List.php
6 years ago
WooCommerce.php
5 years ago
WooCommerce.php
427 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Created by PhpStorm. |
| 4 | * User: themeum |
| 5 | * Date: 1/10/18 |
| 6 | * Time: 3:01 PM |
| 7 | */ |
| 8 | |
| 9 | namespace TUTOR; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) |
| 12 | exit; |
| 13 | |
| 14 | class WooCommerce extends Tutor_Base { |
| 15 | |
| 16 | public function __construct() { |
| 17 | parent::__construct(); |
| 18 | |
| 19 | add_action('tutor_options_before_woocommerce', array($this, 'notice_before_option')); |
| 20 | |
| 21 | //Add option settings |
| 22 | add_filter('tutor_monetization_options', array($this, 'tutor_monetization_options')); |
| 23 | add_filter('tutor/options/attr', array($this, 'add_options')); |
| 24 | |
| 25 | $monetize_by = tutils()->get_option('monetize_by'); |
| 26 | if ( $monetize_by !== 'wc'){ |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Is Course Purchasable |
| 32 | */ |
| 33 | add_filter('is_course_purchasable', array($this, 'is_course_purchasable'), 10, 2); |
| 34 | add_filter('get_tutor_course_price', array($this, 'get_tutor_course_price'), 10, 2); |
| 35 | add_filter('tutor_course_sell_by', array($this, 'tutor_course_sell_by')); |
| 36 | |
| 37 | add_filter('product_type_options', array($this, 'add_tutor_type_in_wc_product')); |
| 38 | |
| 39 | add_action( 'add_meta_boxes', array($this, 'register_meta_box') ); |
| 40 | add_action('save_post_'.$this->course_post_type, array($this, 'save_course_meta')); |
| 41 | add_action('save_post_product', array($this, 'save_wc_product_meta')); |
| 42 | |
| 43 | add_action('tutor_course/single/before/enroll', 'wc_print_notices'); |
| 44 | add_action('woocommerce_new_order_item', array($this, 'course_placing_order'), 10, 3); |
| 45 | |
| 46 | /** |
| 47 | * Order Status Hook |
| 48 | * |
| 49 | * Remove course from active courses if an order is cancelled or refunded |
| 50 | */ |
| 51 | add_action( 'woocommerce_order_status_changed', array( $this, 'enrolled_courses_status_change' ), 10, 3 ); |
| 52 | |
| 53 | /** |
| 54 | * Add Earning Data |
| 55 | */ |
| 56 | add_action('woocommerce_new_order_item', array($this, 'add_earning_data'), 10, 3); |
| 57 | add_action( 'woocommerce_order_status_changed', array( $this, 'add_earning_data_status_change' ), 10, 3 ); |
| 58 | |
| 59 | /** |
| 60 | * WC Print Notices After Enroll |
| 61 | * @since v.1.3.5 |
| 62 | */ |
| 63 | if ( tutils()->has_wc()){ |
| 64 | add_action( 'tutor_course/single/before/inner-wrap', 'wc_print_notices', 10 ); |
| 65 | add_action( 'tutor_course/single/enrolled/before/inner-wrap', 'wc_print_notices', 10 ); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | public function notice_before_option(){ |
| 70 | $has_wc = tutor_utils()->has_wc(); |
| 71 | if ($has_wc){ |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | ob_start(); |
| 76 | ?> |
| 77 | <div class="tutor-notice-warning"> |
| 78 | <p> |
| 79 | <?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 |
| 80 | WooCommerce plugin installed. Get back on this page after installing the plugin and enable the following feature to start selling |
| 81 | courses with Tutor.', 'tutor'); ?> |
| 82 | </p> |
| 83 | <p><?php _e('This notice will disappear after activating <strong>WooCommerce</strong>', 'tutor'); ?></p> |
| 84 | </div> |
| 85 | <?php |
| 86 | echo ob_get_clean(); |
| 87 | } |
| 88 | |
| 89 | public function is_course_purchasable($bool, $course_id){ |
| 90 | if ( ! tutor_utils()->has_wc()){ |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | $course_id = tutor_utils()->get_post_id($course_id); |
| 95 | $has_product_id = get_post_meta($course_id, '_tutor_course_product_id', true); |
| 96 | if ($has_product_id){ |
| 97 | return true; |
| 98 | } |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | public function get_tutor_course_price($price, $course_id){ |
| 103 | $price = null; |
| 104 | |
| 105 | if (tutor_utils()->is_course_purchasable($course_id)) { |
| 106 | if (tutor_utils()->has_wc()){ |
| 107 | $product_id = tutor_utils()->get_course_product_id($course_id); |
| 108 | $product = wc_get_product( $product_id ); |
| 109 | |
| 110 | if ($product) { |
| 111 | ob_start(); |
| 112 | ?> |
| 113 | <div class="price"> |
| 114 | <?php echo $product->get_price_html(); ?> |
| 115 | </div> |
| 116 | <?php |
| 117 | return ob_get_clean(); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return $price; |
| 123 | } |
| 124 | |
| 125 | public function tutor_course_sell_by(){ |
| 126 | return 'woocommerce'; |
| 127 | } |
| 128 | |
| 129 | public function add_tutor_type_in_wc_product($types){ |
| 130 | $types['tutor_product'] = array( |
| 131 | 'id' => '_tutor_product', |
| 132 | 'wrapper_class' => 'show_if_simple', |
| 133 | 'label' => __( 'For Tutor', 'tutor' ), |
| 134 | 'description' => __( 'This checkmark ensure that you will sell a specif course via this product.', 'tutor' ), |
| 135 | 'default' => 'no', |
| 136 | ); |
| 137 | |
| 138 | return $types; |
| 139 | } |
| 140 | |
| 141 | public function register_meta_box(){ |
| 142 | add_meta_box( 'tutor-attach-product', __( 'Add Product', 'tutor' ), array($this, 'course_add_product_metabox'), $this->course_post_type, 'advanced', 'high' ); |
| 143 | } |
| 144 | |
| 145 | public function course_add_product_metabox(){ |
| 146 | include tutor()->path.'views/metabox/course-add-product-metabox.php'; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @param $post_ID |
| 151 | * |
| 152 | * Save course meta for attaching product |
| 153 | */ |
| 154 | public function save_course_meta($post_ID){ |
| 155 | $product_id = tutor_utils()->avalue_dot('_tutor_course_product_id', $_POST); |
| 156 | |
| 157 | if ($product_id === '-1'){ |
| 158 | delete_post_meta($post_ID, '_tutor_course_product_id'); |
| 159 | }else{ |
| 160 | $product_id = (int) $product_id; |
| 161 | if ($product_id){ |
| 162 | update_post_meta($post_ID, '_tutor_course_product_id', $product_id); |
| 163 | //Mark product for woocommerce |
| 164 | update_post_meta($product_id, '_virtual', 'yes'); |
| 165 | update_post_meta($product_id, '_tutor_product', 'yes'); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | public function save_wc_product_meta($post_ID){ |
| 171 | $is_tutor_product = tutor_utils()->avalue_dot('_tutor_product', $_POST); |
| 172 | if ($is_tutor_product === 'on'){ |
| 173 | update_post_meta($post_ID, '_tutor_product', 'yes'); |
| 174 | }else{ |
| 175 | delete_post_meta($post_ID, '_tutor_product'); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Do something after course order place |
| 181 | */ |
| 182 | public function course_placing_order( $item_id, $item, $order_id){ |
| 183 | $item = new \WC_Order_Item_Product($item); |
| 184 | |
| 185 | $product_id = $item->get_product_id(); |
| 186 | $if_has_course = tutor_utils()->product_belongs_with_course($product_id); |
| 187 | |
| 188 | if ($if_has_course){ |
| 189 | $course_id = $if_has_course->post_id; |
| 190 | tutor_utils()->do_enroll($course_id, $order_id); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | |
| 195 | /** |
| 196 | * |
| 197 | * Take enrolled course action based on order status change |
| 198 | */ |
| 199 | |
| 200 | public function enrolled_courses_status_change($order_id, $status_from, $status_to){ |
| 201 | if ( ! tutor_utils()->is_tutor_order($order_id)){ |
| 202 | return; |
| 203 | } |
| 204 | global $wpdb; |
| 205 | |
| 206 | $enrolled_ids_with_course = $this->get_course_enrolled_ids_by_order_id($order_id); |
| 207 | |
| 208 | if ($enrolled_ids_with_course){ |
| 209 | $enrolled_ids = wp_list_pluck($enrolled_ids_with_course, 'enrolled_id'); |
| 210 | |
| 211 | if (is_array($enrolled_ids) && count($enrolled_ids)){ |
| 212 | foreach ($enrolled_ids as $enrolled_id){ |
| 213 | tutils()->course_enrol_status_change($enrolled_id, $status_to); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * @param $order_id |
| 221 | * |
| 222 | * @return array|bool |
| 223 | */ |
| 224 | public function get_course_enrolled_ids_by_order_id($order_id){ |
| 225 | global $wpdb; |
| 226 | //Getting all of courses ids within this order |
| 227 | |
| 228 | $courses_ids = $wpdb->get_results("SELECT * FROM {$wpdb->postmeta} WHERE post_id = {$order_id} AND meta_key LIKE '_tutor_order_for_course_id_%' "); |
| 229 | |
| 230 | if (is_array($courses_ids) && count($courses_ids)){ |
| 231 | $course_enrolled_by_order = array(); |
| 232 | foreach ($courses_ids as $courses_id){ |
| 233 | $course_id = str_replace('_tutor_order_for_course_id_', '',$courses_id->meta_key); |
| 234 | //array(order_id => array('course_id' => $course_id, 'enrolled_id' => enrolled_id, 'order_id' => $courses_id->post_id)) |
| 235 | $course_enrolled_by_order[] = array('course_id' => $course_id, 'enrolled_id' => $courses_id->meta_value, 'order_id' => $courses_id->post_id ); |
| 236 | } |
| 237 | return $course_enrolled_by_order; |
| 238 | } |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Remove course |
| 244 | * |
| 245 | * TODO: right now it's unused |
| 246 | */ |
| 247 | public function remove_active_course($order_id){ |
| 248 | global $wpdb; |
| 249 | //Getting all of courses ids within this order |
| 250 | |
| 251 | $courses_ids = $wpdb->get_results("SELECT * FROM {$wpdb->postmeta} WHERE post_id = {$order_id} meta_key LIKE '_tutor_order_for_course_id_%' "); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | /** |
| 256 | * @param $attr |
| 257 | * |
| 258 | * @return mixed |
| 259 | * |
| 260 | * Add option for WooCommerce settings |
| 261 | */ |
| 262 | public function add_options($attr){ |
| 263 | |
| 264 | $attr['woocommerce'] = array( |
| 265 | 'label' => __( 'WooCommerce', 'tutor' ), |
| 266 | |
| 267 | 'sections' => array( |
| 268 | 'general' => array( |
| 269 | 'label' => __('General', 'tutor'), |
| 270 | 'desc' => __('WooCommerce Settings', 'tutor'), |
| 271 | 'fields' => array( |
| 272 | /*'enable_course_sell_by_woocommerce' => array( |
| 273 | 'type' => 'checkbox', |
| 274 | 'label' => __('Enable / Disable', 'tutor'), |
| 275 | 'label_title' => __('Enable WooComerce to sell course', 'tutor'), |
| 276 | 'desc' => __('By integrating WooCommerce, you can sell your course', 'tutor'), |
| 277 | ),*/ |
| 278 | 'enable_guest_course_cart' => array( |
| 279 | 'type' => 'checkbox', |
| 280 | 'label' => __('Enable / Disable', 'tutor'), |
| 281 | 'label_title' => __('Enable add to cart feature for guest users', 'tutor'), |
| 282 | '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'), |
| 283 | ), |
| 284 | ), |
| 285 | ), |
| 286 | ), |
| 287 | ); |
| 288 | |
| 289 | return $attr; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * @param $arr |
| 294 | * |
| 295 | * @return mixed |
| 296 | * |
| 297 | * Returning monetization options |
| 298 | * |
| 299 | * @since v.1.3.5 |
| 300 | */ |
| 301 | public function tutor_monetization_options($arr){ |
| 302 | $has_wc = tutils()->has_wc(); |
| 303 | if ($has_wc){ |
| 304 | $arr['wc'] = __('WooCommerce', 'tutor'); |
| 305 | } |
| 306 | return $arr; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * @param $item_id |
| 311 | * @param $item |
| 312 | * @param $order_id |
| 313 | * |
| 314 | * Adding Earning Data processing WooCommerce |
| 315 | * |
| 316 | * @since v.1.1.2 |
| 317 | */ |
| 318 | public function add_earning_data( $item_id, $item, $order_id){ |
| 319 | global $wpdb; |
| 320 | $item = new \WC_Order_Item_Product($item); |
| 321 | |
| 322 | $product_id = $item->get_product_id(); |
| 323 | $if_has_course = tutor_utils()->product_belongs_with_course($product_id); |
| 324 | |
| 325 | if ($if_has_course){ |
| 326 | |
| 327 | $enable_tutor_earning = tutor_utils()->get_option('enable_tutor_earning'); |
| 328 | if ( ! $enable_tutor_earning){ |
| 329 | return; |
| 330 | } |
| 331 | |
| 332 | $course_id = $if_has_course->post_id; |
| 333 | $user_id = $wpdb->get_var("SELECT post_author FROM {$wpdb->posts} WHERE ID = {$course_id} "); |
| 334 | $order_status = $wpdb->get_var("SELECT post_status from {$wpdb->posts} where ID = {$order_id} "); |
| 335 | |
| 336 | $total_price = $item->get_total(); |
| 337 | |
| 338 | $fees_deduct_data = array(); |
| 339 | $tutor_earning_fees = tutor_utils()->get_option('tutor_earning_fees'); |
| 340 | $enable_fees_deducting = tutor_utils()->avalue_dot('enable_fees_deducting', $tutor_earning_fees); |
| 341 | |
| 342 | $course_price_grand_total = $total_price; |
| 343 | |
| 344 | if ($enable_fees_deducting){ |
| 345 | $fees_name = tutor_utils()->avalue_dot('fees_name', $tutor_earning_fees); |
| 346 | $fees_amount = (int) tutor_utils()->avalue_dot('fees_amount', $tutor_earning_fees); |
| 347 | $fees_type = tutor_utils()->avalue_dot('fees_type', $tutor_earning_fees); |
| 348 | |
| 349 | if ($fees_amount > 0) { |
| 350 | if ( $fees_type === 'percent' ) { |
| 351 | $fees_amount = ( $total_price * $fees_amount ) / 100; |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | if ( $fees_type === 'fixed' ) { |
| 356 | $course_price_grand_total = $total_price - $fees_amount; |
| 357 | }*/ |
| 358 | |
| 359 | $course_price_grand_total = $total_price - $fees_amount; |
| 360 | } |
| 361 | |
| 362 | $fees_deduct_data = array( |
| 363 | 'deduct_fees_amount' => $fees_amount, |
| 364 | 'deduct_fees_name' => $fees_name, |
| 365 | 'deduct_fees_type' => $fees_type, |
| 366 | ); |
| 367 | } |
| 368 | |
| 369 | $instructor_rate = tutor_utils()->get_option('earning_instructor_commission'); |
| 370 | $admin_rate = tutor_utils()->get_option('earning_admin_commission'); |
| 371 | |
| 372 | $instructor_amount = 0; |
| 373 | if ($instructor_rate > 0){ |
| 374 | $instructor_amount = ($course_price_grand_total * $instructor_rate) / 100; |
| 375 | } |
| 376 | |
| 377 | $admin_amount = 0; |
| 378 | if ($admin_rate > 0){ |
| 379 | $admin_amount = ($course_price_grand_total * $admin_rate) / 100; |
| 380 | } |
| 381 | |
| 382 | $earning_data = array( |
| 383 | 'user_id' => $user_id, |
| 384 | 'course_id' => $course_id, |
| 385 | 'order_id' => $order_id, |
| 386 | 'order_status' => $order_status, |
| 387 | 'course_price_total' => $total_price, |
| 388 | 'course_price_grand_total' => $course_price_grand_total, |
| 389 | |
| 390 | 'instructor_amount' => $instructor_amount, |
| 391 | 'instructor_rate' => $instructor_rate, |
| 392 | 'admin_amount' => $admin_amount, |
| 393 | 'admin_rate' => $admin_rate, |
| 394 | |
| 395 | 'commission_type' => 'percent', |
| 396 | 'process_by' => 'woocommerce', |
| 397 | 'created_at' => date( 'Y-m-d H:i:s', tutor_time()), |
| 398 | ); |
| 399 | $earning_data = apply_filters('tutor_new_earning_data', array_merge($earning_data, $fees_deduct_data)); |
| 400 | |
| 401 | $wpdb->insert($wpdb->prefix.'tutor_earnings', $earning_data); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | |
| 406 | /** |
| 407 | * @param $order_id |
| 408 | * @param $status_from |
| 409 | * @param $status_to |
| 410 | * |
| 411 | * Change Earning data status |
| 412 | * |
| 413 | * @since v.1.1.2 |
| 414 | */ |
| 415 | public function add_earning_data_status_change($order_id, $status_from, $status_to){ |
| 416 | if ( ! tutor_utils()->is_tutor_order($order_id)){ |
| 417 | return; |
| 418 | } |
| 419 | global $wpdb; |
| 420 | |
| 421 | $is_earning_data = (int) $wpdb->get_var("SELECT COUNT(earning_id) FROM {$wpdb->prefix}tutor_earnings WHERE order_id = {$order_id} "); |
| 422 | if ($is_earning_data){ |
| 423 | $wpdb->update( $wpdb->prefix.'tutor_earnings', array( 'order_status' => $status_to ), array( 'order_id' => $order_id ) ); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | } |