@@ -12,741 +12,741 @@ discard block |
||
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | 14 | Class Sensei_WC{ |
| 15 | - /** |
|
| 16 | - * Load the files needed for the woocommerce integration. |
|
| 17 | - * |
|
| 18 | - * @since 1.9.0 |
|
| 19 | - */ |
|
| 20 | - public static function load_woocommerce_integration_hooks(){ |
|
| 15 | + /** |
|
| 16 | + * Load the files needed for the woocommerce integration. |
|
| 17 | + * |
|
| 18 | + * @since 1.9.0 |
|
| 19 | + */ |
|
| 20 | + public static function load_woocommerce_integration_hooks(){ |
|
| 21 | 21 | |
| 22 | - $woocommerce_hooks_file_path = Sensei()->plugin_path() . 'includes/hooks/woocommerce.php'; |
|
| 23 | - require_once( $woocommerce_hooks_file_path ); |
|
| 22 | + $woocommerce_hooks_file_path = Sensei()->plugin_path() . 'includes/hooks/woocommerce.php'; |
|
| 23 | + require_once( $woocommerce_hooks_file_path ); |
|
| 24 | 24 | |
| 25 | - } |
|
| 26 | - /** |
|
| 27 | - * check if WooCommerce plugin is loaded and allowed by Sensei |
|
| 28 | - * |
|
| 29 | - * @since 1.9.0 |
|
| 30 | - * @return bool |
|
| 31 | - */ |
|
| 32 | - public static function is_woocommerce_active(){ |
|
| 25 | + } |
|
| 26 | + /** |
|
| 27 | + * check if WooCommerce plugin is loaded and allowed by Sensei |
|
| 28 | + * |
|
| 29 | + * @since 1.9.0 |
|
| 30 | + * @return bool |
|
| 31 | + */ |
|
| 32 | + public static function is_woocommerce_active(){ |
|
| 33 | 33 | |
| 34 | - $is_woocommerce_enabled_in_settings = isset( Sensei()->settings->settings['woocommerce_enabled'] ) && Sensei()->settings->settings['woocommerce_enabled']; |
|
| 35 | - return self::is_woocommerce_present() && $is_woocommerce_enabled_in_settings; |
|
| 34 | + $is_woocommerce_enabled_in_settings = isset( Sensei()->settings->settings['woocommerce_enabled'] ) && Sensei()->settings->settings['woocommerce_enabled']; |
|
| 35 | + return self::is_woocommerce_present() && $is_woocommerce_enabled_in_settings; |
|
| 36 | 36 | |
| 37 | - } // end is_woocommerce_active |
|
| 37 | + } // end is_woocommerce_active |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Checks if the WooCommerce plugin is installed and activation. |
|
| 41 | - * |
|
| 42 | - * If you need to check if WooCommerce is activated use Sensei_Utils::is_woocommerce_active(). |
|
| 43 | - * This function does nott check to see if the Sensei setting for WooCommerce is enabled. |
|
| 44 | - * |
|
| 45 | - * @since 1.9.0 |
|
| 46 | - * |
|
| 47 | - * @return bool |
|
| 48 | - */ |
|
| 49 | - public static function is_woocommerce_present(){ |
|
| 39 | + /** |
|
| 40 | + * Checks if the WooCommerce plugin is installed and activation. |
|
| 41 | + * |
|
| 42 | + * If you need to check if WooCommerce is activated use Sensei_Utils::is_woocommerce_active(). |
|
| 43 | + * This function does nott check to see if the Sensei setting for WooCommerce is enabled. |
|
| 44 | + * |
|
| 45 | + * @since 1.9.0 |
|
| 46 | + * |
|
| 47 | + * @return bool |
|
| 48 | + */ |
|
| 49 | + public static function is_woocommerce_present(){ |
|
| 50 | 50 | |
| 51 | - $active_plugins = (array) get_option( 'active_plugins', array() ); |
|
| 51 | + $active_plugins = (array) get_option( 'active_plugins', array() ); |
|
| 52 | 52 | |
| 53 | - if ( is_multisite() ){ |
|
| 53 | + if ( is_multisite() ){ |
|
| 54 | 54 | |
| 55 | - $active_plugins = array_merge( $active_plugins, get_site_option( 'active_sitewide_plugins', array() ) ); |
|
| 55 | + $active_plugins = array_merge( $active_plugins, get_site_option( 'active_sitewide_plugins', array() ) ); |
|
| 56 | 56 | |
| 57 | - } |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - $is_woocommerce_plugin_present_and_activated = in_array( 'woocommerce/woocommerce.php', $active_plugins ) || array_key_exists( 'woocommerce/woocommerce.php', $active_plugins ); |
|
| 59 | + $is_woocommerce_plugin_present_and_activated = in_array( 'woocommerce/woocommerce.php', $active_plugins ) || array_key_exists( 'woocommerce/woocommerce.php', $active_plugins ); |
|
| 60 | 60 | |
| 61 | - return class_exists( 'Woocommerce' ) || $is_woocommerce_plugin_present_and_activated; |
|
| 61 | + return class_exists( 'Woocommerce' ) || $is_woocommerce_plugin_present_and_activated; |
|
| 62 | 62 | |
| 63 | - }// end is_woocommerce_present |
|
| 63 | + }// end is_woocommerce_present |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Find the order active number (completed or processing ) for a given user on a course. It will return the latest order. |
|
| 67 | - * |
|
| 68 | - * If multiple exist we will return the latest order. |
|
| 69 | - * |
|
| 70 | - * @param $user_id |
|
| 71 | - * @param $course_id |
|
| 72 | - * @return array $user_course_orders |
|
| 73 | - */ |
|
| 74 | - public static function get_learner_course_active_order_id( $user_id, $course_id ){ |
|
| 65 | + /** |
|
| 66 | + * Find the order active number (completed or processing ) for a given user on a course. It will return the latest order. |
|
| 67 | + * |
|
| 68 | + * If multiple exist we will return the latest order. |
|
| 69 | + * |
|
| 70 | + * @param $user_id |
|
| 71 | + * @param $course_id |
|
| 72 | + * @return array $user_course_orders |
|
| 73 | + */ |
|
| 74 | + public static function get_learner_course_active_order_id( $user_id, $course_id ){ |
|
| 75 | 75 | |
| 76 | - $course_product_id = get_post_meta( $course_id, '_course_woocommerce_product', true ); |
|
| 76 | + $course_product_id = get_post_meta( $course_id, '_course_woocommerce_product', true ); |
|
| 77 | 77 | |
| 78 | - $orders_query = new WP_Query( array( |
|
| 79 | - 'post_type' => 'shop_order', |
|
| 80 | - 'posts_per_page' => -1, |
|
| 81 | - 'post_status' => array( 'wc-processing', 'wc-completed' ), |
|
| 82 | - 'meta_key'=> '_customer_user', |
|
| 83 | - 'meta_value'=> $user_id, |
|
| 84 | - ) ); |
|
| 78 | + $orders_query = new WP_Query( array( |
|
| 79 | + 'post_type' => 'shop_order', |
|
| 80 | + 'posts_per_page' => -1, |
|
| 81 | + 'post_status' => array( 'wc-processing', 'wc-completed' ), |
|
| 82 | + 'meta_key'=> '_customer_user', |
|
| 83 | + 'meta_value'=> $user_id, |
|
| 84 | + ) ); |
|
| 85 | 85 | |
| 86 | - if( $orders_query->post_count == 0 ){ |
|
| 86 | + if( $orders_query->post_count == 0 ){ |
|
| 87 | 87 | |
| 88 | - return false; |
|
| 88 | + return false; |
|
| 89 | 89 | |
| 90 | - } |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - foreach( $orders_query->get_posts() as $order ){ |
|
| 92 | + foreach( $orders_query->get_posts() as $order ){ |
|
| 93 | 93 | |
| 94 | - $order = new WC_Order( $order->ID ); |
|
| 95 | - $items = $order->get_items(); |
|
| 94 | + $order = new WC_Order( $order->ID ); |
|
| 95 | + $items = $order->get_items(); |
|
| 96 | 96 | |
| 97 | - $user_orders = array(); |
|
| 97 | + $user_orders = array(); |
|
| 98 | 98 | |
| 99 | - foreach( $items as $item ){ |
|
| 99 | + foreach( $items as $item ){ |
|
| 100 | 100 | |
| 101 | - // if the product id on the order and the one given to this function |
|
| 102 | - // this order has been placed by the given user on the given course. |
|
| 103 | - $product = wc_get_product( $item['product_id'] ); |
|
| 101 | + // if the product id on the order and the one given to this function |
|
| 102 | + // this order has been placed by the given user on the given course. |
|
| 103 | + $product = wc_get_product( $item['product_id'] ); |
|
| 104 | 104 | |
| 105 | - if ( is_object( $product ) && $product->is_type( 'variable' )) { |
|
| 105 | + if ( is_object( $product ) && $product->is_type( 'variable' )) { |
|
| 106 | 106 | |
| 107 | - $item_product_id = $item['variation_id']; |
|
| 107 | + $item_product_id = $item['variation_id']; |
|
| 108 | 108 | |
| 109 | - } else { |
|
| 109 | + } else { |
|
| 110 | 110 | |
| 111 | - $item_product_id = $item['product_id']; |
|
| 111 | + $item_product_id = $item['product_id']; |
|
| 112 | 112 | |
| 113 | - } |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - if( $course_product_id == $item_product_id ){ |
|
| 115 | + if( $course_product_id == $item_product_id ){ |
|
| 116 | 116 | |
| 117 | - return $order->id; |
|
| 117 | + return $order->id; |
|
| 118 | 118 | |
| 119 | - } |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | 121 | |
| 122 | - }//end for each order item |
|
| 122 | + }//end for each order item |
|
| 123 | 123 | |
| 124 | - } // end for each order |
|
| 124 | + } // end for each order |
|
| 125 | 125 | |
| 126 | - // if we reach this place we found no order |
|
| 127 | - return false; |
|
| 126 | + // if we reach this place we found no order |
|
| 127 | + return false; |
|
| 128 | 128 | |
| 129 | - } // end get_learner_course_active_order_ids |
|
| 129 | + } // end get_learner_course_active_order_ids |
|
| 130 | 130 | |
| 131 | - /** |
|
| 132 | - * Output WooCommerce specific course filters |
|
| 133 | - * Removing the paged argument |
|
| 134 | - * |
|
| 135 | - * @since 1.9.0 |
|
| 136 | - * @param $filter_links |
|
| 137 | - * @return mixed |
|
| 138 | - */ |
|
| 139 | - public static function add_course_archive_wc_filter_links( $filter_links ){ |
|
| 131 | + /** |
|
| 132 | + * Output WooCommerce specific course filters |
|
| 133 | + * Removing the paged argument |
|
| 134 | + * |
|
| 135 | + * @since 1.9.0 |
|
| 136 | + * @param $filter_links |
|
| 137 | + * @return mixed |
|
| 138 | + */ |
|
| 139 | + public static function add_course_archive_wc_filter_links( $filter_links ){ |
|
| 140 | 140 | |
| 141 | - $free_courses = self::get_free_courses(); |
|
| 142 | - $paid_courses = self::get_paid_courses(); |
|
| 141 | + $free_courses = self::get_free_courses(); |
|
| 142 | + $paid_courses = self::get_paid_courses(); |
|
| 143 | 143 | |
| 144 | - if ( empty( $free_courses ) || empty( $paid_courses ) ){ |
|
| 145 | - // do not show any WooCommerce filters if all courses are |
|
| 146 | - // free or if all courses are paid |
|
| 147 | - return $filter_links; |
|
| 144 | + if ( empty( $free_courses ) || empty( $paid_courses ) ){ |
|
| 145 | + // do not show any WooCommerce filters if all courses are |
|
| 146 | + // free or if all courses are paid |
|
| 147 | + return $filter_links; |
|
| 148 | 148 | |
| 149 | - } |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - $filter_links[] = array( |
|
| 152 | - 'id'=>'paid' , |
|
| 153 | - 'url'=> add_query_arg( array( 'course_filter'=>'paid'), Sensei_Course::get_courses_page_url() ), |
|
| 154 | - 'title'=>__( 'Paid', 'woothemes-sensei' ) |
|
| 155 | - ); |
|
| 151 | + $filter_links[] = array( |
|
| 152 | + 'id'=>'paid' , |
|
| 153 | + 'url'=> add_query_arg( array( 'course_filter'=>'paid'), Sensei_Course::get_courses_page_url() ), |
|
| 154 | + 'title'=>__( 'Paid', 'woothemes-sensei' ) |
|
| 155 | + ); |
|
| 156 | 156 | |
| 157 | - $filter_links[] = array( |
|
| 158 | - 'id'=>'free', |
|
| 159 | - 'url'=> add_query_arg( array( 'course_filter'=>'free'), Sensei_Course::get_courses_page_url() ), |
|
| 160 | - 'title'=>__( 'Free', 'woothemes-sensei' ) |
|
| 161 | - ); |
|
| 157 | + $filter_links[] = array( |
|
| 158 | + 'id'=>'free', |
|
| 159 | + 'url'=> add_query_arg( array( 'course_filter'=>'free'), Sensei_Course::get_courses_page_url() ), |
|
| 160 | + 'title'=>__( 'Free', 'woothemes-sensei' ) |
|
| 161 | + ); |
|
| 162 | 162 | |
| 163 | - return $filter_links; |
|
| 163 | + return $filter_links; |
|
| 164 | 164 | |
| 165 | - }// end add_course_archive_wc_filter_links |
|
| 165 | + }// end add_course_archive_wc_filter_links |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * Apply the free filter the the course query |
|
| 169 | - * getting all course with no products or products with zero price |
|
| 170 | - * |
|
| 171 | - * hooked into pre_get_posts |
|
| 172 | - * |
|
| 173 | - * @since 1.9.0 |
|
| 174 | - * @param WP_Query $query |
|
| 175 | - * @return WP_Query $query |
|
| 176 | - */ |
|
| 177 | - public static function course_archive_wc_filter_free( $query ){ |
|
| 167 | + /** |
|
| 168 | + * Apply the free filter the the course query |
|
| 169 | + * getting all course with no products or products with zero price |
|
| 170 | + * |
|
| 171 | + * hooked into pre_get_posts |
|
| 172 | + * |
|
| 173 | + * @since 1.9.0 |
|
| 174 | + * @param WP_Query $query |
|
| 175 | + * @return WP_Query $query |
|
| 176 | + */ |
|
| 177 | + public static function course_archive_wc_filter_free( $query ){ |
|
| 178 | 178 | |
| 179 | - if( isset( $_GET['course_filter'] ) && 'free' == $_GET['course_filter'] |
|
| 180 | - && 'course' == $query->get( 'post_type') && $query->is_main_query() ){ |
|
| 179 | + if( isset( $_GET['course_filter'] ) && 'free' == $_GET['course_filter'] |
|
| 180 | + && 'course' == $query->get( 'post_type') && $query->is_main_query() ){ |
|
| 181 | 181 | |
| 182 | - // setup the course meta query |
|
| 183 | - $meta_query = self::get_free_courses_meta_query_args(); |
|
| 182 | + // setup the course meta query |
|
| 183 | + $meta_query = self::get_free_courses_meta_query_args(); |
|
| 184 | 184 | |
| 185 | - // manipulate the query to return free courses |
|
| 186 | - $query->set('meta_query', $meta_query ); |
|
| 185 | + // manipulate the query to return free courses |
|
| 186 | + $query->set('meta_query', $meta_query ); |
|
| 187 | 187 | |
| 188 | - // don't show any paid courses |
|
| 189 | - $courses = self::get_paid_courses(); |
|
| 190 | - $ids = array(); |
|
| 191 | - foreach( $courses as $course ){ |
|
| 192 | - $ids[] = $course->ID; |
|
| 193 | - } |
|
| 194 | - $query->set( 'post__not_in', $ids ); |
|
| 188 | + // don't show any paid courses |
|
| 189 | + $courses = self::get_paid_courses(); |
|
| 190 | + $ids = array(); |
|
| 191 | + foreach( $courses as $course ){ |
|
| 192 | + $ids[] = $course->ID; |
|
| 193 | + } |
|
| 194 | + $query->set( 'post__not_in', $ids ); |
|
| 195 | 195 | |
| 196 | - }// end if course_filter |
|
| 196 | + }// end if course_filter |
|
| 197 | 197 | |
| 198 | - return $query; |
|
| 198 | + return $query; |
|
| 199 | 199 | |
| 200 | - }// course_archive_wc_filter_free |
|
| 200 | + }// course_archive_wc_filter_free |
|
| 201 | 201 | |
| 202 | - /** |
|
| 203 | - * Apply the paid filter to the course query on the courses page |
|
| 204 | - * will include all course with a product attached with a price |
|
| 205 | - * more than 0 |
|
| 206 | - * |
|
| 207 | - * hooked into pre_get_posts |
|
| 208 | - * |
|
| 209 | - * @since 1.9.0 |
|
| 210 | - * @param WP_Query $query |
|
| 211 | - * @return WP_Query $query |
|
| 212 | - */ |
|
| 213 | - public static function course_archive_wc_filter_paid( $query ){ |
|
| 202 | + /** |
|
| 203 | + * Apply the paid filter to the course query on the courses page |
|
| 204 | + * will include all course with a product attached with a price |
|
| 205 | + * more than 0 |
|
| 206 | + * |
|
| 207 | + * hooked into pre_get_posts |
|
| 208 | + * |
|
| 209 | + * @since 1.9.0 |
|
| 210 | + * @param WP_Query $query |
|
| 211 | + * @return WP_Query $query |
|
| 212 | + */ |
|
| 213 | + public static function course_archive_wc_filter_paid( $query ){ |
|
| 214 | 214 | |
| 215 | - if( isset( $_GET['course_filter'] ) && 'paid' == $_GET['course_filter'] |
|
| 216 | - && 'course' == $query->get( 'post_type') && $query->is_main_query() ){ |
|
| 217 | - |
|
| 218 | - // setup the course meta query |
|
| 219 | - $meta_query = self::get_paid_courses_meta_query_args(); |
|
| 220 | - |
|
| 221 | - // manipulate the query to return free courses |
|
| 222 | - $query->set('meta_query', $meta_query ); |
|
| 223 | - |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - return $query; |
|
| 227 | - |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Load the WooCommerce single product actions above |
|
| 232 | - * single courses if woocommerce is active allowing purchase |
|
| 233 | - * information and actions to be hooked from WooCommerce. |
|
| 234 | - */ |
|
| 235 | - public static function do_single_course_wc_single_product_action(){ |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * this hooks is documented within the WooCommerce plugin. |
|
| 239 | - */ |
|
| 240 | - if ( Sensei_WC::is_woocommerce_active() ) { |
|
| 241 | - |
|
| 242 | - do_action( 'woocommerce_before_single_product' ); |
|
| 243 | - |
|
| 244 | - } // End If Statement |
|
| 245 | - |
|
| 246 | - }// end do_single_course_wc_single_product_action |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * Hooking into the single lesson page to alter the |
|
| 250 | - * user access permissions based on if they have purchased the |
|
| 251 | - * course the lesson belongs to. |
|
| 252 | - * |
|
| 253 | - * This function will only return false or the passed in user_access value. |
|
| 254 | - * It doesn't return true in order to avoid altering other options. |
|
| 255 | - * |
|
| 256 | - * @since 1.9.0 |
|
| 257 | - * |
|
| 258 | - * @param $can_user_view_lesson |
|
| 259 | - * @param $lesson_id |
|
| 260 | - * @param $user_id |
|
| 261 | - * @return bool |
|
| 262 | - */ |
|
| 263 | - public static function alter_can_user_view_lesson ( $can_user_view_lesson, $lesson_id, $user_id ){ |
|
| 264 | - |
|
| 265 | - // do not override access to admins |
|
| 266 | - if( sensei_all_access() || Sensei_Utils::is_preview_lesson( $lesson_id ) ){ |
|
| 267 | - return true; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - // check if the course has a valid product attached to it |
|
| 271 | - // which the user should have purchased if they want to access |
|
| 272 | - // the current lesson |
|
| 273 | - $course_id = get_post_meta( $lesson_id , '_lesson_course', true); |
|
| 274 | - $wc_post_id = get_post_meta( $course_id, '_course_woocommerce_product', true ); |
|
| 275 | - $product = Sensei()->sensei_get_woocommerce_product_object($wc_post_id); |
|
| 276 | - if( isset ($product) && is_object($product) ){ |
|
| 277 | - |
|
| 278 | - // valid product found |
|
| 279 | - $order_id = self::get_learner_course_active_order_id( $user_id, $course_id ); |
|
| 280 | - |
|
| 281 | - // product has a successful order so this user may access the content |
|
| 282 | - // this function may only return false or the default |
|
| 283 | - // returning true may override other negatives which we don't want |
|
| 284 | - if( ! $order_id ){ |
|
| 285 | - |
|
| 286 | - return false; |
|
| 287 | - |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - // return the passed in value |
|
| 293 | - return $can_user_view_lesson; |
|
| 294 | - |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * Add course link to order thank you and details pages. |
|
| 299 | - * |
|
| 300 | - * @since 1.4.5 |
|
| 301 | - * @access public |
|
| 302 | - * |
|
| 303 | - * @return void |
|
| 304 | - */ |
|
| 305 | - public static function course_link_from_order( ) { |
|
| 306 | - |
|
| 307 | - if( ! is_order_received_page() ){ |
|
| 308 | - return; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - $order_id = get_query_var( 'order-received' ); |
|
| 215 | + if( isset( $_GET['course_filter'] ) && 'paid' == $_GET['course_filter'] |
|
| 216 | + && 'course' == $query->get( 'post_type') && $query->is_main_query() ){ |
|
| 217 | + |
|
| 218 | + // setup the course meta query |
|
| 219 | + $meta_query = self::get_paid_courses_meta_query_args(); |
|
| 220 | + |
|
| 221 | + // manipulate the query to return free courses |
|
| 222 | + $query->set('meta_query', $meta_query ); |
|
| 223 | + |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + return $query; |
|
| 227 | + |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Load the WooCommerce single product actions above |
|
| 232 | + * single courses if woocommerce is active allowing purchase |
|
| 233 | + * information and actions to be hooked from WooCommerce. |
|
| 234 | + */ |
|
| 235 | + public static function do_single_course_wc_single_product_action(){ |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * this hooks is documented within the WooCommerce plugin. |
|
| 239 | + */ |
|
| 240 | + if ( Sensei_WC::is_woocommerce_active() ) { |
|
| 241 | + |
|
| 242 | + do_action( 'woocommerce_before_single_product' ); |
|
| 243 | + |
|
| 244 | + } // End If Statement |
|
| 245 | + |
|
| 246 | + }// end do_single_course_wc_single_product_action |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * Hooking into the single lesson page to alter the |
|
| 250 | + * user access permissions based on if they have purchased the |
|
| 251 | + * course the lesson belongs to. |
|
| 252 | + * |
|
| 253 | + * This function will only return false or the passed in user_access value. |
|
| 254 | + * It doesn't return true in order to avoid altering other options. |
|
| 255 | + * |
|
| 256 | + * @since 1.9.0 |
|
| 257 | + * |
|
| 258 | + * @param $can_user_view_lesson |
|
| 259 | + * @param $lesson_id |
|
| 260 | + * @param $user_id |
|
| 261 | + * @return bool |
|
| 262 | + */ |
|
| 263 | + public static function alter_can_user_view_lesson ( $can_user_view_lesson, $lesson_id, $user_id ){ |
|
| 264 | + |
|
| 265 | + // do not override access to admins |
|
| 266 | + if( sensei_all_access() || Sensei_Utils::is_preview_lesson( $lesson_id ) ){ |
|
| 267 | + return true; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + // check if the course has a valid product attached to it |
|
| 271 | + // which the user should have purchased if they want to access |
|
| 272 | + // the current lesson |
|
| 273 | + $course_id = get_post_meta( $lesson_id , '_lesson_course', true); |
|
| 274 | + $wc_post_id = get_post_meta( $course_id, '_course_woocommerce_product', true ); |
|
| 275 | + $product = Sensei()->sensei_get_woocommerce_product_object($wc_post_id); |
|
| 276 | + if( isset ($product) && is_object($product) ){ |
|
| 277 | + |
|
| 278 | + // valid product found |
|
| 279 | + $order_id = self::get_learner_course_active_order_id( $user_id, $course_id ); |
|
| 280 | + |
|
| 281 | + // product has a successful order so this user may access the content |
|
| 282 | + // this function may only return false or the default |
|
| 283 | + // returning true may override other negatives which we don't want |
|
| 284 | + if( ! $order_id ){ |
|
| 285 | + |
|
| 286 | + return false; |
|
| 287 | + |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + // return the passed in value |
|
| 293 | + return $can_user_view_lesson; |
|
| 294 | + |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * Add course link to order thank you and details pages. |
|
| 299 | + * |
|
| 300 | + * @since 1.4.5 |
|
| 301 | + * @access public |
|
| 302 | + * |
|
| 303 | + * @return void |
|
| 304 | + */ |
|
| 305 | + public static function course_link_from_order( ) { |
|
| 306 | + |
|
| 307 | + if( ! is_order_received_page() ){ |
|
| 308 | + return; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + $order_id = get_query_var( 'order-received' ); |
|
| 312 | 312 | $order = new WC_Order( $order_id ); |
| 313 | 313 | |
| 314 | 314 | // exit early if not wc-completed or wc-processing |
| 315 | 315 | if( 'wc-completed' != $order->post_status |
| 316 | - && 'wc-processing' != $order->post_status ) { |
|
| 317 | - return; |
|
| 318 | - } |
|
| 316 | + && 'wc-processing' != $order->post_status ) { |
|
| 317 | + return; |
|
| 318 | + } |
|
| 319 | 319 | |
| 320 | - $course_links = array(); // store the for links for courses purchased |
|
| 320 | + $course_links = array(); // store the for links for courses purchased |
|
| 321 | 321 | foreach ( $order->get_items() as $item ) { |
| 322 | 322 | |
| 323 | - if ( isset( $item['variation_id'] ) && ( 0 < $item['variation_id'] ) ) { |
|
| 323 | + if ( isset( $item['variation_id'] ) && ( 0 < $item['variation_id'] ) ) { |
|
| 324 | 324 | |
| 325 | - // If item has variation_id then its a variation of the product |
|
| 326 | - $item_id = $item['variation_id']; |
|
| 325 | + // If item has variation_id then its a variation of the product |
|
| 326 | + $item_id = $item['variation_id']; |
|
| 327 | 327 | |
| 328 | - } else { |
|
| 328 | + } else { |
|
| 329 | 329 | |
| 330 | - //If not its real product set its id to item_id |
|
| 331 | - $item_id = $item['product_id']; |
|
| 330 | + //If not its real product set its id to item_id |
|
| 331 | + $item_id = $item['product_id']; |
|
| 332 | 332 | |
| 333 | - } // End If Statement |
|
| 333 | + } // End If Statement |
|
| 334 | 334 | |
| 335 | - $user_id = get_post_meta( $order->id, '_customer_user', true ); |
|
| 335 | + $user_id = get_post_meta( $order->id, '_customer_user', true ); |
|
| 336 | 336 | |
| 337 | - if( $user_id ) { |
|
| 337 | + if( $user_id ) { |
|
| 338 | 338 | |
| 339 | - // Get all courses for product |
|
| 340 | - $args = Sensei_Course::get_default_query_args(); |
|
| 341 | - $args['meta_query'] = array( array( |
|
| 342 | - 'key' => '_course_woocommerce_product', |
|
| 343 | - 'value' => $item_id |
|
| 344 | - ) ); |
|
| 345 | - $args['orderby'] = 'menu_order date'; |
|
| 346 | - $args['order'] = 'ASC'; |
|
| 339 | + // Get all courses for product |
|
| 340 | + $args = Sensei_Course::get_default_query_args(); |
|
| 341 | + $args['meta_query'] = array( array( |
|
| 342 | + 'key' => '_course_woocommerce_product', |
|
| 343 | + 'value' => $item_id |
|
| 344 | + ) ); |
|
| 345 | + $args['orderby'] = 'menu_order date'; |
|
| 346 | + $args['order'] = 'ASC'; |
|
| 347 | 347 | |
| 348 | - // loop through courses |
|
| 349 | - $courses = get_posts( $args ); |
|
| 350 | - if( $courses && count( $courses ) > 0 ) { |
|
| 348 | + // loop through courses |
|
| 349 | + $courses = get_posts( $args ); |
|
| 350 | + if( $courses && count( $courses ) > 0 ) { |
|
| 351 | 351 | |
| 352 | - foreach( $courses as $course ) { |
|
| 352 | + foreach( $courses as $course ) { |
|
| 353 | 353 | |
| 354 | - $title = $course->post_title; |
|
| 355 | - $permalink = get_permalink( $course->ID ); |
|
| 356 | - $course_links[] .= '<a href="' . esc_url( $permalink ) . '" >' . $title . '</a> '; |
|
| 354 | + $title = $course->post_title; |
|
| 355 | + $permalink = get_permalink( $course->ID ); |
|
| 356 | + $course_links[] .= '<a href="' . esc_url( $permalink ) . '" >' . $title . '</a> '; |
|
| 357 | 357 | |
| 358 | - } // end for each |
|
| 358 | + } // end for each |
|
| 359 | 359 | |
| 360 | - // close the message div |
|
| 360 | + // close the message div |
|
| 361 | 361 | |
| 362 | - }// end if $courses check |
|
| 363 | - } |
|
| 364 | - }// end loop through orders |
|
| 362 | + }// end if $courses check |
|
| 363 | + } |
|
| 364 | + }// end loop through orders |
|
| 365 | 365 | |
| 366 | - // add the courses to the WooCommerce notice |
|
| 367 | - if( ! empty( $course_links) ){ |
|
| 366 | + // add the courses to the WooCommerce notice |
|
| 367 | + if( ! empty( $course_links) ){ |
|
| 368 | 368 | |
| 369 | - $courses_html = _nx( |
|
| 370 | - 'You have purchased the following course:', |
|
| 371 | - 'You have purchased the following courses:', |
|
| 372 | - count( $course_links ), |
|
| 373 | - 'Purchase thank you note on Checkout page. The course link(s) will be show', 'woothemes-sensei' |
|
| 374 | - ); |
|
| 369 | + $courses_html = _nx( |
|
| 370 | + 'You have purchased the following course:', |
|
| 371 | + 'You have purchased the following courses:', |
|
| 372 | + count( $course_links ), |
|
| 373 | + 'Purchase thank you note on Checkout page. The course link(s) will be show', 'woothemes-sensei' |
|
| 374 | + ); |
|
| 375 | 375 | |
| 376 | - foreach( $course_links as $link ){ |
|
| 376 | + foreach( $course_links as $link ){ |
|
| 377 | 377 | |
| 378 | - $courses_html .= '<li>' . $link . '</li>'; |
|
| 378 | + $courses_html .= '<li>' . $link . '</li>'; |
|
| 379 | 379 | |
| 380 | - } |
|
| 380 | + } |
|
| 381 | 381 | |
| 382 | - $courses_html .= ' </ul>'; |
|
| 382 | + $courses_html .= ' </ul>'; |
|
| 383 | 383 | |
| 384 | - wc_add_notice( $courses_html, 'success' ); |
|
| 385 | - } |
|
| 384 | + wc_add_notice( $courses_html, 'success' ); |
|
| 385 | + } |
|
| 386 | 386 | |
| 387 | 387 | } // end course_link_order_form |
| 388 | 388 | |
| 389 | - /** |
|
| 390 | - * Show the message that a user should complete |
|
| 391 | - * their purchase if the course is in the cart |
|
| 392 | - * |
|
| 393 | - * This should be used within the course loop or single course page |
|
| 394 | - * |
|
| 395 | - * @since 1.9.0 |
|
| 396 | - */ |
|
| 397 | - public static function course_in_cart_message(){ |
|
| 389 | + /** |
|
| 390 | + * Show the message that a user should complete |
|
| 391 | + * their purchase if the course is in the cart |
|
| 392 | + * |
|
| 393 | + * This should be used within the course loop or single course page |
|
| 394 | + * |
|
| 395 | + * @since 1.9.0 |
|
| 396 | + */ |
|
| 397 | + public static function course_in_cart_message(){ |
|
| 398 | 398 | |
| 399 | - global $post; |
|
| 399 | + global $post; |
|
| 400 | 400 | |
| 401 | - if( self::is_course_in_cart( $post->ID ) ){ ?> |
|
| 401 | + if( self::is_course_in_cart( $post->ID ) ){ ?> |
|
| 402 | 402 | |
| 403 | 403 | <div class="sensei-message info"> |
| 404 | 404 | <?php |
| 405 | 405 | |
| 406 | - $cart_link = '<a class="cart-complete" href="' . WC()->cart->get_checkout_url() |
|
| 407 | - . '" title="' . __('complete purchase', 'woothemes-sensei') . '">' |
|
| 408 | - . __('complete the purchase', 'woothemes-sensei') . '</a>'; |
|
| 406 | + $cart_link = '<a class="cart-complete" href="' . WC()->cart->get_checkout_url() |
|
| 407 | + . '" title="' . __('complete purchase', 'woothemes-sensei') . '">' |
|
| 408 | + . __('complete the purchase', 'woothemes-sensei') . '</a>'; |
|
| 409 | 409 | |
| 410 | - echo sprintf( __('You have already added this Course to your cart. Please %1$s to access the course.', 'woothemes-sensei'), $cart_link ); |
|
| 410 | + echo sprintf( __('You have already added this Course to your cart. Please %1$s to access the course.', 'woothemes-sensei'), $cart_link ); |
|
| 411 | 411 | |
| 412 | - ?> |
|
| 412 | + ?> |
|
| 413 | 413 | </div> |
| 414 | 414 | <?php } |
| 415 | 415 | |
| 416 | - } // End sensei_woocommerce_in_cart_message() |
|
| 416 | + } // End sensei_woocommerce_in_cart_message() |
|
| 417 | 417 | |
| 418 | - /** |
|
| 419 | - * Checks the cart to see if a course is in the cart. |
|
| 420 | - * |
|
| 421 | - * @param $course_id |
|
| 422 | - * @return bool |
|
| 423 | - */ |
|
| 424 | - public static function is_course_in_cart( $course_id ){ |
|
| 425 | - |
|
| 426 | - $wc_post_id = absint( get_post_meta( $course_id, '_course_woocommerce_product', true ) ); |
|
| 427 | - $user_course_status_id = Sensei_Utils::user_started_course( $course_id , get_current_user_id() ); |
|
| 428 | - |
|
| 429 | - if ( 0 < intval( $wc_post_id ) && ! $user_course_status_id ) { |
|
| 430 | - |
|
| 431 | - if ( self::is_product_in_cart( $wc_post_id ) ) { |
|
| 432 | - |
|
| 433 | - return true; |
|
| 434 | - |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - return false; |
|
| 440 | - |
|
| 441 | - }// is_course_in_cart |
|
| 442 | - |
|
| 443 | - /** |
|
| 444 | - * Check the cart to see if the product is in the cart |
|
| 445 | - * |
|
| 446 | - * @param $product_id |
|
| 447 | - * @return bool |
|
| 448 | - */ |
|
| 449 | - public static function is_product_in_cart( $product_id ){ |
|
| 450 | - |
|
| 451 | - if ( 0 < $product_id ) { |
|
| 452 | - |
|
| 453 | - $product = wc_get_product( $product_id ); |
|
| 454 | - |
|
| 455 | - $parent_id = ''; |
|
| 456 | - if( isset( $product->variation_id ) && 0 < intval( $product->variation_id ) ) { |
|
| 457 | - $wc_product_id = $product->parent->id; |
|
| 458 | - } |
|
| 459 | - foreach( WC()->cart->get_cart() as $cart_item_key => $values ) { |
|
| 460 | - |
|
| 461 | - $cart_product = $values['data']; |
|
| 462 | - if( $product_id == $cart_product->id ) { |
|
| 463 | - |
|
| 464 | - return true; |
|
| 465 | - |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - } |
|
| 469 | - } // End If Statement |
|
| 470 | - |
|
| 471 | - return false; |
|
| 472 | - |
|
| 473 | - } // end is_product_in_car |
|
| 474 | - |
|
| 475 | - /** |
|
| 476 | - * Get all free WooCommerce products |
|
| 477 | - * |
|
| 478 | - * @since 1.9.0 |
|
| 479 | - * |
|
| 480 | - * @return array $free_products{ |
|
| 481 | - * @type int $wp_post_id |
|
| 482 | - * } |
|
| 483 | - */ |
|
| 484 | - public static function get_free_product_ids(){ |
|
| 485 | - |
|
| 486 | - return get_posts( array( |
|
| 487 | - 'post_type' => 'product', |
|
| 488 | - 'posts_per_page' => '1000', |
|
| 489 | - 'fields' => 'ids', |
|
| 490 | - 'meta_query'=> array( |
|
| 491 | - 'relation' => 'OR', |
|
| 492 | - array( |
|
| 493 | - 'key'=> '_regular_price', |
|
| 494 | - 'value' => 0, |
|
| 495 | - ), |
|
| 496 | - array( |
|
| 497 | - 'key'=> '_sale_price', |
|
| 498 | - 'value' => 0, |
|
| 499 | - ), |
|
| 500 | - ), |
|
| 501 | - )); |
|
| 502 | - |
|
| 503 | - }// end get free product query |
|
| 504 | - |
|
| 505 | - /** |
|
| 506 | - * The metat query for courses that are free |
|
| 507 | - * |
|
| 508 | - * @since 1.9.0 |
|
| 509 | - * @return array $wp_meta_query_param |
|
| 510 | - */ |
|
| 511 | - public static function get_free_courses_meta_query_args(){ |
|
| 512 | - |
|
| 513 | - return array( |
|
| 514 | - 'relation' => 'OR', |
|
| 515 | - array( |
|
| 516 | - 'key' => '_course_woocommerce_product', |
|
| 517 | - 'value' => '-', |
|
| 518 | - 'compare' => '=', |
|
| 519 | - ), |
|
| 520 | - array( |
|
| 521 | - 'key' => '_course_woocommerce_product', |
|
| 522 | - 'value' => self::get_free_product_ids(), |
|
| 523 | - 'compare' => 'IN', |
|
| 524 | - ), |
|
| 525 | - ); |
|
| 526 | - |
|
| 527 | - }// get_free_courses_meta_query |
|
| 528 | - |
|
| 529 | - /** |
|
| 530 | - * The metat query for courses that are free |
|
| 531 | - * |
|
| 532 | - * @since 1.9.0 |
|
| 533 | - * @return array $wp_query_meta_query_args_param |
|
| 534 | - */ |
|
| 535 | - public static function get_paid_courses_meta_query_args(){ |
|
| 536 | - |
|
| 537 | - $paid_product_ids = self::get_paid_product_ids(); |
|
| 538 | - |
|
| 539 | - return array( |
|
| 540 | - array( |
|
| 541 | - 'key' => '_course_woocommerce_product', |
|
| 542 | - // when empty we give a false post_id to ensure the caller doesn't get any courses for their |
|
| 543 | - // query |
|
| 544 | - 'value' => empty( $paid_product_ids )? '-1000' : $paid_product_ids, |
|
| 545 | - 'compare' => 'IN', |
|
| 546 | - ), |
|
| 547 | - ); |
|
| 548 | - |
|
| 549 | - }// get_free_courses_meta_query |
|
| 550 | - |
|
| 551 | - /** |
|
| 552 | - * The WordPress Query args |
|
| 553 | - * for paid products on sale |
|
| 554 | - * |
|
| 555 | - * @since 1.9.0 |
|
| 556 | - * @return array $product_query_args |
|
| 557 | - */ |
|
| 558 | - public static function get_paid_products_on_sale_query_args(){ |
|
| 559 | - |
|
| 560 | - $args = array( |
|
| 561 | - 'post_type' => 'product', |
|
| 562 | - 'posts_per_page' => 1000, |
|
| 563 | - 'orderby' => 'date', |
|
| 564 | - 'order' => 'DESC', |
|
| 565 | - 'suppress_filters' => 0 |
|
| 566 | - ); |
|
| 567 | - |
|
| 568 | - $args[ 'fields' ] = 'ids'; |
|
| 569 | - |
|
| 570 | - $args[ 'meta_query' ] = array( |
|
| 571 | - 'relation' => 'AND', |
|
| 572 | - array( |
|
| 573 | - 'key'=> '_regular_price', |
|
| 574 | - 'compare' => '>', |
|
| 575 | - 'value' => 0, |
|
| 576 | - ), |
|
| 577 | - array( |
|
| 578 | - 'key'=> '_sale_price', |
|
| 579 | - 'compare' => '>', |
|
| 580 | - 'value' => 0, |
|
| 581 | - ), |
|
| 582 | - ); |
|
| 583 | - |
|
| 584 | - return $args; |
|
| 585 | - |
|
| 586 | - } // get_paid_products_on_sale_query_args |
|
| 587 | - |
|
| 588 | - |
|
| 589 | - /** |
|
| 590 | - * Return the WordPress query args for |
|
| 591 | - * products not on sale but that is not a free |
|
| 592 | - * |
|
| 593 | - * @since 1.9.0 |
|
| 594 | - * |
|
| 595 | - * @return array |
|
| 596 | - */ |
|
| 597 | - public static function get_paid_products_not_on_sale_query_args(){ |
|
| 598 | - |
|
| 599 | - $args = array( |
|
| 600 | - 'post_type' => 'product', |
|
| 601 | - 'posts_per_page' => 1000, |
|
| 602 | - 'orderby' => 'date', |
|
| 603 | - 'order' => 'DESC', |
|
| 604 | - 'suppress_filters' => 0 |
|
| 605 | - ); |
|
| 606 | - |
|
| 607 | - $args[ 'fields' ] = 'ids'; |
|
| 608 | - $args[ 'meta_query' ] = array( |
|
| 609 | - 'relation' => 'AND', |
|
| 610 | - array( |
|
| 611 | - 'key'=> '_regular_price', |
|
| 612 | - 'compare' => '>', |
|
| 613 | - 'value' => 0, |
|
| 614 | - ), |
|
| 615 | - array( |
|
| 616 | - 'key'=> '_sale_price', |
|
| 617 | - 'compare' => '=', |
|
| 618 | - 'value' => '', |
|
| 619 | - ), |
|
| 620 | - ); |
|
| 621 | - |
|
| 622 | - return $args; |
|
| 623 | - |
|
| 624 | - |
|
| 625 | - } // get_paid_courses_meta_query |
|
| 626 | - |
|
| 627 | - /** |
|
| 628 | - * Get all WooCommerce non-free product id's |
|
| 629 | - * |
|
| 630 | - * @since 1.9.0 |
|
| 631 | - * |
|
| 632 | - * @return array $woocommerce_paid_product_ids |
|
| 633 | - */ |
|
| 634 | - public static function get_paid_product_ids(){ |
|
| 635 | - |
|
| 636 | - // get all the paid WooCommerce products that has regular |
|
| 637 | - // and sale price greater than 0 |
|
| 638 | - // will be used later to check for course with the id as meta |
|
| 639 | - $paid_product_ids_with_sale = get_posts( self::get_paid_products_on_sale_query_args() ); |
|
| 640 | - |
|
| 641 | - // get all the paid WooCommerce products that has regular price |
|
| 642 | - // greater than 0 without a sale price |
|
| 643 | - // will be used later to check for course with the id as meta |
|
| 644 | - $paid_product_ids_without_sale = get_posts( self::get_paid_products_not_on_sale_query_args() ); |
|
| 645 | - |
|
| 646 | - // combine products ID's with regular and sale price grater than zero and those without |
|
| 647 | - // sale but regular price greater than zero |
|
| 648 | - $woocommerce_paid_product_ids = array_merge( $paid_product_ids_with_sale, $paid_product_ids_without_sale ); |
|
| 649 | - |
|
| 650 | - // if |
|
| 651 | - if( empty($woocommerce_paid_product_ids) ){ |
|
| 652 | - return array( ); |
|
| 653 | - } |
|
| 654 | - return $woocommerce_paid_product_ids; |
|
| 655 | - |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - /** |
|
| 659 | - * Get all free courses. |
|
| 660 | - * |
|
| 661 | - * This course that have a WC product attached |
|
| 662 | - * that has a price or sale price of zero and |
|
| 663 | - * other courses with no WooCommerce products |
|
| 664 | - * attached. |
|
| 665 | - * |
|
| 666 | - * @since 1.9.0 |
|
| 667 | - * |
|
| 668 | - * @return array |
|
| 669 | - */ |
|
| 670 | - public static function get_free_courses(){ |
|
| 671 | - |
|
| 672 | - $free_course_query_args = Sensei_Course::get_default_query_args(); |
|
| 673 | - $free_course_query_args[ 'meta_query' ] = self::get_free_courses_meta_query_args(); |
|
| 674 | - |
|
| 675 | - // don't show any paid courses |
|
| 676 | - $courses = self::get_paid_courses(); |
|
| 677 | - $ids = array(); |
|
| 678 | - foreach( $courses as $course ){ |
|
| 679 | - $ids[] = $course->ID; |
|
| 680 | - } |
|
| 681 | - $free_course_query_args[ 'post__not_in' ] = $ids; |
|
| 682 | - |
|
| 683 | - return get_posts( $free_course_query_args ); |
|
| 684 | - |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - /** |
|
| 688 | - * Return all products that are not free |
|
| 689 | - * |
|
| 690 | - * @since 1.9.0 |
|
| 691 | - * @return array |
|
| 692 | - */ |
|
| 693 | - public static function get_paid_courses(){ |
|
| 694 | - |
|
| 695 | - $paid_course_query_args = Sensei_Course::get_default_query_args(); |
|
| 696 | - |
|
| 697 | - $paid_course_query_args[ 'meta_query' ] = self::get_paid_courses_meta_query_args(); |
|
| 698 | - |
|
| 699 | - return get_posts( $paid_course_query_args ); |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - /** |
|
| 703 | - * Show the WooCommerce add to cart button for the current course |
|
| 704 | - * |
|
| 705 | - * The function will only show the button if |
|
| 706 | - * 1- the user can buy the course |
|
| 707 | - * 2- if they have completed their pre-requisite |
|
| 708 | - * 3- if the course has a valid product attached |
|
| 709 | - * |
|
| 710 | - * @since 1.9.0 |
|
| 711 | - * @param int $course_id |
|
| 712 | - * @return string $html markup for the button or nothing if user not allowed to buy |
|
| 713 | - */ |
|
| 714 | - public static function the_add_to_cart_button_html( $course_id ){ |
|
| 715 | - |
|
| 716 | - if ( ! Sensei_Course::is_prerequisite_complete( $course_id )) { |
|
| 717 | - return ''; |
|
| 718 | - } |
|
| 719 | - |
|
| 720 | - $wc_post_id = self::get_course_product_id( $course_id ); |
|
| 721 | - |
|
| 722 | - // Check if customer purchased the product |
|
| 723 | - if ( self::has_customer_bought_product( get_current_user_id(), $wc_post_id ) |
|
| 724 | - || empty( $wc_post_id ) ) { |
|
| 725 | - |
|
| 726 | - return ''; |
|
| 727 | - |
|
| 728 | - } |
|
| 729 | - |
|
| 730 | - // based on simple.php in WC templates/single-product/add-to-cart/ |
|
| 731 | - // Get the product |
|
| 732 | - $product = Sensei()->sensei_get_woocommerce_product_object( $wc_post_id ); |
|
| 733 | - |
|
| 734 | - // do not show the button for invalid products, non purchasable products, out |
|
| 735 | - // of stock product or if course is already in cart |
|
| 736 | - if ( ! isset ( $product ) |
|
| 737 | - || ! is_object( $product ) |
|
| 738 | - || ! $product->is_purchasable() |
|
| 739 | - || ! $product->is_in_stock() |
|
| 740 | - || self::is_course_in_cart( $wc_post_id ) ) { |
|
| 741 | - |
|
| 742 | - return ''; |
|
| 743 | - |
|
| 744 | - } |
|
| 745 | - |
|
| 746 | - // |
|
| 747 | - // button output: |
|
| 748 | - // |
|
| 749 | - ?> |
|
| 418 | + /** |
|
| 419 | + * Checks the cart to see if a course is in the cart. |
|
| 420 | + * |
|
| 421 | + * @param $course_id |
|
| 422 | + * @return bool |
|
| 423 | + */ |
|
| 424 | + public static function is_course_in_cart( $course_id ){ |
|
| 425 | + |
|
| 426 | + $wc_post_id = absint( get_post_meta( $course_id, '_course_woocommerce_product', true ) ); |
|
| 427 | + $user_course_status_id = Sensei_Utils::user_started_course( $course_id , get_current_user_id() ); |
|
| 428 | + |
|
| 429 | + if ( 0 < intval( $wc_post_id ) && ! $user_course_status_id ) { |
|
| 430 | + |
|
| 431 | + if ( self::is_product_in_cart( $wc_post_id ) ) { |
|
| 432 | + |
|
| 433 | + return true; |
|
| 434 | + |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + return false; |
|
| 440 | + |
|
| 441 | + }// is_course_in_cart |
|
| 442 | + |
|
| 443 | + /** |
|
| 444 | + * Check the cart to see if the product is in the cart |
|
| 445 | + * |
|
| 446 | + * @param $product_id |
|
| 447 | + * @return bool |
|
| 448 | + */ |
|
| 449 | + public static function is_product_in_cart( $product_id ){ |
|
| 450 | + |
|
| 451 | + if ( 0 < $product_id ) { |
|
| 452 | + |
|
| 453 | + $product = wc_get_product( $product_id ); |
|
| 454 | + |
|
| 455 | + $parent_id = ''; |
|
| 456 | + if( isset( $product->variation_id ) && 0 < intval( $product->variation_id ) ) { |
|
| 457 | + $wc_product_id = $product->parent->id; |
|
| 458 | + } |
|
| 459 | + foreach( WC()->cart->get_cart() as $cart_item_key => $values ) { |
|
| 460 | + |
|
| 461 | + $cart_product = $values['data']; |
|
| 462 | + if( $product_id == $cart_product->id ) { |
|
| 463 | + |
|
| 464 | + return true; |
|
| 465 | + |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + } |
|
| 469 | + } // End If Statement |
|
| 470 | + |
|
| 471 | + return false; |
|
| 472 | + |
|
| 473 | + } // end is_product_in_car |
|
| 474 | + |
|
| 475 | + /** |
|
| 476 | + * Get all free WooCommerce products |
|
| 477 | + * |
|
| 478 | + * @since 1.9.0 |
|
| 479 | + * |
|
| 480 | + * @return array $free_products{ |
|
| 481 | + * @type int $wp_post_id |
|
| 482 | + * } |
|
| 483 | + */ |
|
| 484 | + public static function get_free_product_ids(){ |
|
| 485 | + |
|
| 486 | + return get_posts( array( |
|
| 487 | + 'post_type' => 'product', |
|
| 488 | + 'posts_per_page' => '1000', |
|
| 489 | + 'fields' => 'ids', |
|
| 490 | + 'meta_query'=> array( |
|
| 491 | + 'relation' => 'OR', |
|
| 492 | + array( |
|
| 493 | + 'key'=> '_regular_price', |
|
| 494 | + 'value' => 0, |
|
| 495 | + ), |
|
| 496 | + array( |
|
| 497 | + 'key'=> '_sale_price', |
|
| 498 | + 'value' => 0, |
|
| 499 | + ), |
|
| 500 | + ), |
|
| 501 | + )); |
|
| 502 | + |
|
| 503 | + }// end get free product query |
|
| 504 | + |
|
| 505 | + /** |
|
| 506 | + * The metat query for courses that are free |
|
| 507 | + * |
|
| 508 | + * @since 1.9.0 |
|
| 509 | + * @return array $wp_meta_query_param |
|
| 510 | + */ |
|
| 511 | + public static function get_free_courses_meta_query_args(){ |
|
| 512 | + |
|
| 513 | + return array( |
|
| 514 | + 'relation' => 'OR', |
|
| 515 | + array( |
|
| 516 | + 'key' => '_course_woocommerce_product', |
|
| 517 | + 'value' => '-', |
|
| 518 | + 'compare' => '=', |
|
| 519 | + ), |
|
| 520 | + array( |
|
| 521 | + 'key' => '_course_woocommerce_product', |
|
| 522 | + 'value' => self::get_free_product_ids(), |
|
| 523 | + 'compare' => 'IN', |
|
| 524 | + ), |
|
| 525 | + ); |
|
| 526 | + |
|
| 527 | + }// get_free_courses_meta_query |
|
| 528 | + |
|
| 529 | + /** |
|
| 530 | + * The metat query for courses that are free |
|
| 531 | + * |
|
| 532 | + * @since 1.9.0 |
|
| 533 | + * @return array $wp_query_meta_query_args_param |
|
| 534 | + */ |
|
| 535 | + public static function get_paid_courses_meta_query_args(){ |
|
| 536 | + |
|
| 537 | + $paid_product_ids = self::get_paid_product_ids(); |
|
| 538 | + |
|
| 539 | + return array( |
|
| 540 | + array( |
|
| 541 | + 'key' => '_course_woocommerce_product', |
|
| 542 | + // when empty we give a false post_id to ensure the caller doesn't get any courses for their |
|
| 543 | + // query |
|
| 544 | + 'value' => empty( $paid_product_ids )? '-1000' : $paid_product_ids, |
|
| 545 | + 'compare' => 'IN', |
|
| 546 | + ), |
|
| 547 | + ); |
|
| 548 | + |
|
| 549 | + }// get_free_courses_meta_query |
|
| 550 | + |
|
| 551 | + /** |
|
| 552 | + * The WordPress Query args |
|
| 553 | + * for paid products on sale |
|
| 554 | + * |
|
| 555 | + * @since 1.9.0 |
|
| 556 | + * @return array $product_query_args |
|
| 557 | + */ |
|
| 558 | + public static function get_paid_products_on_sale_query_args(){ |
|
| 559 | + |
|
| 560 | + $args = array( |
|
| 561 | + 'post_type' => 'product', |
|
| 562 | + 'posts_per_page' => 1000, |
|
| 563 | + 'orderby' => 'date', |
|
| 564 | + 'order' => 'DESC', |
|
| 565 | + 'suppress_filters' => 0 |
|
| 566 | + ); |
|
| 567 | + |
|
| 568 | + $args[ 'fields' ] = 'ids'; |
|
| 569 | + |
|
| 570 | + $args[ 'meta_query' ] = array( |
|
| 571 | + 'relation' => 'AND', |
|
| 572 | + array( |
|
| 573 | + 'key'=> '_regular_price', |
|
| 574 | + 'compare' => '>', |
|
| 575 | + 'value' => 0, |
|
| 576 | + ), |
|
| 577 | + array( |
|
| 578 | + 'key'=> '_sale_price', |
|
| 579 | + 'compare' => '>', |
|
| 580 | + 'value' => 0, |
|
| 581 | + ), |
|
| 582 | + ); |
|
| 583 | + |
|
| 584 | + return $args; |
|
| 585 | + |
|
| 586 | + } // get_paid_products_on_sale_query_args |
|
| 587 | + |
|
| 588 | + |
|
| 589 | + /** |
|
| 590 | + * Return the WordPress query args for |
|
| 591 | + * products not on sale but that is not a free |
|
| 592 | + * |
|
| 593 | + * @since 1.9.0 |
|
| 594 | + * |
|
| 595 | + * @return array |
|
| 596 | + */ |
|
| 597 | + public static function get_paid_products_not_on_sale_query_args(){ |
|
| 598 | + |
|
| 599 | + $args = array( |
|
| 600 | + 'post_type' => 'product', |
|
| 601 | + 'posts_per_page' => 1000, |
|
| 602 | + 'orderby' => 'date', |
|
| 603 | + 'order' => 'DESC', |
|
| 604 | + 'suppress_filters' => 0 |
|
| 605 | + ); |
|
| 606 | + |
|
| 607 | + $args[ 'fields' ] = 'ids'; |
|
| 608 | + $args[ 'meta_query' ] = array( |
|
| 609 | + 'relation' => 'AND', |
|
| 610 | + array( |
|
| 611 | + 'key'=> '_regular_price', |
|
| 612 | + 'compare' => '>', |
|
| 613 | + 'value' => 0, |
|
| 614 | + ), |
|
| 615 | + array( |
|
| 616 | + 'key'=> '_sale_price', |
|
| 617 | + 'compare' => '=', |
|
| 618 | + 'value' => '', |
|
| 619 | + ), |
|
| 620 | + ); |
|
| 621 | + |
|
| 622 | + return $args; |
|
| 623 | + |
|
| 624 | + |
|
| 625 | + } // get_paid_courses_meta_query |
|
| 626 | + |
|
| 627 | + /** |
|
| 628 | + * Get all WooCommerce non-free product id's |
|
| 629 | + * |
|
| 630 | + * @since 1.9.0 |
|
| 631 | + * |
|
| 632 | + * @return array $woocommerce_paid_product_ids |
|
| 633 | + */ |
|
| 634 | + public static function get_paid_product_ids(){ |
|
| 635 | + |
|
| 636 | + // get all the paid WooCommerce products that has regular |
|
| 637 | + // and sale price greater than 0 |
|
| 638 | + // will be used later to check for course with the id as meta |
|
| 639 | + $paid_product_ids_with_sale = get_posts( self::get_paid_products_on_sale_query_args() ); |
|
| 640 | + |
|
| 641 | + // get all the paid WooCommerce products that has regular price |
|
| 642 | + // greater than 0 without a sale price |
|
| 643 | + // will be used later to check for course with the id as meta |
|
| 644 | + $paid_product_ids_without_sale = get_posts( self::get_paid_products_not_on_sale_query_args() ); |
|
| 645 | + |
|
| 646 | + // combine products ID's with regular and sale price grater than zero and those without |
|
| 647 | + // sale but regular price greater than zero |
|
| 648 | + $woocommerce_paid_product_ids = array_merge( $paid_product_ids_with_sale, $paid_product_ids_without_sale ); |
|
| 649 | + |
|
| 650 | + // if |
|
| 651 | + if( empty($woocommerce_paid_product_ids) ){ |
|
| 652 | + return array( ); |
|
| 653 | + } |
|
| 654 | + return $woocommerce_paid_product_ids; |
|
| 655 | + |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + /** |
|
| 659 | + * Get all free courses. |
|
| 660 | + * |
|
| 661 | + * This course that have a WC product attached |
|
| 662 | + * that has a price or sale price of zero and |
|
| 663 | + * other courses with no WooCommerce products |
|
| 664 | + * attached. |
|
| 665 | + * |
|
| 666 | + * @since 1.9.0 |
|
| 667 | + * |
|
| 668 | + * @return array |
|
| 669 | + */ |
|
| 670 | + public static function get_free_courses(){ |
|
| 671 | + |
|
| 672 | + $free_course_query_args = Sensei_Course::get_default_query_args(); |
|
| 673 | + $free_course_query_args[ 'meta_query' ] = self::get_free_courses_meta_query_args(); |
|
| 674 | + |
|
| 675 | + // don't show any paid courses |
|
| 676 | + $courses = self::get_paid_courses(); |
|
| 677 | + $ids = array(); |
|
| 678 | + foreach( $courses as $course ){ |
|
| 679 | + $ids[] = $course->ID; |
|
| 680 | + } |
|
| 681 | + $free_course_query_args[ 'post__not_in' ] = $ids; |
|
| 682 | + |
|
| 683 | + return get_posts( $free_course_query_args ); |
|
| 684 | + |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + /** |
|
| 688 | + * Return all products that are not free |
|
| 689 | + * |
|
| 690 | + * @since 1.9.0 |
|
| 691 | + * @return array |
|
| 692 | + */ |
|
| 693 | + public static function get_paid_courses(){ |
|
| 694 | + |
|
| 695 | + $paid_course_query_args = Sensei_Course::get_default_query_args(); |
|
| 696 | + |
|
| 697 | + $paid_course_query_args[ 'meta_query' ] = self::get_paid_courses_meta_query_args(); |
|
| 698 | + |
|
| 699 | + return get_posts( $paid_course_query_args ); |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + /** |
|
| 703 | + * Show the WooCommerce add to cart button for the current course |
|
| 704 | + * |
|
| 705 | + * The function will only show the button if |
|
| 706 | + * 1- the user can buy the course |
|
| 707 | + * 2- if they have completed their pre-requisite |
|
| 708 | + * 3- if the course has a valid product attached |
|
| 709 | + * |
|
| 710 | + * @since 1.9.0 |
|
| 711 | + * @param int $course_id |
|
| 712 | + * @return string $html markup for the button or nothing if user not allowed to buy |
|
| 713 | + */ |
|
| 714 | + public static function the_add_to_cart_button_html( $course_id ){ |
|
| 715 | + |
|
| 716 | + if ( ! Sensei_Course::is_prerequisite_complete( $course_id )) { |
|
| 717 | + return ''; |
|
| 718 | + } |
|
| 719 | + |
|
| 720 | + $wc_post_id = self::get_course_product_id( $course_id ); |
|
| 721 | + |
|
| 722 | + // Check if customer purchased the product |
|
| 723 | + if ( self::has_customer_bought_product( get_current_user_id(), $wc_post_id ) |
|
| 724 | + || empty( $wc_post_id ) ) { |
|
| 725 | + |
|
| 726 | + return ''; |
|
| 727 | + |
|
| 728 | + } |
|
| 729 | + |
|
| 730 | + // based on simple.php in WC templates/single-product/add-to-cart/ |
|
| 731 | + // Get the product |
|
| 732 | + $product = Sensei()->sensei_get_woocommerce_product_object( $wc_post_id ); |
|
| 733 | + |
|
| 734 | + // do not show the button for invalid products, non purchasable products, out |
|
| 735 | + // of stock product or if course is already in cart |
|
| 736 | + if ( ! isset ( $product ) |
|
| 737 | + || ! is_object( $product ) |
|
| 738 | + || ! $product->is_purchasable() |
|
| 739 | + || ! $product->is_in_stock() |
|
| 740 | + || self::is_course_in_cart( $wc_post_id ) ) { |
|
| 741 | + |
|
| 742 | + return ''; |
|
| 743 | + |
|
| 744 | + } |
|
| 745 | + |
|
| 746 | + // |
|
| 747 | + // button output: |
|
| 748 | + // |
|
| 749 | + ?> |
|
| 750 | 750 | |
| 751 | 751 | <form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" |
| 752 | 752 | class="cart" |
@@ -775,68 +775,68 @@ discard block |
||
| 775 | 775 | <button type="submit" class="single_add_to_cart_button button alt"> |
| 776 | 776 | <?php $button_text = $product->get_price_html() . ' - ' . __( 'Purchase this Course', 'woothemes-sensei' ); ?> |
| 777 | 777 | <?php |
| 778 | - /** |
|
| 779 | - * Filter Add to Cart button text |
|
| 780 | - * |
|
| 781 | - * @since 1.9.1 |
|
| 782 | - * |
|
| 783 | - * @param string $button_text |
|
| 784 | - */ |
|
| 785 | - echo apply_filters( 'sensei_wc_single_add_to_cart_button_text', $button_text ); |
|
| 786 | - ?> |
|
| 778 | + /** |
|
| 779 | + * Filter Add to Cart button text |
|
| 780 | + * |
|
| 781 | + * @since 1.9.1 |
|
| 782 | + * |
|
| 783 | + * @param string $button_text |
|
| 784 | + */ |
|
| 785 | + echo apply_filters( 'sensei_wc_single_add_to_cart_button_text', $button_text ); |
|
| 786 | + ?> |
|
| 787 | 787 | </button> |
| 788 | 788 | |
| 789 | 789 | </form> |
| 790 | 790 | |
| 791 | 791 | <?php |
| 792 | - } // end the_add_to_cart_button_html |
|
| 792 | + } // end the_add_to_cart_button_html |
|
| 793 | 793 | |
| 794 | - /** |
|
| 795 | - * Alter the no permissions message on the single course page |
|
| 796 | - * Changes the message to a WooCommerce specific message. |
|
| 797 | - * |
|
| 798 | - * @since 1.9.0 |
|
| 799 | - * |
|
| 800 | - * @param $message |
|
| 801 | - * @param $post_id |
|
| 802 | - * |
|
| 803 | - * @return string $message |
|
| 804 | - */ |
|
| 805 | - public static function alter_no_permissions_message( $message, $post_id ){ |
|
| 794 | + /** |
|
| 795 | + * Alter the no permissions message on the single course page |
|
| 796 | + * Changes the message to a WooCommerce specific message. |
|
| 797 | + * |
|
| 798 | + * @since 1.9.0 |
|
| 799 | + * |
|
| 800 | + * @param $message |
|
| 801 | + * @param $post_id |
|
| 802 | + * |
|
| 803 | + * @return string $message |
|
| 804 | + */ |
|
| 805 | + public static function alter_no_permissions_message( $message, $post_id ){ |
|
| 806 | 806 | |
| 807 | - if( empty( $post_id ) || 'course'!=get_post_type( $post_id ) ){ |
|
| 808 | - return $message; |
|
| 809 | - } |
|
| 807 | + if( empty( $post_id ) || 'course'!=get_post_type( $post_id ) ){ |
|
| 808 | + return $message; |
|
| 809 | + } |
|
| 810 | 810 | |
| 811 | - $product_id = self::get_course_product_id( $post_id ); |
|
| 811 | + $product_id = self::get_course_product_id( $post_id ); |
|
| 812 | 812 | |
| 813 | - if( ! $product_id |
|
| 814 | - || self::has_customer_bought_product( get_current_user_id(),$product_id ) ){ |
|
| 813 | + if( ! $product_id |
|
| 814 | + || self::has_customer_bought_product( get_current_user_id(),$product_id ) ){ |
|
| 815 | 815 | |
| 816 | - return $message; |
|
| 816 | + return $message; |
|
| 817 | 817 | |
| 818 | - } |
|
| 818 | + } |
|
| 819 | 819 | |
| 820 | - ob_start(); |
|
| 821 | - self::the_course_no_permissions_message( $post_id ); |
|
| 822 | - $woocommerce_course_no_permissions_message = ob_get_clean(); |
|
| 820 | + ob_start(); |
|
| 821 | + self::the_course_no_permissions_message( $post_id ); |
|
| 822 | + $woocommerce_course_no_permissions_message = ob_get_clean(); |
|
| 823 | 823 | |
| 824 | - return $woocommerce_course_no_permissions_message ; |
|
| 824 | + return $woocommerce_course_no_permissions_message ; |
|
| 825 | 825 | |
| 826 | - } |
|
| 827 | - /** |
|
| 828 | - * Show the no permissions message when a user is logged in |
|
| 829 | - * and have not yet purchased the current course |
|
| 830 | - * |
|
| 831 | - * @since 1.9.0 |
|
| 832 | - */ |
|
| 833 | - public static function the_course_no_permissions_message( $course_id ){ |
|
| 826 | + } |
|
| 827 | + /** |
|
| 828 | + * Show the no permissions message when a user is logged in |
|
| 829 | + * and have not yet purchased the current course |
|
| 830 | + * |
|
| 831 | + * @since 1.9.0 |
|
| 832 | + */ |
|
| 833 | + public static function the_course_no_permissions_message( $course_id ){ |
|
| 834 | 834 | |
| 835 | - // login link |
|
| 836 | - $my_courses_page_id = intval( Sensei()->settings->settings[ 'my_course_page' ] ); |
|
| 837 | - $login_link = '<a href="' . esc_url( get_permalink( $my_courses_page_id ) ) . '">' . __( 'log in', 'woothemes-sensei' ) . '</a>'; |
|
| 835 | + // login link |
|
| 836 | + $my_courses_page_id = intval( Sensei()->settings->settings[ 'my_course_page' ] ); |
|
| 837 | + $login_link = '<a href="' . esc_url( get_permalink( $my_courses_page_id ) ) . '">' . __( 'log in', 'woothemes-sensei' ) . '</a>'; |
|
| 838 | 838 | |
| 839 | - ?> |
|
| 839 | + ?> |
|
| 840 | 840 | |
| 841 | 841 | <span class="add-to-cart-login"> |
| 842 | 842 | <?php echo sprintf( __( 'Or %1$s to access your purchased courses', 'woothemes-sensei' ), $login_link ); ?> |
@@ -844,765 +844,765 @@ discard block |
||
| 844 | 844 | |
| 845 | 845 | <?php } |
| 846 | 846 | |
| 847 | - /** |
|
| 848 | - * Checks if a user has bought a product item. |
|
| 849 | - * |
|
| 850 | - * @since 1.9.0 |
|
| 851 | - * |
|
| 852 | - * @param int $user_id |
|
| 853 | - * @param int $product_id |
|
| 854 | - * |
|
| 855 | - * @return bool |
|
| 856 | - */ |
|
| 857 | - public static function has_customer_bought_product ( $user_id, $product_id ){ |
|
| 847 | + /** |
|
| 848 | + * Checks if a user has bought a product item. |
|
| 849 | + * |
|
| 850 | + * @since 1.9.0 |
|
| 851 | + * |
|
| 852 | + * @param int $user_id |
|
| 853 | + * @param int $product_id |
|
| 854 | + * |
|
| 855 | + * @return bool |
|
| 856 | + */ |
|
| 857 | + public static function has_customer_bought_product ( $user_id, $product_id ){ |
|
| 858 | 858 | |
| 859 | - $orders = get_posts( array( |
|
| 860 | - 'numberposts' => -1, |
|
| 861 | - 'post_type' => 'shope_order', |
|
| 862 | - 'meta_key' => '_customer_user', |
|
| 863 | - 'meta_value' => intval( $user_id ), |
|
| 864 | - 'post_status' => array( 'wc-complete','wc-processing' ), |
|
| 865 | - ) ); |
|
| 866 | - |
|
| 867 | - foreach ( $orders as $order_id ) { |
|
| 859 | + $orders = get_posts( array( |
|
| 860 | + 'numberposts' => -1, |
|
| 861 | + 'post_type' => 'shope_order', |
|
| 862 | + 'meta_key' => '_customer_user', |
|
| 863 | + 'meta_value' => intval( $user_id ), |
|
| 864 | + 'post_status' => array( 'wc-complete','wc-processing' ), |
|
| 865 | + ) ); |
|
| 866 | + |
|
| 867 | + foreach ( $orders as $order_id ) { |
|
| 868 | 868 | |
| 869 | - $order = new WC_Order( $order_id->ID ); |
|
| 870 | - |
|
| 871 | - // wc-active is the subscriptions complete status |
|
| 872 | - if ( ! in_array( $order->post_status, array( 'wc-complete','wc-processing' ) ) ){ |
|
| 873 | - |
|
| 874 | - continue; |
|
| 875 | - |
|
| 876 | - } |
|
| 877 | - |
|
| 878 | - if ( ! ( 0 < sizeof( $order->get_items() ) ) ) { |
|
| 869 | + $order = new WC_Order( $order_id->ID ); |
|
| 870 | + |
|
| 871 | + // wc-active is the subscriptions complete status |
|
| 872 | + if ( ! in_array( $order->post_status, array( 'wc-complete','wc-processing' ) ) ){ |
|
| 873 | + |
|
| 874 | + continue; |
|
| 875 | + |
|
| 876 | + } |
|
| 877 | + |
|
| 878 | + if ( ! ( 0 < sizeof( $order->get_items() ) ) ) { |
|
| 879 | 879 | |
| 880 | - continue; |
|
| 880 | + continue; |
|
| 881 | 881 | |
| 882 | - } |
|
| 882 | + } |
|
| 883 | 883 | |
| 884 | - foreach( $order->get_items() as $item ) { |
|
| 884 | + foreach( $order->get_items() as $item ) { |
|
| 885 | 885 | |
| 886 | - // Check if user has bought product |
|
| 887 | - if ( $item['product_id'] == $product_id || $item['variation_id'] == $product_id ) { |
|
| 886 | + // Check if user has bought product |
|
| 887 | + if ( $item['product_id'] == $product_id || $item['variation_id'] == $product_id ) { |
|
| 888 | 888 | |
| 889 | - // Check if user has an active subscription for product |
|
| 890 | - if( class_exists( 'WC_Subscriptions_Manager' ) ) { |
|
| 891 | - $sub_key = wcs_get_subscription( $order ); |
|
| 892 | - if( $sub_key ) { |
|
| 893 | - $sub = wcs_get_subscription( $sub_key ); |
|
| 894 | - if( $sub && isset( $sub['status'] ) ) { |
|
| 895 | - if( 'active' == $sub['status'] ) { |
|
| 896 | - return true; |
|
| 897 | - } else { |
|
| 898 | - return false; |
|
| 899 | - } |
|
| 900 | - } |
|
| 901 | - } |
|
| 902 | - } |
|
| 889 | + // Check if user has an active subscription for product |
|
| 890 | + if( class_exists( 'WC_Subscriptions_Manager' ) ) { |
|
| 891 | + $sub_key = wcs_get_subscription( $order ); |
|
| 892 | + if( $sub_key ) { |
|
| 893 | + $sub = wcs_get_subscription( $sub_key ); |
|
| 894 | + if( $sub && isset( $sub['status'] ) ) { |
|
| 895 | + if( 'active' == $sub['status'] ) { |
|
| 896 | + return true; |
|
| 897 | + } else { |
|
| 898 | + return false; |
|
| 899 | + } |
|
| 900 | + } |
|
| 901 | + } |
|
| 902 | + } |
|
| 903 | 903 | |
| 904 | - // Customer has bought product |
|
| 905 | - return true; |
|
| 906 | - } // End If Statement |
|
| 904 | + // Customer has bought product |
|
| 905 | + return true; |
|
| 906 | + } // End If Statement |
|
| 907 | 907 | |
| 908 | - } // End For each item |
|
| 908 | + } // End For each item |
|
| 909 | 909 | |
| 910 | - } // End For each order |
|
| 910 | + } // End For each order |
|
| 911 | 911 | |
| 912 | - } // end has customer bought product |
|
| 912 | + } // end has customer bought product |
|
| 913 | 913 | |
| 914 | - /** |
|
| 915 | - * Return the product id for the given course |
|
| 916 | - * |
|
| 917 | - * @since 1.9.0 |
|
| 918 | - * |
|
| 919 | - * @param int $course_id |
|
| 920 | - * |
|
| 921 | - * @return string $woocommerce_product_id or false if none exist |
|
| 922 | - * |
|
| 923 | - */ |
|
| 924 | - public static function get_course_product_id( $course_id ){ |
|
| 914 | + /** |
|
| 915 | + * Return the product id for the given course |
|
| 916 | + * |
|
| 917 | + * @since 1.9.0 |
|
| 918 | + * |
|
| 919 | + * @param int $course_id |
|
| 920 | + * |
|
| 921 | + * @return string $woocommerce_product_id or false if none exist |
|
| 922 | + * |
|
| 923 | + */ |
|
| 924 | + public static function get_course_product_id( $course_id ){ |
|
| 925 | 925 | |
| 926 | - $product_id = get_post_meta( $course_id, '_course_woocommerce_product', true ); |
|
| 926 | + $product_id = get_post_meta( $course_id, '_course_woocommerce_product', true ); |
|
| 927 | 927 | |
| 928 | - if( empty( $product_id ) || 'product' != get_post_type( $product_id ) ){ |
|
| 929 | - return false; |
|
| 930 | - } |
|
| 928 | + if( empty( $product_id ) || 'product' != get_post_type( $product_id ) ){ |
|
| 929 | + return false; |
|
| 930 | + } |
|
| 931 | 931 | |
| 932 | - return $product_id; |
|
| 932 | + return $product_id; |
|
| 933 | 933 | |
| 934 | - } |
|
| 934 | + } |
|
| 935 | 935 | |
| 936 | - /** |
|
| 937 | - * Alter the body classes adding WooCommerce to the body |
|
| 938 | - * |
|
| 939 | - * Speciall cases where this is needed is template no-permissions.php |
|
| 940 | - * |
|
| 941 | - * @param array $classes |
|
| 942 | - * @return array |
|
| 943 | - */ |
|
| 944 | - public static function add_woocommerce_body_class( $classes ){ |
|
| 936 | + /** |
|
| 937 | + * Alter the body classes adding WooCommerce to the body |
|
| 938 | + * |
|
| 939 | + * Speciall cases where this is needed is template no-permissions.php |
|
| 940 | + * |
|
| 941 | + * @param array $classes |
|
| 942 | + * @return array |
|
| 943 | + */ |
|
| 944 | + public static function add_woocommerce_body_class( $classes ){ |
|
| 945 | 945 | |
| 946 | - if( ! in_array( 'woocommerce', $classes ) && defined( 'SENSEI_NO_PERMISSION' ) && SENSEI_NO_PERMISSION ){ |
|
| 946 | + if( ! in_array( 'woocommerce', $classes ) && defined( 'SENSEI_NO_PERMISSION' ) && SENSEI_NO_PERMISSION ){ |
|
| 947 | 947 | |
| 948 | - $classes[] ='woocommerce'; |
|
| 948 | + $classes[] ='woocommerce'; |
|
| 949 | 949 | |
| 950 | - } |
|
| 950 | + } |
|
| 951 | 951 | |
| 952 | - return $classes; |
|
| 952 | + return $classes; |
|
| 953 | 953 | |
| 954 | - } |
|
| 954 | + } |
|
| 955 | 955 | |
| 956 | - /** |
|
| 957 | - * Responds to when a subscription product is purchased |
|
| 958 | - * |
|
| 959 | - * @since 1.2.0 |
|
| 960 | - * @since 1.9.0 move to class Sensei_WC |
|
| 961 | - * |
|
| 962 | - * @param WC_Order $order |
|
| 963 | - * |
|
| 964 | - * @return void |
|
| 965 | - */ |
|
| 966 | - public static function activate_subscription( $order ) { |
|
| 956 | + /** |
|
| 957 | + * Responds to when a subscription product is purchased |
|
| 958 | + * |
|
| 959 | + * @since 1.2.0 |
|
| 960 | + * @since 1.9.0 move to class Sensei_WC |
|
| 961 | + * |
|
| 962 | + * @param WC_Order $order |
|
| 963 | + * |
|
| 964 | + * @return void |
|
| 965 | + */ |
|
| 966 | + public static function activate_subscription( $order ) { |
|
| 967 | 967 | |
| 968 | - $order_user = get_user_by('id', $order->user_id); |
|
| 969 | - $user['ID'] = $order_user->ID; |
|
| 970 | - $user['user_login'] = $order_user->user_login; |
|
| 971 | - $user['user_email'] = $order_user->user_email; |
|
| 972 | - $user['user_url'] = $order_user->user_url; |
|
| 968 | + $order_user = get_user_by('id', $order->user_id); |
|
| 969 | + $user['ID'] = $order_user->ID; |
|
| 970 | + $user['user_login'] = $order_user->user_login; |
|
| 971 | + $user['user_email'] = $order_user->user_email; |
|
| 972 | + $user['user_url'] = $order_user->user_url; |
|
| 973 | 973 | |
| 974 | - // Run through each product ordered |
|
| 975 | - if ( ! sizeof($order->get_items() )>0 ) { |
|
| 974 | + // Run through each product ordered |
|
| 975 | + if ( ! sizeof($order->get_items() )>0 ) { |
|
| 976 | 976 | |
| 977 | - return; |
|
| 977 | + return; |
|
| 978 | 978 | |
| 979 | - } |
|
| 979 | + } |
|
| 980 | 980 | |
| 981 | - foreach($order->get_items() as $item) { |
|
| 981 | + foreach($order->get_items() as $item) { |
|
| 982 | 982 | |
| 983 | - $product_type = ''; |
|
| 983 | + $product_type = ''; |
|
| 984 | 984 | |
| 985 | - if (isset($item['variation_id']) && $item['variation_id'] > 0) { |
|
| 985 | + if (isset($item['variation_id']) && $item['variation_id'] > 0) { |
|
| 986 | 986 | |
| 987 | - $item_id = $item['variation_id']; |
|
| 988 | - $product_type = 'subscription_variation'; |
|
| 987 | + $item_id = $item['variation_id']; |
|
| 988 | + $product_type = 'subscription_variation'; |
|
| 989 | 989 | |
| 990 | - } else { |
|
| 990 | + } else { |
|
| 991 | 991 | |
| 992 | - $item_id = $item['product_id']; |
|
| 992 | + $item_id = $item['product_id']; |
|
| 993 | 993 | |
| 994 | - } // End If Statement |
|
| 994 | + } // End If Statement |
|
| 995 | 995 | |
| 996 | - $_product = self::get_product_object( $item_id, $product_type ); |
|
| 996 | + $_product = self::get_product_object( $item_id, $product_type ); |
|
| 997 | 997 | |
| 998 | - // Get courses that use the WC product |
|
| 999 | - $courses = array(); |
|
| 998 | + // Get courses that use the WC product |
|
| 999 | + $courses = array(); |
|
| 1000 | 1000 | |
| 1001 | - if ( ! in_array( $product_type, self::get_subscription_types() ) ) { |
|
| 1001 | + if ( ! in_array( $product_type, self::get_subscription_types() ) ) { |
|
| 1002 | 1002 | |
| 1003 | - $courses = Sensei()->course->get_product_courses( $item_id ); |
|
| 1003 | + $courses = Sensei()->course->get_product_courses( $item_id ); |
|
| 1004 | 1004 | |
| 1005 | - } // End If Statement |
|
| 1005 | + } // End If Statement |
|
| 1006 | 1006 | |
| 1007 | - // Loop and add the user to the course. |
|
| 1008 | - foreach ( $courses as $course_item ){ |
|
| 1007 | + // Loop and add the user to the course. |
|
| 1008 | + foreach ( $courses as $course_item ){ |
|
| 1009 | 1009 | |
| 1010 | - Sensei_Utils::user_start_course( intval( $user['ID'] ), $course_item->ID ); |
|
| 1010 | + Sensei_Utils::user_start_course( intval( $user['ID'] ), $course_item->ID ); |
|
| 1011 | 1011 | |
| 1012 | - } // End For Loop |
|
| 1012 | + } // End For Loop |
|
| 1013 | 1013 | |
| 1014 | - } // End For Loop |
|
| 1014 | + } // End For Loop |
|
| 1015 | 1015 | |
| 1016 | - } // End activate_subscription() |
|
| 1016 | + } // End activate_subscription() |
|
| 1017 | 1017 | |
| 1018 | - /** |
|
| 1019 | - * Adds detail to to the WooCommerce order |
|
| 1020 | - * |
|
| 1021 | - * @since 1.4.5 |
|
| 1022 | - * @since 1.9.0 function moved to class Sensei_WC and renamed from sensei_woocommerce_email_course_details to email_course_details |
|
| 1023 | - * |
|
| 1024 | - * @param WC_Order $order |
|
| 1025 | - * |
|
| 1026 | - * @return void |
|
| 1027 | - */ |
|
| 1028 | - public static function email_course_details( $order ){ |
|
| 1018 | + /** |
|
| 1019 | + * Adds detail to to the WooCommerce order |
|
| 1020 | + * |
|
| 1021 | + * @since 1.4.5 |
|
| 1022 | + * @since 1.9.0 function moved to class Sensei_WC and renamed from sensei_woocommerce_email_course_details to email_course_details |
|
| 1023 | + * |
|
| 1024 | + * @param WC_Order $order |
|
| 1025 | + * |
|
| 1026 | + * @return void |
|
| 1027 | + */ |
|
| 1028 | + public static function email_course_details( $order ){ |
|
| 1029 | 1029 | |
| 1030 | - global $woocommerce; |
|
| 1030 | + global $woocommerce; |
|
| 1031 | 1031 | |
| 1032 | - // exit early if not wc-completed or wc-processing |
|
| 1033 | - if( 'wc-completed' != $order->post_status |
|
| 1034 | - && 'wc-processing' != $order->post_status ) { |
|
| 1035 | - return; |
|
| 1036 | - } |
|
| 1032 | + // exit early if not wc-completed or wc-processing |
|
| 1033 | + if( 'wc-completed' != $order->post_status |
|
| 1034 | + && 'wc-processing' != $order->post_status ) { |
|
| 1035 | + return; |
|
| 1036 | + } |
|
| 1037 | 1037 | |
| 1038 | - $order_items = $order->get_items(); |
|
| 1039 | - $order_id = $order->id; |
|
| 1038 | + $order_items = $order->get_items(); |
|
| 1039 | + $order_id = $order->id; |
|
| 1040 | 1040 | |
| 1041 | - //If object have items go through them all to find course |
|
| 1042 | - if ( 0 < sizeof( $order_items ) ) { |
|
| 1041 | + //If object have items go through them all to find course |
|
| 1042 | + if ( 0 < sizeof( $order_items ) ) { |
|
| 1043 | 1043 | |
| 1044 | - $course_details_html = '<h2>' . __( 'Course details', 'woothemes-sensei' ) . '</h2>'; |
|
| 1045 | - $order_contains_courses = false; |
|
| 1044 | + $course_details_html = '<h2>' . __( 'Course details', 'woothemes-sensei' ) . '</h2>'; |
|
| 1045 | + $order_contains_courses = false; |
|
| 1046 | 1046 | |
| 1047 | 1047 | |
| 1048 | - foreach ( $order_items as $item ) { |
|
| 1048 | + foreach ( $order_items as $item ) { |
|
| 1049 | 1049 | |
| 1050 | - $product_type = ''; |
|
| 1051 | - if ( isset( $item['variation_id'] ) && ( 0 < $item['variation_id'] ) ) { |
|
| 1052 | - // If item has variation_id then its from variation |
|
| 1053 | - $item_id = $item['variation_id']; |
|
| 1054 | - $product_type = 'variation'; |
|
| 1055 | - } else { |
|
| 1056 | - // If not its real product set its id to item_id |
|
| 1057 | - $item_id = $item['product_id']; |
|
| 1058 | - } // End If Statement |
|
| 1050 | + $product_type = ''; |
|
| 1051 | + if ( isset( $item['variation_id'] ) && ( 0 < $item['variation_id'] ) ) { |
|
| 1052 | + // If item has variation_id then its from variation |
|
| 1053 | + $item_id = $item['variation_id']; |
|
| 1054 | + $product_type = 'variation'; |
|
| 1055 | + } else { |
|
| 1056 | + // If not its real product set its id to item_id |
|
| 1057 | + $item_id = $item['product_id']; |
|
| 1058 | + } // End If Statement |
|
| 1059 | 1059 | |
| 1060 | - $user_id = get_post_meta( $order_id, '_customer_user', true ); |
|
| 1060 | + $user_id = get_post_meta( $order_id, '_customer_user', true ); |
|
| 1061 | 1061 | |
| 1062 | - if( $user_id ) { |
|
| 1062 | + if( $user_id ) { |
|
| 1063 | 1063 | |
| 1064 | - // Get all courses for product |
|
| 1065 | - $args = array( |
|
| 1066 | - 'posts_per_page' => -1, |
|
| 1067 | - 'post_type' => 'course', |
|
| 1068 | - 'meta_query' => array( |
|
| 1069 | - array( |
|
| 1070 | - 'key' => '_course_woocommerce_product', |
|
| 1071 | - 'value' => $item_id |
|
| 1072 | - ) |
|
| 1073 | - ), |
|
| 1074 | - 'orderby' => 'menu_order date', |
|
| 1075 | - 'order' => 'ASC', |
|
| 1076 | - ); |
|
| 1077 | - $courses = get_posts( $args ); |
|
| 1064 | + // Get all courses for product |
|
| 1065 | + $args = array( |
|
| 1066 | + 'posts_per_page' => -1, |
|
| 1067 | + 'post_type' => 'course', |
|
| 1068 | + 'meta_query' => array( |
|
| 1069 | + array( |
|
| 1070 | + 'key' => '_course_woocommerce_product', |
|
| 1071 | + 'value' => $item_id |
|
| 1072 | + ) |
|
| 1073 | + ), |
|
| 1074 | + 'orderby' => 'menu_order date', |
|
| 1075 | + 'order' => 'ASC', |
|
| 1076 | + ); |
|
| 1077 | + $courses = get_posts( $args ); |
|
| 1078 | 1078 | |
| 1079 | - if( $courses && count( $courses ) > 0 ) { |
|
| 1079 | + if( $courses && count( $courses ) > 0 ) { |
|
| 1080 | 1080 | |
| 1081 | - foreach( $courses as $course ) { |
|
| 1081 | + foreach( $courses as $course ) { |
|
| 1082 | 1082 | |
| 1083 | - $title = $course->post_title; |
|
| 1084 | - $permalink = get_permalink( $course->ID ); |
|
| 1085 | - $order_contains_courses = true; |
|
| 1086 | - $course_details_html .= '<p><strong>' . sprintf( __( 'View course: %1$s', 'woothemes-sensei' ), '</strong><a href="' . esc_url( $permalink ) . '">' . $title . '</a>' ) . '</p>'; |
|
| 1087 | - } |
|
| 1083 | + $title = $course->post_title; |
|
| 1084 | + $permalink = get_permalink( $course->ID ); |
|
| 1085 | + $order_contains_courses = true; |
|
| 1086 | + $course_details_html .= '<p><strong>' . sprintf( __( 'View course: %1$s', 'woothemes-sensei' ), '</strong><a href="' . esc_url( $permalink ) . '">' . $title . '</a>' ) . '</p>'; |
|
| 1087 | + } |
|
| 1088 | 1088 | |
| 1089 | 1089 | |
| 1090 | - } // end if has courses |
|
| 1090 | + } // end if has courses |
|
| 1091 | 1091 | |
| 1092 | - } // end if $userPid |
|
| 1092 | + } // end if $userPid |
|
| 1093 | 1093 | |
| 1094 | - } // end for each order item |
|
| 1094 | + } // end for each order item |
|
| 1095 | 1095 | |
| 1096 | - // Output Course details |
|
| 1097 | - if( $order_contains_courses ){ |
|
| 1096 | + // Output Course details |
|
| 1097 | + if( $order_contains_courses ){ |
|
| 1098 | 1098 | |
| 1099 | - echo $course_details_html; |
|
| 1099 | + echo $course_details_html; |
|
| 1100 | 1100 | |
| 1101 | - } |
|
| 1101 | + } |
|
| 1102 | 1102 | |
| 1103 | 1103 | |
| 1104 | - } // end if order items not empty |
|
| 1104 | + } // end if order items not empty |
|
| 1105 | 1105 | |
| 1106 | - }// end email_course_details |
|
| 1106 | + }// end email_course_details |
|
| 1107 | 1107 | |
| 1108 | - /** |
|
| 1109 | - * sensei_woocommerce_complete_order description |
|
| 1110 | - * @since 1.0.3 |
|
| 1111 | - * @access public |
|
| 1112 | - * @param int $order_id WC order ID |
|
| 1113 | - * @return void |
|
| 1114 | - */ |
|
| 1115 | - public static function complete_order ( $order_id = 0 ) { |
|
| 1108 | + /** |
|
| 1109 | + * sensei_woocommerce_complete_order description |
|
| 1110 | + * @since 1.0.3 |
|
| 1111 | + * @access public |
|
| 1112 | + * @param int $order_id WC order ID |
|
| 1113 | + * @return void |
|
| 1114 | + */ |
|
| 1115 | + public static function complete_order ( $order_id = 0 ) { |
|
| 1116 | 1116 | |
| 1117 | - $order_user = array(); |
|
| 1117 | + $order_user = array(); |
|
| 1118 | 1118 | |
| 1119 | - // Check for WooCommerce |
|
| 1120 | - if ( Sensei_WC::is_woocommerce_active() && ( 0 < $order_id ) ) { |
|
| 1121 | - // Get order object |
|
| 1122 | - $order = new WC_Order( $order_id ); |
|
| 1123 | - $user = get_user_by( 'id', $order->get_user_id() ); |
|
| 1124 | - $order_user['ID'] = $user->ID; |
|
| 1125 | - $order_user['user_login'] = $user->user_login; |
|
| 1126 | - $order_user['user_email'] = $user->user_email; |
|
| 1127 | - $order_user['user_url'] = $user->user_url; |
|
| 1128 | - // Run through each product ordered |
|
| 1129 | - if ( 0 < sizeof( $order->get_items() ) ) { |
|
| 1119 | + // Check for WooCommerce |
|
| 1120 | + if ( Sensei_WC::is_woocommerce_active() && ( 0 < $order_id ) ) { |
|
| 1121 | + // Get order object |
|
| 1122 | + $order = new WC_Order( $order_id ); |
|
| 1123 | + $user = get_user_by( 'id', $order->get_user_id() ); |
|
| 1124 | + $order_user['ID'] = $user->ID; |
|
| 1125 | + $order_user['user_login'] = $user->user_login; |
|
| 1126 | + $order_user['user_email'] = $user->user_email; |
|
| 1127 | + $order_user['user_url'] = $user->user_url; |
|
| 1128 | + // Run through each product ordered |
|
| 1129 | + if ( 0 < sizeof( $order->get_items() ) ) { |
|
| 1130 | 1130 | |
| 1131 | - foreach( $order->get_items() as $item ) { |
|
| 1131 | + foreach( $order->get_items() as $item ) { |
|
| 1132 | 1132 | |
| 1133 | - $product_type = ''; |
|
| 1134 | - if ( isset( $item['variation_id'] ) && ( 0 < $item['variation_id'] ) ) { |
|
| 1133 | + $product_type = ''; |
|
| 1134 | + if ( isset( $item['variation_id'] ) && ( 0 < $item['variation_id'] ) ) { |
|
| 1135 | 1135 | |
| 1136 | - $item_id = $item['variation_id']; |
|
| 1137 | - $product_type = 'variation'; |
|
| 1136 | + $item_id = $item['variation_id']; |
|
| 1137 | + $product_type = 'variation'; |
|
| 1138 | 1138 | |
| 1139 | - } else { |
|
| 1139 | + } else { |
|
| 1140 | 1140 | |
| 1141 | - $item_id = $item['product_id']; |
|
| 1141 | + $item_id = $item['product_id']; |
|
| 1142 | 1142 | |
| 1143 | - } // End If Statement |
|
| 1143 | + } // End If Statement |
|
| 1144 | 1144 | |
| 1145 | - $_product = Sensei_WC::get_product_object( $item_id, $product_type ); |
|
| 1145 | + $_product = Sensei_WC::get_product_object( $item_id, $product_type ); |
|
| 1146 | 1146 | |
| 1147 | - // Get courses that use the WC product |
|
| 1148 | - $courses = Sensei()->course->get_product_courses( $_product->id ); |
|
| 1147 | + // Get courses that use the WC product |
|
| 1148 | + $courses = Sensei()->course->get_product_courses( $_product->id ); |
|
| 1149 | 1149 | |
| 1150 | - // Loop and update those courses |
|
| 1151 | - foreach ( $courses as $course_item ) { |
|
| 1150 | + // Loop and update those courses |
|
| 1151 | + foreach ( $courses as $course_item ) { |
|
| 1152 | 1152 | |
| 1153 | - $update_course = Sensei()->woocommerce_course_update( $course_item->ID, $order_user ); |
|
| 1153 | + $update_course = Sensei()->woocommerce_course_update( $course_item->ID, $order_user ); |
|
| 1154 | 1154 | |
| 1155 | - } // End For Loop |
|
| 1155 | + } // End For Loop |
|
| 1156 | 1156 | |
| 1157 | - } // End For Loop |
|
| 1157 | + } // End For Loop |
|
| 1158 | 1158 | |
| 1159 | - } // End If Statement |
|
| 1160 | - // Add meta to indicate that payment has been completed successfully |
|
| 1161 | - update_post_meta( $order_id, 'sensei_payment_complete', '1' ); |
|
| 1159 | + } // End If Statement |
|
| 1160 | + // Add meta to indicate that payment has been completed successfully |
|
| 1161 | + update_post_meta( $order_id, 'sensei_payment_complete', '1' ); |
|
| 1162 | 1162 | |
| 1163 | - } // End If Statement |
|
| 1163 | + } // End If Statement |
|
| 1164 | 1164 | |
| 1165 | - } // End sensei_woocommerce_complete_order() |
|
| 1165 | + } // End sensei_woocommerce_complete_order() |
|
| 1166 | 1166 | |
| 1167 | - /** |
|
| 1168 | - * Responds to when an order is cancelled. |
|
| 1169 | - * |
|
| 1170 | - * @since 1.2.0 |
|
| 1171 | - * @since 1.9.0 Move function to the Sensei_WC class |
|
| 1172 | - * @param integer| WC_Order $order_id order ID |
|
| 1173 | - * @return void |
|
| 1174 | - */ |
|
| 1175 | - public static function cancel_order ( $order_id ) { |
|
| 1167 | + /** |
|
| 1168 | + * Responds to when an order is cancelled. |
|
| 1169 | + * |
|
| 1170 | + * @since 1.2.0 |
|
| 1171 | + * @since 1.9.0 Move function to the Sensei_WC class |
|
| 1172 | + * @param integer| WC_Order $order_id order ID |
|
| 1173 | + * @return void |
|
| 1174 | + */ |
|
| 1175 | + public static function cancel_order ( $order_id ) { |
|
| 1176 | 1176 | |
| 1177 | - // Get order object |
|
| 1178 | - if( is_object( $order_id ) ){ |
|
| 1177 | + // Get order object |
|
| 1178 | + if( is_object( $order_id ) ){ |
|
| 1179 | 1179 | |
| 1180 | - $order = $order_id; |
|
| 1180 | + $order = $order_id; |
|
| 1181 | 1181 | |
| 1182 | - }else{ |
|
| 1182 | + }else{ |
|
| 1183 | 1183 | |
| 1184 | - $order = new WC_Order( $order_id ); |
|
| 1185 | - } |
|
| 1184 | + $order = new WC_Order( $order_id ); |
|
| 1185 | + } |
|
| 1186 | 1186 | |
| 1187 | - // Run through each product ordered |
|
| 1188 | - if ( 0 < sizeof( $order->get_items() ) ) { |
|
| 1187 | + // Run through each product ordered |
|
| 1188 | + if ( 0 < sizeof( $order->get_items() ) ) { |
|
| 1189 | 1189 | |
| 1190 | - // Get order user |
|
| 1191 | - $user_id = $order->__get( 'user_id' ); |
|
| 1190 | + // Get order user |
|
| 1191 | + $user_id = $order->__get( 'user_id' ); |
|
| 1192 | 1192 | |
| 1193 | - foreach( $order->get_items() as $item ) { |
|
| 1193 | + foreach( $order->get_items() as $item ) { |
|
| 1194 | 1194 | |
| 1195 | - $product_type = ''; |
|
| 1196 | - if ( isset( $item['variation_id'] ) && ( 0 < $item['variation_id'] ) ) { |
|
| 1195 | + $product_type = ''; |
|
| 1196 | + if ( isset( $item['variation_id'] ) && ( 0 < $item['variation_id'] ) ) { |
|
| 1197 | 1197 | |
| 1198 | - $item_id = $item['variation_id']; |
|
| 1199 | - $product_type = 'variation'; |
|
| 1198 | + $item_id = $item['variation_id']; |
|
| 1199 | + $product_type = 'variation'; |
|
| 1200 | 1200 | |
| 1201 | - } else { |
|
| 1201 | + } else { |
|
| 1202 | 1202 | |
| 1203 | - $item_id = $item['product_id']; |
|
| 1203 | + $item_id = $item['product_id']; |
|
| 1204 | 1204 | |
| 1205 | - } // End If Statement |
|
| 1205 | + } // End If Statement |
|
| 1206 | 1206 | |
| 1207 | - $_product = Sensei_WC::get_product_object( $item_id, $product_type ); |
|
| 1207 | + $_product = Sensei_WC::get_product_object( $item_id, $product_type ); |
|
| 1208 | 1208 | |
| 1209 | - // Get courses that use the WC product |
|
| 1210 | - $courses = array(); |
|
| 1211 | - $courses = Sensei()->course->get_product_courses( $item_id ); |
|
| 1209 | + // Get courses that use the WC product |
|
| 1210 | + $courses = array(); |
|
| 1211 | + $courses = Sensei()->course->get_product_courses( $item_id ); |
|
| 1212 | 1212 | |
| 1213 | - // Loop and update those courses |
|
| 1214 | - foreach ($courses as $course_item){ |
|
| 1213 | + // Loop and update those courses |
|
| 1214 | + foreach ($courses as $course_item){ |
|
| 1215 | 1215 | |
| 1216 | - // Check and Remove course from courses user meta |
|
| 1217 | - $dataset_changes = Sensei_Utils::sensei_remove_user_from_course( $course_item->ID, $user_id ); |
|
| 1216 | + // Check and Remove course from courses user meta |
|
| 1217 | + $dataset_changes = Sensei_Utils::sensei_remove_user_from_course( $course_item->ID, $user_id ); |
|
| 1218 | 1218 | |
| 1219 | - } // End For Loop |
|
| 1219 | + } // End For Loop |
|
| 1220 | 1220 | |
| 1221 | - } // End For Loop |
|
| 1221 | + } // End For Loop |
|
| 1222 | 1222 | |
| 1223 | - } // End If Statement |
|
| 1223 | + } // End If Statement |
|
| 1224 | 1224 | |
| 1225 | - } // End sensei_woocommerce_cancel_order() |
|
| 1225 | + } // End sensei_woocommerce_cancel_order() |
|
| 1226 | 1226 | |
| 1227 | - /** |
|
| 1228 | - * Returns the WooCommerce Product Object |
|
| 1229 | - * |
|
| 1230 | - * The code caters for pre and post WooCommerce 2.2 installations. |
|
| 1231 | - * |
|
| 1232 | - * @since 1.1.1 |
|
| 1233 | - * @access public |
|
| 1234 | - * @param integer $wc_product_id Product ID or Variation ID |
|
| 1235 | - * @param string $product_type '' or 'variation' |
|
| 1236 | - * @return WC_Product $wc_product_object |
|
| 1237 | - */ |
|
| 1238 | - public static function get_product_object ( $wc_product_id = 0, $product_type = '' ) { |
|
| 1227 | + /** |
|
| 1228 | + * Returns the WooCommerce Product Object |
|
| 1229 | + * |
|
| 1230 | + * The code caters for pre and post WooCommerce 2.2 installations. |
|
| 1231 | + * |
|
| 1232 | + * @since 1.1.1 |
|
| 1233 | + * @access public |
|
| 1234 | + * @param integer $wc_product_id Product ID or Variation ID |
|
| 1235 | + * @param string $product_type '' or 'variation' |
|
| 1236 | + * @return WC_Product $wc_product_object |
|
| 1237 | + */ |
|
| 1238 | + public static function get_product_object ( $wc_product_id = 0, $product_type = '' ) { |
|
| 1239 | 1239 | |
| 1240 | - $wc_product_object = false; |
|
| 1241 | - if ( 0 < intval( $wc_product_id ) ) { |
|
| 1240 | + $wc_product_object = false; |
|
| 1241 | + if ( 0 < intval( $wc_product_id ) ) { |
|
| 1242 | 1242 | |
| 1243 | - // Get the product |
|
| 1244 | - if ( function_exists( 'wc_get_product' ) ) { |
|
| 1243 | + // Get the product |
|
| 1244 | + if ( function_exists( 'wc_get_product' ) ) { |
|
| 1245 | 1245 | |
| 1246 | - $wc_product_object = wc_get_product( $wc_product_id ); // Post WC 2.3 |
|
| 1246 | + $wc_product_object = wc_get_product( $wc_product_id ); // Post WC 2.3 |
|
| 1247 | 1247 | |
| 1248 | - } elseif ( function_exists( 'get_product' ) ) { |
|
| 1248 | + } elseif ( function_exists( 'get_product' ) ) { |
|
| 1249 | 1249 | |
| 1250 | - $wc_product_object = get_product( $wc_product_id ); // Post WC 2.0 |
|
| 1250 | + $wc_product_object = get_product( $wc_product_id ); // Post WC 2.0 |
|
| 1251 | 1251 | |
| 1252 | - } else { |
|
| 1252 | + } else { |
|
| 1253 | 1253 | |
| 1254 | - // Pre WC 2.0 |
|
| 1255 | - if ( 'variation' == $product_type || 'subscription_variation' == $product_type ) { |
|
| 1254 | + // Pre WC 2.0 |
|
| 1255 | + if ( 'variation' == $product_type || 'subscription_variation' == $product_type ) { |
|
| 1256 | 1256 | |
| 1257 | - $wc_product_object = new WC_Product_Variation( $wc_product_id ); |
|
| 1257 | + $wc_product_object = new WC_Product_Variation( $wc_product_id ); |
|
| 1258 | 1258 | |
| 1259 | - } else { |
|
| 1259 | + } else { |
|
| 1260 | 1260 | |
| 1261 | - $wc_product_object = new WC_Product( $wc_product_id ); |
|
| 1261 | + $wc_product_object = new WC_Product( $wc_product_id ); |
|
| 1262 | 1262 | |
| 1263 | - } // End If Statement |
|
| 1263 | + } // End If Statement |
|
| 1264 | 1264 | |
| 1265 | - } // End If Statement |
|
| 1265 | + } // End If Statement |
|
| 1266 | 1266 | |
| 1267 | - } // End If Statement |
|
| 1267 | + } // End If Statement |
|
| 1268 | 1268 | |
| 1269 | - return $wc_product_object; |
|
| 1269 | + return $wc_product_object; |
|
| 1270 | 1270 | |
| 1271 | - } // End sensei_get_woocommerce_product_object() |
|
| 1271 | + } // End sensei_get_woocommerce_product_object() |
|
| 1272 | 1272 | |
| 1273 | - /** |
|
| 1274 | - * If customer has purchased the course, update Sensei to indicate that they are taking the course. |
|
| 1275 | - * |
|
| 1276 | - * @since 1.0.0 |
|
| 1277 | - * @since 1.9.0 move to class Sensei_WC |
|
| 1278 | - * |
|
| 1279 | - * @param int $course_id (default: 0) |
|
| 1280 | - * @param array/Object $order_user (default: array()) Specific user's data. |
|
| 1281 | - * |
|
| 1282 | - * @return bool|int |
|
| 1283 | - */ |
|
| 1284 | - public static function course_update ( $course_id = 0, $order_user = array() ) { |
|
| 1273 | + /** |
|
| 1274 | + * If customer has purchased the course, update Sensei to indicate that they are taking the course. |
|
| 1275 | + * |
|
| 1276 | + * @since 1.0.0 |
|
| 1277 | + * @since 1.9.0 move to class Sensei_WC |
|
| 1278 | + * |
|
| 1279 | + * @param int $course_id (default: 0) |
|
| 1280 | + * @param array/Object $order_user (default: array()) Specific user's data. |
|
| 1281 | + * |
|
| 1282 | + * @return bool|int |
|
| 1283 | + */ |
|
| 1284 | + public static function course_update ( $course_id = 0, $order_user = array() ) { |
|
| 1285 | 1285 | |
| 1286 | - global $current_user; |
|
| 1286 | + global $current_user; |
|
| 1287 | 1287 | |
| 1288 | - if ( ! isset( $current_user ) || !$current_user->ID > 0 ) return false; |
|
| 1288 | + if ( ! isset( $current_user ) || !$current_user->ID > 0 ) return false; |
|
| 1289 | 1289 | |
| 1290 | - $data_update = false; |
|
| 1290 | + $data_update = false; |
|
| 1291 | 1291 | |
| 1292 | - // Get the product ID |
|
| 1293 | - $wc_post_id = get_post_meta( intval( $course_id ), '_course_woocommerce_product', true ); |
|
| 1292 | + // Get the product ID |
|
| 1293 | + $wc_post_id = get_post_meta( intval( $course_id ), '_course_woocommerce_product', true ); |
|
| 1294 | 1294 | |
| 1295 | - // Check if in the admin |
|
| 1296 | - if ( is_admin() ) { |
|
| 1295 | + // Check if in the admin |
|
| 1296 | + if ( is_admin() ) { |
|
| 1297 | 1297 | |
| 1298 | - $user_login = $order_user['user_login']; |
|
| 1299 | - $user_email = $order_user['user_email']; |
|
| 1300 | - $user_url = $order_user['user_url']; |
|
| 1301 | - $user_id = $order_user['ID']; |
|
| 1298 | + $user_login = $order_user['user_login']; |
|
| 1299 | + $user_email = $order_user['user_email']; |
|
| 1300 | + $user_url = $order_user['user_url']; |
|
| 1301 | + $user_id = $order_user['ID']; |
|
| 1302 | 1302 | |
| 1303 | - } else { |
|
| 1303 | + } else { |
|
| 1304 | 1304 | |
| 1305 | - $user_login = $current_user->user_login; |
|
| 1306 | - $user_email = $current_user->user_email; |
|
| 1307 | - $user_url = $current_user->user_url; |
|
| 1308 | - $user_id = $current_user->ID; |
|
| 1305 | + $user_login = $current_user->user_login; |
|
| 1306 | + $user_email = $current_user->user_email; |
|
| 1307 | + $user_url = $current_user->user_url; |
|
| 1308 | + $user_id = $current_user->ID; |
|
| 1309 | 1309 | |
| 1310 | - } // End If Statement |
|
| 1310 | + } // End If Statement |
|
| 1311 | 1311 | |
| 1312 | - // This doesn't appear to be purely WooCommerce related. Should it be in a separate function? |
|
| 1313 | - $course_prerequisite_id = (int) get_post_meta( $course_id, '_course_prerequisite', true ); |
|
| 1314 | - if( 0 < absint( $course_prerequisite_id ) ) { |
|
| 1312 | + // This doesn't appear to be purely WooCommerce related. Should it be in a separate function? |
|
| 1313 | + $course_prerequisite_id = (int) get_post_meta( $course_id, '_course_prerequisite', true ); |
|
| 1314 | + if( 0 < absint( $course_prerequisite_id ) ) { |
|
| 1315 | 1315 | |
| 1316 | - $prereq_course_complete = Sensei_Utils::user_completed_course( $course_prerequisite_id, intval( $user_id ) ); |
|
| 1317 | - if ( ! $prereq_course_complete ) { |
|
| 1316 | + $prereq_course_complete = Sensei_Utils::user_completed_course( $course_prerequisite_id, intval( $user_id ) ); |
|
| 1317 | + if ( ! $prereq_course_complete ) { |
|
| 1318 | 1318 | |
| 1319 | - // Remove all course user meta |
|
| 1320 | - return Sensei_Utils::sensei_remove_user_from_course( $course_id, $user_id ); |
|
| 1319 | + // Remove all course user meta |
|
| 1320 | + return Sensei_Utils::sensei_remove_user_from_course( $course_id, $user_id ); |
|
| 1321 | 1321 | |
| 1322 | - } |
|
| 1323 | - } |
|
| 1322 | + } |
|
| 1323 | + } |
|
| 1324 | 1324 | |
| 1325 | - $is_user_taking_course = Sensei_Utils::user_started_course( intval( $course_id ), intval( $user_id ) ); |
|
| 1325 | + $is_user_taking_course = Sensei_Utils::user_started_course( intval( $course_id ), intval( $user_id ) ); |
|
| 1326 | 1326 | |
| 1327 | - if( ! $is_user_taking_course ) { |
|
| 1327 | + if( ! $is_user_taking_course ) { |
|
| 1328 | 1328 | |
| 1329 | - if ( Sensei_WC::is_woocommerce_active() && Sensei_WC::has_customer_bought_product( $user_id, $wc_post_id ) && ( 0 < $wc_post_id ) ) { |
|
| 1329 | + if ( Sensei_WC::is_woocommerce_active() && Sensei_WC::has_customer_bought_product( $user_id, $wc_post_id ) && ( 0 < $wc_post_id ) ) { |
|
| 1330 | 1330 | |
| 1331 | - $activity_logged = Sensei_Utils::user_start_course( intval( $user_id), intval( $course_id ) ); |
|
| 1331 | + $activity_logged = Sensei_Utils::user_start_course( intval( $user_id), intval( $course_id ) ); |
|
| 1332 | 1332 | |
| 1333 | - $is_user_taking_course = false; |
|
| 1334 | - if ( true == $activity_logged ) { |
|
| 1333 | + $is_user_taking_course = false; |
|
| 1334 | + if ( true == $activity_logged ) { |
|
| 1335 | 1335 | |
| 1336 | - $is_user_taking_course = true; |
|
| 1336 | + $is_user_taking_course = true; |
|
| 1337 | 1337 | |
| 1338 | - } // End If Statement |
|
| 1338 | + } // End If Statement |
|
| 1339 | 1339 | |
| 1340 | - } // End If Statement |
|
| 1341 | - }// end if is user taking course |
|
| 1340 | + } // End If Statement |
|
| 1341 | + }// end if is user taking course |
|
| 1342 | 1342 | |
| 1343 | - return $is_user_taking_course; |
|
| 1343 | + return $is_user_taking_course; |
|
| 1344 | 1344 | |
| 1345 | - } // End course_update() |
|
| 1345 | + } // End course_update() |
|
| 1346 | 1346 | |
| 1347 | - /** |
|
| 1348 | - * Disable guest checkout if a course product is in the cart |
|
| 1349 | - * |
|
| 1350 | - * @since 1.1.0 |
|
| 1351 | - * @since 1.9.0 move to class Sensei_WC |
|
| 1352 | - * |
|
| 1353 | - * @param boolean $guest_checkout Current guest checkout setting |
|
| 1354 | - * |
|
| 1355 | - * @return boolean Modified guest checkout setting |
|
| 1356 | - */ |
|
| 1357 | - public static function disable_guest_checkout( $guest_checkout ) { |
|
| 1347 | + /** |
|
| 1348 | + * Disable guest checkout if a course product is in the cart |
|
| 1349 | + * |
|
| 1350 | + * @since 1.1.0 |
|
| 1351 | + * @since 1.9.0 move to class Sensei_WC |
|
| 1352 | + * |
|
| 1353 | + * @param boolean $guest_checkout Current guest checkout setting |
|
| 1354 | + * |
|
| 1355 | + * @return boolean Modified guest checkout setting |
|
| 1356 | + */ |
|
| 1357 | + public static function disable_guest_checkout( $guest_checkout ) { |
|
| 1358 | 1358 | |
| 1359 | - if( ! is_admin() || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 1359 | + if( ! is_admin() || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 1360 | 1360 | |
| 1361 | - if( isset( WC()->cart->cart_contents ) && count( WC()->cart->cart_contents ) > 0 ) { |
|
| 1361 | + if( isset( WC()->cart->cart_contents ) && count( WC()->cart->cart_contents ) > 0 ) { |
|
| 1362 | 1362 | |
| 1363 | - foreach( WC()->cart->cart_contents as $cart_key => $product ) { |
|
| 1364 | - if( isset( $product['product_id'] ) ) { |
|
| 1363 | + foreach( WC()->cart->cart_contents as $cart_key => $product ) { |
|
| 1364 | + if( isset( $product['product_id'] ) ) { |
|
| 1365 | 1365 | |
| 1366 | - $args = array( |
|
| 1367 | - 'posts_per_page' => -1, |
|
| 1368 | - 'post_type' => 'course', |
|
| 1369 | - 'meta_query' => array( |
|
| 1370 | - array( |
|
| 1371 | - 'key' => '_course_woocommerce_product', |
|
| 1372 | - 'value' => $product['product_id'] |
|
| 1373 | - ) |
|
| 1374 | - ) |
|
| 1375 | - ); |
|
| 1366 | + $args = array( |
|
| 1367 | + 'posts_per_page' => -1, |
|
| 1368 | + 'post_type' => 'course', |
|
| 1369 | + 'meta_query' => array( |
|
| 1370 | + array( |
|
| 1371 | + 'key' => '_course_woocommerce_product', |
|
| 1372 | + 'value' => $product['product_id'] |
|
| 1373 | + ) |
|
| 1374 | + ) |
|
| 1375 | + ); |
|
| 1376 | 1376 | |
| 1377 | - $posts = get_posts( $args ); |
|
| 1377 | + $posts = get_posts( $args ); |
|
| 1378 | 1378 | |
| 1379 | - if( $posts && count( $posts ) > 0 ) { |
|
| 1379 | + if( $posts && count( $posts ) > 0 ) { |
|
| 1380 | 1380 | |
| 1381 | - foreach( $posts as $course ) { |
|
| 1382 | - $guest_checkout = ''; |
|
| 1383 | - break; |
|
| 1381 | + foreach( $posts as $course ) { |
|
| 1382 | + $guest_checkout = ''; |
|
| 1383 | + break; |
|
| 1384 | 1384 | |
| 1385 | - } |
|
| 1386 | - } |
|
| 1385 | + } |
|
| 1386 | + } |
|
| 1387 | 1387 | |
| 1388 | - } |
|
| 1388 | + } |
|
| 1389 | 1389 | |
| 1390 | - } |
|
| 1390 | + } |
|
| 1391 | 1391 | |
| 1392 | - } |
|
| 1393 | - } |
|
| 1392 | + } |
|
| 1393 | + } |
|
| 1394 | 1394 | |
| 1395 | - return $guest_checkout; |
|
| 1395 | + return $guest_checkout; |
|
| 1396 | 1396 | |
| 1397 | - }// end disable_guest_checkout |
|
| 1397 | + }// end disable_guest_checkout |
|
| 1398 | 1398 | |
| 1399 | - /** |
|
| 1400 | - * Change order status with virtual products to completed |
|
| 1401 | - * |
|
| 1402 | - * @since 1.1.0 |
|
| 1403 | - * @since 1.9.0 move to class Sensei_WC |
|
| 1404 | - * |
|
| 1405 | - * @param string $order_status |
|
| 1406 | - * @param int $order_id |
|
| 1407 | - * |
|
| 1408 | - * @return string |
|
| 1409 | - **/ |
|
| 1410 | - public static function virtual_order_payment_complete( $order_status, $order_id ) { |
|
| 1399 | + /** |
|
| 1400 | + * Change order status with virtual products to completed |
|
| 1401 | + * |
|
| 1402 | + * @since 1.1.0 |
|
| 1403 | + * @since 1.9.0 move to class Sensei_WC |
|
| 1404 | + * |
|
| 1405 | + * @param string $order_status |
|
| 1406 | + * @param int $order_id |
|
| 1407 | + * |
|
| 1408 | + * @return string |
|
| 1409 | + **/ |
|
| 1410 | + public static function virtual_order_payment_complete( $order_status, $order_id ) { |
|
| 1411 | 1411 | |
| 1412 | - $order = new WC_Order( $order_id ); |
|
| 1412 | + $order = new WC_Order( $order_id ); |
|
| 1413 | 1413 | |
| 1414 | - if ( ! isset ( $order ) ) return ''; |
|
| 1414 | + if ( ! isset ( $order ) ) return ''; |
|
| 1415 | 1415 | |
| 1416 | - if ( $order_status == 'wc-processing' && ( $order->post_status == 'wc-on-hold' || $order->post_status == 'wc-pending' || $order->post_status == 'wc-failed' ) ) { |
|
| 1416 | + if ( $order_status == 'wc-processing' && ( $order->post_status == 'wc-on-hold' || $order->post_status == 'wc-pending' || $order->post_status == 'wc-failed' ) ) { |
|
| 1417 | 1417 | |
| 1418 | - $virtual_order = true; |
|
| 1418 | + $virtual_order = true; |
|
| 1419 | 1419 | |
| 1420 | - if ( count( $order->get_items() ) > 0 ) { |
|
| 1420 | + if ( count( $order->get_items() ) > 0 ) { |
|
| 1421 | 1421 | |
| 1422 | - foreach( $order->get_items() as $item ) { |
|
| 1422 | + foreach( $order->get_items() as $item ) { |
|
| 1423 | 1423 | |
| 1424 | - if ( $item['product_id'] > 0 ) { |
|
| 1425 | - $_product = $order->get_product_from_item( $item ); |
|
| 1426 | - if ( ! $_product->is_virtual() ) { |
|
| 1424 | + if ( $item['product_id'] > 0 ) { |
|
| 1425 | + $_product = $order->get_product_from_item( $item ); |
|
| 1426 | + if ( ! $_product->is_virtual() ) { |
|
| 1427 | 1427 | |
| 1428 | - $virtual_order = false; |
|
| 1429 | - break; |
|
| 1428 | + $virtual_order = false; |
|
| 1429 | + break; |
|
| 1430 | 1430 | |
| 1431 | - } // End If Statement |
|
| 1431 | + } // End If Statement |
|
| 1432 | 1432 | |
| 1433 | - } // End If Statement |
|
| 1433 | + } // End If Statement |
|
| 1434 | 1434 | |
| 1435 | - } // End For Loop |
|
| 1435 | + } // End For Loop |
|
| 1436 | 1436 | |
| 1437 | - } // End If Statement |
|
| 1437 | + } // End If Statement |
|
| 1438 | 1438 | |
| 1439 | - // virtual order, mark as completed |
|
| 1440 | - if ( $virtual_order ) { |
|
| 1439 | + // virtual order, mark as completed |
|
| 1440 | + if ( $virtual_order ) { |
|
| 1441 | 1441 | |
| 1442 | - return 'completed'; |
|
| 1442 | + return 'completed'; |
|
| 1443 | 1443 | |
| 1444 | - } // End If Statement |
|
| 1444 | + } // End If Statement |
|
| 1445 | 1445 | |
| 1446 | - } // End If Statement |
|
| 1446 | + } // End If Statement |
|
| 1447 | 1447 | |
| 1448 | - return $order_status; |
|
| 1448 | + return $order_status; |
|
| 1449 | 1449 | |
| 1450 | - }// end virtual_order_payment_complete |
|
| 1450 | + }// end virtual_order_payment_complete |
|
| 1451 | 1451 | |
| 1452 | 1452 | |
| 1453 | - /** |
|
| 1454 | - * Determine if the user has and active subscription to give them access |
|
| 1455 | - * to the requested resource. |
|
| 1456 | - * |
|
| 1457 | - * @since 1.9.0 |
|
| 1458 | - * |
|
| 1459 | - * @param boolean$user_access_permission |
|
| 1460 | - * @param integer $user_id |
|
| 1461 | - * @return boolean $user_access_permission |
|
| 1462 | - */ |
|
| 1463 | - public static function get_subscription_permission( $user_access_permission, $user_id ){ |
|
| 1453 | + /** |
|
| 1454 | + * Determine if the user has and active subscription to give them access |
|
| 1455 | + * to the requested resource. |
|
| 1456 | + * |
|
| 1457 | + * @since 1.9.0 |
|
| 1458 | + * |
|
| 1459 | + * @param boolean$user_access_permission |
|
| 1460 | + * @param integer $user_id |
|
| 1461 | + * @return boolean $user_access_permission |
|
| 1462 | + */ |
|
| 1463 | + public static function get_subscription_permission( $user_access_permission, $user_id ){ |
|
| 1464 | 1464 | |
| 1465 | - global $post; |
|
| 1465 | + global $post; |
|
| 1466 | 1466 | |
| 1467 | - // ignore the current case if the following conditions are met |
|
| 1468 | - if ( ! class_exists( 'WC_Subscriptions' ) || empty( $user_id ) |
|
| 1469 | - || ! in_array( $post->post_type, array( 'course','lesson','quiz' ) ) |
|
| 1470 | - || ! wcs_user_has_subscription( $user_id) ){ |
|
| 1467 | + // ignore the current case if the following conditions are met |
|
| 1468 | + if ( ! class_exists( 'WC_Subscriptions' ) || empty( $user_id ) |
|
| 1469 | + || ! in_array( $post->post_type, array( 'course','lesson','quiz' ) ) |
|
| 1470 | + || ! wcs_user_has_subscription( $user_id) ){ |
|
| 1471 | 1471 | |
| 1472 | - return $user_access_permission; |
|
| 1472 | + return $user_access_permission; |
|
| 1473 | 1473 | |
| 1474 | - } |
|
| 1474 | + } |
|
| 1475 | 1475 | |
| 1476 | - // at this user has a subscription |
|
| 1477 | - // is the subscription on the the current course? |
|
| 1476 | + // at this user has a subscription |
|
| 1477 | + // is the subscription on the the current course? |
|
| 1478 | 1478 | |
| 1479 | - $course_id = 0; |
|
| 1480 | - if ( 'course' == $post->post_type ){ |
|
| 1479 | + $course_id = 0; |
|
| 1480 | + if ( 'course' == $post->post_type ){ |
|
| 1481 | 1481 | |
| 1482 | - $course_id = $post->ID; |
|
| 1482 | + $course_id = $post->ID; |
|
| 1483 | 1483 | |
| 1484 | - } elseif ( 'lesson' == $post->post_type ) { |
|
| 1484 | + } elseif ( 'lesson' == $post->post_type ) { |
|
| 1485 | 1485 | |
| 1486 | - $course_id = Sensei()->lesson->get_course_id( $post->ID ); |
|
| 1486 | + $course_id = Sensei()->lesson->get_course_id( $post->ID ); |
|
| 1487 | 1487 | |
| 1488 | - } else { |
|
| 1488 | + } else { |
|
| 1489 | 1489 | |
| 1490 | - $lesson_id = Sensei()->quiz->get_lesson_id( $post->ID ); |
|
| 1491 | - $course_id = Sensei()->lesson->get_course_id( $lesson_id ); |
|
| 1490 | + $lesson_id = Sensei()->quiz->get_lesson_id( $post->ID ); |
|
| 1491 | + $course_id = Sensei()->lesson->get_course_id( $lesson_id ); |
|
| 1492 | 1492 | |
| 1493 | - } |
|
| 1493 | + } |
|
| 1494 | 1494 | |
| 1495 | - // if the course has no subscription WooCommerce product attached to return the permissions as is |
|
| 1496 | - $product_id = Sensei_WC::get_course_product_id( $course_id ); |
|
| 1497 | - $product = wc_get_product( $product_id ); |
|
| 1498 | - if( ! in_array( $product->get_type(), self::get_subscription_types() ) ){ |
|
| 1495 | + // if the course has no subscription WooCommerce product attached to return the permissions as is |
|
| 1496 | + $product_id = Sensei_WC::get_course_product_id( $course_id ); |
|
| 1497 | + $product = wc_get_product( $product_id ); |
|
| 1498 | + if( ! in_array( $product->get_type(), self::get_subscription_types() ) ){ |
|
| 1499 | 1499 | |
| 1500 | - return $user_access_permission; |
|
| 1500 | + return $user_access_permission; |
|
| 1501 | 1501 | |
| 1502 | - } |
|
| 1502 | + } |
|
| 1503 | 1503 | |
| 1504 | - // give access if user has active subscription on the product otherwise restrict it. |
|
| 1505 | - // also check if the user was added to the course directly after the subscription started. |
|
| 1506 | - if( wcs_user_has_subscription( $user_id, $product_id, 'active' ) |
|
| 1507 | - || wcs_user_has_subscription( $user_id, $product_id, 'pending-cancel' ) |
|
| 1508 | - || self::was_user_added_without_subscription( $user_id, $product_id, $course_id ) ){ |
|
| 1504 | + // give access if user has active subscription on the product otherwise restrict it. |
|
| 1505 | + // also check if the user was added to the course directly after the subscription started. |
|
| 1506 | + if( wcs_user_has_subscription( $user_id, $product_id, 'active' ) |
|
| 1507 | + || wcs_user_has_subscription( $user_id, $product_id, 'pending-cancel' ) |
|
| 1508 | + || self::was_user_added_without_subscription( $user_id, $product_id, $course_id ) ){ |
|
| 1509 | 1509 | |
| 1510 | - $user_access_permission = true; |
|
| 1510 | + $user_access_permission = true; |
|
| 1511 | 1511 | |
| 1512 | - }else{ |
|
| 1512 | + }else{ |
|
| 1513 | 1513 | |
| 1514 | - $user_access_permission = false; |
|
| 1515 | - // do not show the WC permissions message |
|
| 1516 | - remove_filter( 'sensei_the_no_permissions_message', array( 'Sensei_WC', 'alter_no_permissions_message' ), 20, 2 ); |
|
| 1517 | - Sensei()->permissions_message['title'] = __( 'No active subscription', 'woothemes-sensei' ); |
|
| 1518 | - Sensei()->permissions_message['message'] = __( 'Sorry, you do not have an access to this content without an active subscription.', 'woothemes-sensei' ); |
|
| 1519 | - } |
|
| 1514 | + $user_access_permission = false; |
|
| 1515 | + // do not show the WC permissions message |
|
| 1516 | + remove_filter( 'sensei_the_no_permissions_message', array( 'Sensei_WC', 'alter_no_permissions_message' ), 20, 2 ); |
|
| 1517 | + Sensei()->permissions_message['title'] = __( 'No active subscription', 'woothemes-sensei' ); |
|
| 1518 | + Sensei()->permissions_message['message'] = __( 'Sorry, you do not have an access to this content without an active subscription.', 'woothemes-sensei' ); |
|
| 1519 | + } |
|
| 1520 | 1520 | |
| 1521 | - return $user_access_permission; |
|
| 1521 | + return $user_access_permission; |
|
| 1522 | 1522 | |
| 1523 | - } // end get_subscription_permission |
|
| 1523 | + } // end get_subscription_permission |
|
| 1524 | 1524 | |
| 1525 | - /** |
|
| 1526 | - * Get all the valid subscription types. |
|
| 1527 | - * |
|
| 1528 | - * @since 1.9.0 |
|
| 1529 | - * @return array |
|
| 1530 | - */ |
|
| 1531 | - public static function get_subscription_types(){ |
|
| 1525 | + /** |
|
| 1526 | + * Get all the valid subscription types. |
|
| 1527 | + * |
|
| 1528 | + * @since 1.9.0 |
|
| 1529 | + * @return array |
|
| 1530 | + */ |
|
| 1531 | + public static function get_subscription_types(){ |
|
| 1532 | 1532 | |
| 1533 | - return array( 'subscription','subscription_variation','variable-subscription' ); |
|
| 1533 | + return array( 'subscription','subscription_variation','variable-subscription' ); |
|
| 1534 | 1534 | |
| 1535 | - } |
|
| 1535 | + } |
|
| 1536 | 1536 | |
| 1537 | - /** |
|
| 1538 | - * Compare the user's subscriptions end date with the date |
|
| 1539 | - * the user was added to the course. If the user was added after |
|
| 1540 | - * the subscription ended they were manually added and this will return |
|
| 1541 | - * true. |
|
| 1542 | - * |
|
| 1543 | - * Important to note that all subscriptions for the user is compared. |
|
| 1544 | - * |
|
| 1545 | - * @since 1.9.0 |
|
| 1546 | - * |
|
| 1547 | - * @param $user_id |
|
| 1548 | - * @param $product_id |
|
| 1549 | - * @param $course_id |
|
| 1550 | - * |
|
| 1551 | - * @return bool |
|
| 1552 | - */ |
|
| 1553 | - public static function was_user_added_without_subscription($user_id, $product_id, $course_id ){ |
|
| 1537 | + /** |
|
| 1538 | + * Compare the user's subscriptions end date with the date |
|
| 1539 | + * the user was added to the course. If the user was added after |
|
| 1540 | + * the subscription ended they were manually added and this will return |
|
| 1541 | + * true. |
|
| 1542 | + * |
|
| 1543 | + * Important to note that all subscriptions for the user is compared. |
|
| 1544 | + * |
|
| 1545 | + * @since 1.9.0 |
|
| 1546 | + * |
|
| 1547 | + * @param $user_id |
|
| 1548 | + * @param $product_id |
|
| 1549 | + * @param $course_id |
|
| 1550 | + * |
|
| 1551 | + * @return bool |
|
| 1552 | + */ |
|
| 1553 | + public static function was_user_added_without_subscription($user_id, $product_id, $course_id ){ |
|
| 1554 | 1554 | |
| 1555 | - $course_start_date = ''; |
|
| 1556 | - $subscription_start_date = ''; |
|
| 1557 | - $is_a_subscription =''; |
|
| 1558 | - $was_user_added_without_subscription = true; |
|
| 1555 | + $course_start_date = ''; |
|
| 1556 | + $subscription_start_date = ''; |
|
| 1557 | + $is_a_subscription =''; |
|
| 1558 | + $was_user_added_without_subscription = true; |
|
| 1559 | 1559 | |
| 1560 | - // if user is not on the course they were not added |
|
| 1561 | - if( ! Sensei_Utils::user_started_course( $course_id, $user_id ) ){ |
|
| 1560 | + // if user is not on the course they were not added |
|
| 1561 | + if( ! Sensei_Utils::user_started_course( $course_id, $user_id ) ){ |
|
| 1562 | 1562 | |
| 1563 | - return false; |
|
| 1563 | + return false; |
|
| 1564 | 1564 | |
| 1565 | - } |
|
| 1565 | + } |
|
| 1566 | 1566 | |
| 1567 | - // if user doesn't have a subscription and is taking the course |
|
| 1568 | - // they were added manually |
|
| 1569 | - if ( ! wcs_user_has_subscription($user_id, $product_id) |
|
| 1570 | - && Sensei_Utils::user_started_course( $course_id, get_current_user_id() ) ){ |
|
| 1567 | + // if user doesn't have a subscription and is taking the course |
|
| 1568 | + // they were added manually |
|
| 1569 | + if ( ! wcs_user_has_subscription($user_id, $product_id) |
|
| 1570 | + && Sensei_Utils::user_started_course( $course_id, get_current_user_id() ) ){ |
|
| 1571 | 1571 | |
| 1572 | - return true; |
|
| 1572 | + return true; |
|
| 1573 | 1573 | |
| 1574 | - } |
|
| 1574 | + } |
|
| 1575 | 1575 | |
| 1576 | - $course_status = Sensei_Utils::user_course_status( $course_id, $user_id ); |
|
| 1576 | + $course_status = Sensei_Utils::user_course_status( $course_id, $user_id ); |
|
| 1577 | 1577 | |
| 1578 | - // comparing dates setup data |
|
| 1579 | - $course_start_date = date_create( $course_status->comment_date ); |
|
| 1580 | - $subscriptions = wcs_get_users_subscriptions( $user_id ); |
|
| 1578 | + // comparing dates setup data |
|
| 1579 | + $course_start_date = date_create( $course_status->comment_date ); |
|
| 1580 | + $subscriptions = wcs_get_users_subscriptions( $user_id ); |
|
| 1581 | 1581 | |
| 1582 | - // comparing every subscription |
|
| 1583 | - foreach( $subscriptions as $subscription ){ |
|
| 1582 | + // comparing every subscription |
|
| 1583 | + foreach( $subscriptions as $subscription ){ |
|
| 1584 | 1584 | |
| 1585 | - // for the following statuses we know the user was not added |
|
| 1586 | - // manually |
|
| 1587 | - $status = $subscription->get_status(); |
|
| 1588 | - if ( in_array( $status, array( 'pending-canceled', 'active', 'on-hold', 'pending' ) ) ) { |
|
| 1585 | + // for the following statuses we know the user was not added |
|
| 1586 | + // manually |
|
| 1587 | + $status = $subscription->get_status(); |
|
| 1588 | + if ( in_array( $status, array( 'pending-canceled', 'active', 'on-hold', 'pending' ) ) ) { |
|
| 1589 | 1589 | |
| 1590 | - continue; |
|
| 1590 | + continue; |
|
| 1591 | 1591 | |
| 1592 | - } |
|
| 1592 | + } |
|
| 1593 | 1593 | |
| 1594 | - $current_subscription_start_date = date_create( $subscription->modified_date ); |
|
| 1594 | + $current_subscription_start_date = date_create( $subscription->modified_date ); |
|
| 1595 | 1595 | |
| 1596 | - // is the last updated subscription date newer than course start date |
|
| 1597 | - if ( $current_subscription_start_date > $course_start_date ) { |
|
| 1596 | + // is the last updated subscription date newer than course start date |
|
| 1597 | + if ( $current_subscription_start_date > $course_start_date ) { |
|
| 1598 | 1598 | |
| 1599 | - return false; |
|
| 1599 | + return false; |
|
| 1600 | 1600 | |
| 1601 | - } |
|
| 1601 | + } |
|
| 1602 | 1602 | |
| 1603 | - } |
|
| 1603 | + } |
|
| 1604 | 1604 | |
| 1605 | - return $was_user_added_without_subscription; |
|
| 1606 | - } |
|
| 1605 | + return $was_user_added_without_subscription; |
|
| 1606 | + } |
|
| 1607 | 1607 | |
| 1608 | 1608 | }// end Sensei_WC |