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
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
Template.php
7 years ago
Theme_Compatibility.php
7 years ago
Tools.php
7 years ago
TutorEDD.php
7 years ago
Tutor_Base.php
7 years ago
Tutor_List_Table.php
7 years ago
User.php
7 years ago
Utils.php
7 years ago
Video_Stream.php
7 years ago
WooCommerce.php
7 years ago
init.php
7 years ago
WooCommerce.php
267 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 | class WooCommerce extends Tutor_Base { |
| 12 | |
| 13 | public function __construct() { |
| 14 | parent::__construct(); |
| 15 | |
| 16 | add_action('tutor_options_before_woocommerce', array($this, 'notice_before_option')); |
| 17 | |
| 18 | //Add option settings |
| 19 | add_filter('tutor/options/attr', array($this, 'add_options')); |
| 20 | |
| 21 | $course_sell = tutor_utils()->get_option('enable_course_sell_by_woocommerce'); |
| 22 | if ( ! $course_sell){ |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Is Course Purchasable |
| 28 | */ |
| 29 | add_filter('is_course_purchasable', array($this, 'is_course_purchasable'), 10, 2); |
| 30 | add_filter('get_tutor_course_price', array($this, 'get_tutor_course_price'), 10, 2); |
| 31 | add_filter('tutor_course_sell_by', array($this, 'tutor_course_sell_by')); |
| 32 | |
| 33 | add_filter('product_type_options', array($this, 'add_tutor_type_in_wc_product')); |
| 34 | |
| 35 | add_action( 'add_meta_boxes', array($this, 'register_meta_box') ); |
| 36 | add_action('save_post_'.$this->course_post_type, array($this, 'save_course_meta')); |
| 37 | add_action('save_post_product', array($this, 'save_wc_product_meta')); |
| 38 | |
| 39 | add_action('tutor_course/single/before/enroll', 'wc_print_notices'); |
| 40 | |
| 41 | add_action('woocommerce_new_order_item', array($this, 'course_placing_order'), 10, 3); |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * Order Status Hook |
| 46 | * |
| 47 | * Remove course from active courses if an order is cancelled or refunded |
| 48 | */ |
| 49 | add_action( 'woocommerce_order_status_changed', array( $this, 'enrolled_courses_status_change' ), 10, 3 ); |
| 50 | |
| 51 | } |
| 52 | |
| 53 | public function notice_before_option(){ |
| 54 | $has_wc = tutor_utils()->has_wc(); |
| 55 | if ($has_wc){ |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | ob_start(); |
| 60 | ?> |
| 61 | <div class="tutor-notice-warning"> |
| 62 | <p> |
| 63 | <?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 |
| 64 | WooCommerce plugin installed. Get back on this page after installing the plugin and enable the following feature to start selling |
| 65 | courses with Tutor.', 'tutor'); ?> |
| 66 | </p> |
| 67 | <p><?php _e('This notice will disappear after activating <strong>WooCommerce</strong>', 'tutor'); ?></p> |
| 68 | </div> |
| 69 | <?php |
| 70 | echo ob_get_clean(); |
| 71 | } |
| 72 | |
| 73 | public function is_course_purchasable($bool, $course_id){ |
| 74 | if ( ! tutor_utils()->has_wc()){ |
| 75 | return false; |
| 76 | } |
| 77 | /* |
| 78 | $course_sell = tutor_utils()->get_option('enable_course_sell_by_woocommerce'); |
| 79 | if ( ! $course_sell){ |
| 80 | return false; |
| 81 | }*/ |
| 82 | |
| 83 | $course_id = tutor_utils()->get_post_id($course_id); |
| 84 | $has_product_id = get_post_meta($course_id, '_tutor_course_product_id', true); |
| 85 | if ($has_product_id){ |
| 86 | return true; |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | public function get_tutor_course_price($price, $course_id){ |
| 92 | |
| 93 | $product_id = tutor_utils()->get_course_product_id($course_id); |
| 94 | $product = wc_get_product( $product_id ); |
| 95 | |
| 96 | if ($product) { |
| 97 | ob_start(); |
| 98 | ?> |
| 99 | <p class="price"> |
| 100 | <?php echo $product->get_price_html(); ?> |
| 101 | </p> |
| 102 | <?php |
| 103 | return ob_get_clean(); |
| 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | public function tutor_course_sell_by(){ |
| 109 | return 'woocommerce'; |
| 110 | } |
| 111 | |
| 112 | public function add_tutor_type_in_wc_product($types){ |
| 113 | $types['tutor_product'] = array( |
| 114 | 'id' => '_tutor_product', |
| 115 | 'wrapper_class' => 'show_if_simple', |
| 116 | 'label' => __( 'For Tutor', 'tutor' ), |
| 117 | 'description' => __( 'This checkmark ensure that you will sell a specif course via this product.', 'tutor' ), |
| 118 | 'default' => 'no', |
| 119 | ); |
| 120 | |
| 121 | return $types; |
| 122 | } |
| 123 | |
| 124 | public function register_meta_box(){ |
| 125 | add_meta_box( 'tutor-attach-product', __( 'Add Product', 'tutor' ), array($this, 'course_add_product_metabox'), $this->course_post_type, 'advanced', 'high' ); |
| 126 | } |
| 127 | |
| 128 | public function course_add_product_metabox(){ |
| 129 | include tutor()->path.'views/metabox/course-add-product-metabox.php'; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @param $post_ID |
| 134 | * |
| 135 | * Save course meta for attaching product |
| 136 | */ |
| 137 | public function save_course_meta($post_ID){ |
| 138 | $product_id = (int) tutor_utils()->avalue_dot('_tutor_course_product_id', $_POST); |
| 139 | if ($product_id){ |
| 140 | update_post_meta($post_ID, '_tutor_course_product_id', $product_id); |
| 141 | //Mark product for woocommerce |
| 142 | update_post_meta($product_id, '_virtual', 'yes'); |
| 143 | update_post_meta($product_id, '_tutor_product', 'yes'); |
| 144 | }else{ |
| 145 | delete_post_meta($post_ID, '_tutor_course_product_id'); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | public function save_wc_product_meta($post_ID){ |
| 150 | $is_tutor_product = tutor_utils()->avalue_dot('_tutor_product', $_POST); |
| 151 | if ($is_tutor_product === 'on'){ |
| 152 | update_post_meta($post_ID, '_tutor_product', 'yes'); |
| 153 | }else{ |
| 154 | delete_post_meta($post_ID, '_tutor_product'); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Do something after course order place |
| 160 | */ |
| 161 | |
| 162 | public function course_placing_order( $item_id, $item, $order_id){ |
| 163 | $item = new \WC_Order_Item_Product($item); |
| 164 | |
| 165 | $product_id = $item->get_product_id(); |
| 166 | $if_has_course = tutor_utils()->product_belongs_with_course($product_id); |
| 167 | |
| 168 | if ($if_has_course){ |
| 169 | $course_id = $if_has_course->post_id; |
| 170 | tutor_utils()->do_enroll($course_id, $order_id); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | |
| 175 | /** |
| 176 | * |
| 177 | * Take enrolled course action based on order status change |
| 178 | */ |
| 179 | |
| 180 | public function enrolled_courses_status_change($order_id, $status_from, $status_to){ |
| 181 | if ( ! tutor_utils()->is_tutor_order($order_id)){ |
| 182 | return; |
| 183 | } |
| 184 | global $wpdb; |
| 185 | |
| 186 | $enrolled_ids_with_course = $this->get_course_enrolled_ids_by_order_id($order_id); |
| 187 | |
| 188 | if ($enrolled_ids_with_course){ |
| 189 | $enrolled_ids = wp_list_pluck($enrolled_ids_with_course, 'enrolled_id'); |
| 190 | |
| 191 | if (is_array($enrolled_ids) && count($enrolled_ids)){ |
| 192 | foreach ($enrolled_ids as $enrolled_id){ |
| 193 | $wpdb->update( $wpdb->posts, array( 'post_status' => $status_to ), array( 'ID' => $enrolled_id ) ); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * @param $order_id |
| 201 | * |
| 202 | * @return array|bool |
| 203 | */ |
| 204 | public function get_course_enrolled_ids_by_order_id($order_id){ |
| 205 | global $wpdb; |
| 206 | //Getting all of courses ids within this order |
| 207 | |
| 208 | $courses_ids = $wpdb->get_results("SELECT * FROM {$wpdb->postmeta} WHERE post_id = {$order_id} AND meta_key LIKE '_tutor_order_for_course_id_%' "); |
| 209 | |
| 210 | if (is_array($courses_ids) && count($courses_ids)){ |
| 211 | $course_enrolled_by_order = array(); |
| 212 | foreach ($courses_ids as $courses_id){ |
| 213 | $course_id = str_replace('_tutor_order_for_course_id_', '',$courses_id->meta_key); |
| 214 | //array(order_id => array('course_id' => $course_id, 'enrolled_id' => enrolled_id)) |
| 215 | $course_enrolled_by_order[$courses_id->post_id] = array('course_id' => $course_id, 'enrolled_id' => $courses_id->meta_value); |
| 216 | } |
| 217 | return $course_enrolled_by_order; |
| 218 | } |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Remove course |
| 224 | * |
| 225 | * TODO: right now it's unused |
| 226 | */ |
| 227 | public function remove_active_course($order_id){ |
| 228 | global $wpdb; |
| 229 | //Getting all of courses ids within this order |
| 230 | |
| 231 | $courses_ids = $wpdb->get_results("SELECT * FROM {$wpdb->postmeta} WHERE post_id = {$order_id} meta_key LIKE '_tutor_order_for_course_id_%' "); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /** |
| 236 | * @param $attr |
| 237 | * |
| 238 | * @return mixed |
| 239 | * |
| 240 | * Add option for WooCommerce settings |
| 241 | */ |
| 242 | public function add_options($attr){ |
| 243 | |
| 244 | $attr['woocommerce'] = array( |
| 245 | 'label' => __( 'WooCommerce', 'tutor' ), |
| 246 | |
| 247 | 'sections' => array( |
| 248 | 'general' => array( |
| 249 | 'label' => __('General', 'tutor'), |
| 250 | 'desc' => __('WooCommerce Settings', 'tutor'), |
| 251 | 'fields' => array( |
| 252 | 'enable_course_sell_by_woocommerce' => array( |
| 253 | 'type' => 'checkbox', |
| 254 | 'label' => __('Enable / Disable', 'tutor'), |
| 255 | 'label_title' => __('Enable WooComerce to sell course', 'tutor'), |
| 256 | 'desc' => __('By integrating WooCommerce, you can sell your course', 'tutor'), |
| 257 | ), |
| 258 | ), |
| 259 | ), |
| 260 | ), |
| 261 | ); |
| 262 | |
| 263 | return $attr; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | } |