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