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