From e867f3fd8fec2bf08dbb2a7ba3b76c4ab9220fb6 Mon Sep 17 00:00:00 2001 From: Nick Williams Date: Mon, 26 Mar 2018 11:13:22 -0600 Subject: [PATCH 1/7] adds functionality to set discount in general settings --- .../settings/class-lp-settings-general.php | 9 +++++++++ inc/course/abstract-course.php | 20 +++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/inc/admin/settings/class-lp-settings-general.php b/inc/admin/settings/class-lp-settings-general.php index 1ffe3055a..fde18f3bf 100644 --- a/inc/admin/settings/class-lp-settings-general.php +++ b/inc/admin/settings/class-lp-settings-general.php @@ -54,6 +54,15 @@ public function get_settings( $section = '', $tab = '' ) { 'type' => 'heading', 'desc' => __( 'Setting up your currency unit and its formatting.', 'learnpress' ) ), + array( + 'title' => __( 'Returning Customer Discount', 'learnpress' ), + 'id' => $this->get_field_name('returning_customer_discount'), + 'type' => 'number', + 'clone' => false, + 'desc' => __( 'Set a percentage to discount the courses by if the its a returning customer (enter zero or leave blank for no discount)', 'learnpress' ), + 'min' => 0, + 'std' => 0 + ), array( 'title' => __( 'Currency', 'learnpress' ), 'id' => 'currency', diff --git a/inc/course/abstract-course.php b/inc/course/abstract-course.php index 3fd263231..5e7a4e61a 100644 --- a/inc/course/abstract-course.php +++ b/inc/course/abstract-course.php @@ -681,10 +681,26 @@ public function get_price() { if ( ! $price /* || 'yes' != $this->get_data('payment') */ ) { $price = 0; } else { - if ( false !== ( $sale_price = $this->get_sale_price() ) ) { + $theUser = learn_press_get_current_user(); + $userCourses = learn_press_get_all_courses(); + $hasPaid = false; + foreach($userCourses as $c){ + $c2 = LP_Course::get_course($c); + if($theUser->has_purchased_course($c) && !($c2->is_free())){ + $hasPaid = true; + } + } + $maybe_discount = LP()->settings->get( 'returning_customer_discount'); + if ($hasPaid && $maybe_discount > 0){ + $price = floatval($price); + $price = $price - (($maybe_discount*$price)/100); + } + $price = floatval( $price ); + $sale_price = $this->get_sale_price(); + if ( is_numeric( $sale_price ) ) { $price = $sale_price; } - } + // @deprecated $price = apply_filters( 'learn_press_course_price', $price, $this ); From 89a71a66664ba0123be2756e49eb1b4c59cdb0a0 Mon Sep 17 00:00:00 2001 From: Nick Williams Date: Mon, 26 Mar 2018 11:44:25 -0600 Subject: [PATCH 2/7] added missing parentheses --- inc/course/abstract-course.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/course/abstract-course.php b/inc/course/abstract-course.php index 5e7a4e61a..40e746972 100644 --- a/inc/course/abstract-course.php +++ b/inc/course/abstract-course.php @@ -700,7 +700,7 @@ public function get_price() { if ( is_numeric( $sale_price ) ) { $price = $sale_price; } - + } // @deprecated $price = apply_filters( 'learn_press_course_price', $price, $this ); From ea5d4aae411e3e643bab7b51be87d19d34b8ccda Mon Sep 17 00:00:00 2001 From: Nick Williams Date: Mon, 26 Mar 2018 11:48:15 -0600 Subject: [PATCH 3/7] change the id for the discount setting meta box --- inc/admin/settings/class-lp-settings-general.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/admin/settings/class-lp-settings-general.php b/inc/admin/settings/class-lp-settings-general.php index fde18f3bf..397cf81f5 100644 --- a/inc/admin/settings/class-lp-settings-general.php +++ b/inc/admin/settings/class-lp-settings-general.php @@ -56,7 +56,7 @@ public function get_settings( $section = '', $tab = '' ) { ), array( 'title' => __( 'Returning Customer Discount', 'learnpress' ), - 'id' => $this->get_field_name('returning_customer_discount'), + 'id' => 'returning_customer_discount', 'type' => 'number', 'clone' => false, 'desc' => __( 'Set a percentage to discount the courses by if the its a returning customer (enter zero or leave blank for no discount)', 'learnpress' ), From 1e91102f662151e61f1cd6aaccec2fa6f553a29f Mon Sep 17 00:00:00 2001 From: Nick Williams Date: Tue, 22 May 2018 09:54:57 -0600 Subject: [PATCH 4/7] making my code more gooder: --- inc/course/abstract-course.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/course/abstract-course.php b/inc/course/abstract-course.php index 40e746972..a1ff1e0e3 100644 --- a/inc/course/abstract-course.php +++ b/inc/course/abstract-course.php @@ -685,7 +685,7 @@ public function get_price() { $userCourses = learn_press_get_all_courses(); $hasPaid = false; foreach($userCourses as $c){ - $c2 = LP_Course::get_course($c); + $c2 = learn_press_get_course($c); if($theUser->has_purchased_course($c) && !($c2->is_free())){ $hasPaid = true; } From f8d70615a35e5136dcffa5657602ce05a489da5a Mon Sep 17 00:00:00 2001 From: Nick Williams Date: Tue, 22 May 2018 10:22:59 -0600 Subject: [PATCH 5/7] fixed looping action that cause timeouts and got rid of static method call --- inc/course/abstract-course.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/inc/course/abstract-course.php b/inc/course/abstract-course.php index a1ff1e0e3..f915137b7 100644 --- a/inc/course/abstract-course.php +++ b/inc/course/abstract-course.php @@ -684,11 +684,8 @@ public function get_price() { $theUser = learn_press_get_current_user(); $userCourses = learn_press_get_all_courses(); $hasPaid = false; - foreach($userCourses as $c){ - $c2 = learn_press_get_course($c); - if($theUser->has_purchased_course($c) && !($c2->is_free())){ - $hasPaid = true; - } + if (sizeof($theUser->get_purchased_courses()) > 0){ + $hasPaid = true; } $maybe_discount = LP()->settings->get( 'returning_customer_discount'); if ($hasPaid && $maybe_discount > 0){ From 5c39c86f7ca3038052780442d9cf4d8ca55eea52 Mon Sep 17 00:00:00 2001 From: Nick Williams Date: Fri, 10 Aug 2018 13:09:49 -0600 Subject: [PATCH 6/7] returning user discount updated be fastcgi compatable --- inc/course/abstract-course.php | 7 +------ inc/user/abstract-lp-user.php | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/inc/course/abstract-course.php b/inc/course/abstract-course.php index f915137b7..04415973a 100644 --- a/inc/course/abstract-course.php +++ b/inc/course/abstract-course.php @@ -681,12 +681,7 @@ public function get_price() { if ( ! $price /* || 'yes' != $this->get_data('payment') */ ) { $price = 0; } else { - $theUser = learn_press_get_current_user(); - $userCourses = learn_press_get_all_courses(); - $hasPaid = false; - if (sizeof($theUser->get_purchased_courses()) > 0){ - $hasPaid = true; - } + $hasPaid = learn_press_get_current_user()->is_returning(); $maybe_discount = LP()->settings->get( 'returning_customer_discount'); if ($hasPaid && $maybe_discount > 0){ $price = floatval($price); diff --git a/inc/user/abstract-lp-user.php b/inc/user/abstract-lp-user.php index b92399ff9..7b56a5fa1 100644 --- a/inc/user/abstract-lp-user.php +++ b/inc/user/abstract-lp-user.php @@ -267,6 +267,23 @@ public function __get( $key ) { return $return; } + /** + * + * @return Bool + * Will return true or false depending on wheter the user is a + * returning customer or not + */ + + public function is_returning($currentID){ + $hasPaid = false; + $filter_status = LP_Request::get_string( 'filter-status' ); + $query = $this->get_purchased_courses(); + if(sizeof($query['items']) > 1){ + $hasPaid = true; + } + return $hasPaid; + } + /** * Check if a course is exists then return it's ID. * Try to get it from global. From 5d3968f90f75e66da88686e7004da0a2ffcecabb Mon Sep 17 00:00:00 2001 From: Nick Williams Date: Fri, 10 Aug 2018 13:10:00 -0600 Subject: [PATCH 7/7] returning user discount updated be fastcgi compatable --- inc/user/abstract-lp-user.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/user/abstract-lp-user.php b/inc/user/abstract-lp-user.php index 7b56a5fa1..b27c4da76 100644 --- a/inc/user/abstract-lp-user.php +++ b/inc/user/abstract-lp-user.php @@ -278,12 +278,12 @@ public function is_returning($currentID){ $hasPaid = false; $filter_status = LP_Request::get_string( 'filter-status' ); $query = $this->get_purchased_courses(); - if(sizeof($query['items']) > 1){ + if(sizeof($query['items']) > 0){ $hasPaid = true; } return $hasPaid; } - + /** * Check if a course is exists then return it's ID. * Try to get it from global.