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.2 3.5.0 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / classes / WooCommerce.php
tutor / classes Last commit date
Addons.php 6 years ago Admin.php 6 years ago Ajax.php 6 years ago Assets.php 6 years ago Course.php 6 years ago Course_Widget.php 6 years ago Dashboard.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 6 years ago Post_types.php 6 years ago Q_and_A.php 6 years ago Question.php 6 years ago Question_Answers_List.php 6 years ago Quiz.php 6 years ago Quiz_Attempts_List.php 6 years ago Rewrite_Rules.php 6 years ago Shortcode.php 6 years ago Student.php 6 years ago Students_List.php 6 years ago Taxonomies.php 6 years ago Template.php 6 years ago Theme_Compatibility.php 6 years ago Tools.php 6 years ago Tutor.php 6 years ago TutorEDD.php 6 years ago Tutor_Base.php 6 years ago Tutor_List_Table.php 6 years ago Upgrader.php 6 years ago User.php 6 years ago Utils.php 6 years ago Video_Stream.php 6 years ago Withdraw.php 6 years ago Withdraw_Requests_List.php 6 years ago WooCommerce.php 6 years ago
WooCommerce.php
402 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 = tutor_utils()->avalue_dot('_tutor_course_product_id', $_POST);
152
153 if ($product_id === '-1'){
154 delete_post_meta($post_ID, '_tutor_course_product_id');
155 }else{
156 $product_id = (int) $product_id;
157 if ($product_id){
158 update_post_meta($post_ID, '_tutor_course_product_id', $product_id);
159 //Mark product for woocommerce
160 update_post_meta($product_id, '_virtual', 'yes');
161 update_post_meta($product_id, '_tutor_product', 'yes');
162 }
163 }
164 }
165
166 public function save_wc_product_meta($post_ID){
167 $is_tutor_product = tutor_utils()->avalue_dot('_tutor_product', $_POST);
168 if ($is_tutor_product === 'on'){
169 update_post_meta($post_ID, '_tutor_product', 'yes');
170 }else{
171 delete_post_meta($post_ID, '_tutor_product');
172 }
173 }
174
175 /**
176 * Do something after course order place
177 */
178 public function course_placing_order( $item_id, $item, $order_id){
179 $item = new \WC_Order_Item_Product($item);
180
181 $product_id = $item->get_product_id();
182 $if_has_course = tutor_utils()->product_belongs_with_course($product_id);
183
184 if ($if_has_course){
185 $course_id = $if_has_course->post_id;
186 tutor_utils()->do_enroll($course_id, $order_id);
187 }
188 }
189
190
191 /**
192 *
193 * Take enrolled course action based on order status change
194 */
195
196 public function enrolled_courses_status_change($order_id, $status_from, $status_to){
197 if ( ! tutor_utils()->is_tutor_order($order_id)){
198 return;
199 }
200 global $wpdb;
201
202 $enrolled_ids_with_course = $this->get_course_enrolled_ids_by_order_id($order_id);
203
204 if ($enrolled_ids_with_course){
205 $enrolled_ids = wp_list_pluck($enrolled_ids_with_course, 'enrolled_id');
206
207 if (is_array($enrolled_ids) && count($enrolled_ids)){
208 foreach ($enrolled_ids as $enrolled_id){
209 $wpdb->update( $wpdb->posts, array( 'post_status' => $status_to ), array( 'ID' => $enrolled_id ) );
210 }
211 }
212 }
213 }
214
215 /**
216 * @param $order_id
217 *
218 * @return array|bool
219 */
220 public function get_course_enrolled_ids_by_order_id($order_id){
221 global $wpdb;
222 //Getting all of courses ids within this order
223
224 $courses_ids = $wpdb->get_results("SELECT * FROM {$wpdb->postmeta} WHERE post_id = {$order_id} AND meta_key LIKE '_tutor_order_for_course_id_%' ");
225
226 if (is_array($courses_ids) && count($courses_ids)){
227 $course_enrolled_by_order = array();
228 foreach ($courses_ids as $courses_id){
229 $course_id = str_replace('_tutor_order_for_course_id_', '',$courses_id->meta_key);
230 //array(order_id => array('course_id' => $course_id, 'enrolled_id' => enrolled_id, 'order_id' => $courses_id->post_id))
231 $course_enrolled_by_order[] = array('course_id' => $course_id, 'enrolled_id' => $courses_id->meta_value, 'order_id' => $courses_id->post_id );
232 }
233 return $course_enrolled_by_order;
234 }
235 return false;
236 }
237
238 /**
239 * Remove course
240 *
241 * TODO: right now it's unused
242 */
243 public function remove_active_course($order_id){
244 global $wpdb;
245 //Getting all of courses ids within this order
246
247 $courses_ids = $wpdb->get_results("SELECT * FROM {$wpdb->postmeta} WHERE post_id = {$order_id} meta_key LIKE '_tutor_order_for_course_id_%' ");
248 }
249
250
251 /**
252 * @param $attr
253 *
254 * @return mixed
255 *
256 * Add option for WooCommerce settings
257 */
258 public function add_options($attr){
259
260 $attr['woocommerce'] = array(
261 'label' => __( 'WooCommerce', 'tutor' ),
262
263 'sections' => array(
264 'general' => array(
265 'label' => __('General', 'tutor'),
266 'desc' => __('WooCommerce Settings', 'tutor'),
267 'fields' => array(
268 'enable_course_sell_by_woocommerce' => array(
269 'type' => 'checkbox',
270 'label' => __('Enable / Disable', 'tutor'),
271 'label_title' => __('Enable WooComerce to sell course', 'tutor'),
272 'desc' => __('By integrating WooCommerce, you can sell your course', 'tutor'),
273 ),
274 'enable_guest_course_cart' => array(
275 'type' => 'checkbox',
276 'label' => __('Enable / Disable', 'tutor'),
277 'label_title' => __('Enable Add to cart by guest', 'tutor'),
278 'desc' => __('From the course details page, any user without login can add to cart course', 'tutor'),
279 ),
280 ),
281 ),
282 ),
283 );
284
285 return $attr;
286 }
287
288 /**
289 * @param $item_id
290 * @param $item
291 * @param $order_id
292 *
293 * Adding Earning Data processing WooCommerce
294 *
295 * @since v.1.1.2
296 */
297 public function add_earning_data( $item_id, $item, $order_id){
298 global $wpdb;
299 $item = new \WC_Order_Item_Product($item);
300
301 $product_id = $item->get_product_id();
302 $if_has_course = tutor_utils()->product_belongs_with_course($product_id);
303
304 if ($if_has_course){
305
306 $enable_tutor_earning = tutor_utils()->get_option('enable_tutor_earning');
307 if ( ! $enable_tutor_earning){
308 return;
309 }
310
311 $course_id = $if_has_course->post_id;
312 $user_id = $wpdb->get_var("SELECT post_author FROM {$wpdb->posts} WHERE ID = {$course_id} ");
313 $order_status = $wpdb->get_var("SELECT post_status from {$wpdb->posts} where ID = {$order_id} ");
314
315 $total_price = $item->get_total();
316
317 $fees_deduct_data = array();
318 $tutor_earning_fees = tutor_utils()->get_option('tutor_earning_fees');
319 $enable_fees_deducting = tutor_utils()->avalue_dot('enable_fees_deducting', $tutor_earning_fees);
320
321 $course_price_grand_total = $total_price;
322
323 if ($enable_fees_deducting){
324 $fees_name = tutor_utils()->avalue_dot('fees_name', $tutor_earning_fees);
325 $fees_amount = tutor_utils()->avalue_dot('fees_amount', $tutor_earning_fees);
326 $fees_type = tutor_utils()->avalue_dot('fees_type', $tutor_earning_fees);
327
328 if ($fees_amount > 0) {
329 if ( $fees_type === 'percent' ) {
330 $course_price_grand_total = ( $total_price * $fees_amount ) / 100;
331 }
332 if ( $fees_type === 'fixed' ) {
333 $course_price_grand_total = $total_price - $fees_amount;
334 }
335 }
336
337 $fees_deduct_data = array(
338 'deduct_fees_amount' => $fees_amount,
339 'deduct_fees_name' => $fees_name,
340 'deduct_fees_type' => $fees_type,
341 );
342 }
343
344 $instructor_rate = tutor_utils()->get_option('earning_instructor_commission');
345 $admin_rate = tutor_utils()->get_option('earning_admin_commission');
346
347 $instructor_amount = 0;
348 if ($instructor_rate > 0){
349 $instructor_amount = ($course_price_grand_total * $instructor_rate) / 100;
350 }
351
352 $admin_amount = 0;
353 if ($admin_rate > 0){
354 $admin_amount = ($course_price_grand_total * $admin_rate) / 100;
355 }
356
357 $earning_data = array(
358 'user_id' => $user_id,
359 'course_id' => $course_id,
360 'order_id' => $order_id,
361 'order_status' => $order_status,
362 'course_price_total' => $total_price,
363 'course_price_grand_total' => $course_price_grand_total,
364
365 'instructor_amount' => $instructor_amount,
366 'instructor_rate' => $instructor_rate,
367 'admin_amount' => $admin_amount,
368 'admin_rate' => $admin_rate,
369
370 'commission_type' => 'percent',
371 'process_by' => 'woocommerce',
372 'created_at' => date( 'Y-m-d H:i:s'),
373 );
374 $earning_data = apply_filters('tutor_new_earning_data', array_merge($earning_data, $fees_deduct_data));
375
376 $wpdb->insert($wpdb->prefix.'tutor_earnings', $earning_data);
377 }
378 }
379
380
381 /**
382 * @param $order_id
383 * @param $status_from
384 * @param $status_to
385 *
386 * Change Earning data status
387 *
388 * @since v.1.1.2
389 */
390 public function add_earning_data_status_change($order_id, $status_from, $status_to){
391 if ( ! tutor_utils()->is_tutor_order($order_id)){
392 return;
393 }
394 global $wpdb;
395
396 $is_earning_data = (int) $wpdb->get_var("SELECT COUNT(earning_id) FROM {$wpdb->prefix}tutor_earnings WHERE order_id = {$order_id} ");
397 if ($is_earning_data){
398 $wpdb->update( $wpdb->prefix.'tutor_earnings', array( 'order_status' => $status_to ), array( 'order_id' => $order_id ) );
399 }
400 }
401
402 }