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