From cb9556ff467ccac931ceca16e8b58f50e9bfc857 Mon Sep 17 00:00:00 2001 From: william dutton Date: Sun, 9 Oct 2016 17:44:33 +1100 Subject: [PATCH 1/3] Calculate last_payment for imported subscriptions A workaround and viable fix for issue #142 added a filter to make up a date when the date return from the parent function is 0 and the subscription was imported. This will then allow switch subscriptions to work. Note: this may cause issues with free trials where no payment has been paid, testing is required to ensure it does not cause interference on those imported subscriptions. --- wcs-importer-exporter.php | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/wcs-importer-exporter.php b/wcs-importer-exporter.php index 10dadf5..c50115e 100644 --- a/wcs-importer-exporter.php +++ b/wcs-importer-exporter.php @@ -50,21 +50,46 @@ public static function init() { */ public static function setup_importer() { + if ( class_exists( 'WC_Subscriptions' ) && version_compare( WC_Subscriptions::$version, '2.0', '>=' ) ) { + add_filter('woocommerce_subscription_get_' . 'last_payment' . '_date', array(WCS_Importer_Exporter::class, 'last_payment_calculation'), 10, 3); + } if ( is_admin() ) { - if ( class_exists( 'WC_Subscriptions' ) && version_compare( WC_Subscriptions::$version, '2.0', '>=' ) ) { + if (class_exists('WC_Subscriptions') && version_compare(WC_Subscriptions::$version, '2.0', '>=')) { self::$wcs_exporter = new WCS_Export_Admin(); self::$wcs_importer = new WCS_Import_Admin(); + } else { add_action( 'admin_notices', __CLASS__ . '::plugin_dependency_notice' ); } } } + /** + * This is to calculate last_payment when the last payment not set by import + * this is done by taking next interval and removing twice. + * Only change from 0 if it was an imported subscription + * + * @param $date + * @param $subscription @class WC_Subscription + * @param $timezone + * @return $date or date of last payment calulation + */ + public static function last_payment_calculation($date, $subscription, $timezone ) { + if ($date == 0 && "importer" === $subscription->__get("created_via")) { + $next_payment = strtotime($subscription->get_date('next_payment')); + $next_interval = wcs_add_time( $subscription->billing_interval, $subscription->billing_period, $next_payment ); + $last_payment_cal = $next_payment - ($next_interval - $next_payment); + return gmdate( 'Y-m-d H:i:s',$last_payment_cal); + } + return $date; + } + /** * Include Docs & Settings links on the Plugins administration screen * * @since 1.0 * @param mixed $links + * @return array */ public static function action_links( $links ) { From 858f0ec3e3ecdbaf8a4f23306b0df498cc472e6a Mon Sep 17 00:00:00 2001 From: william dutton Date: Sun, 9 Oct 2016 17:50:50 +1100 Subject: [PATCH 2/3] correct whitespace removal --- wcs-importer-exporter.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/wcs-importer-exporter.php b/wcs-importer-exporter.php index c50115e..bf9670c 100644 --- a/wcs-importer-exporter.php +++ b/wcs-importer-exporter.php @@ -54,10 +54,9 @@ public static function setup_importer() { add_filter('woocommerce_subscription_get_' . 'last_payment' . '_date', array(WCS_Importer_Exporter::class, 'last_payment_calculation'), 10, 3); } if ( is_admin() ) { - if (class_exists('WC_Subscriptions') && version_compare(WC_Subscriptions::$version, '2.0', '>=')) { + if ( class_exists( 'WC_Subscriptions' ) && version_compare( WC_Subscriptions::$version, '2.0', '>=' ) ) { self::$wcs_exporter = new WCS_Export_Admin(); self::$wcs_importer = new WCS_Import_Admin(); - } else { add_action( 'admin_notices', __CLASS__ . '::plugin_dependency_notice' ); } From c4f145fae18ff6c82d36ea428d094a935a1fbde8 Mon Sep 17 00:00:00 2001 From: william dutton Date: Tue, 11 Oct 2016 21:28:58 +1000 Subject: [PATCH 3/3] #146 code review corrections, whitespace change and clean up --- wcs-importer-exporter.php | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/wcs-importer-exporter.php b/wcs-importer-exporter.php index bf9670c..9b9a1ae 100644 --- a/wcs-importer-exporter.php +++ b/wcs-importer-exporter.php @@ -50,9 +50,8 @@ public static function init() { */ public static function setup_importer() { - if ( class_exists( 'WC_Subscriptions' ) && version_compare( WC_Subscriptions::$version, '2.0', '>=' ) ) { - add_filter('woocommerce_subscription_get_' . 'last_payment' . '_date', array(WCS_Importer_Exporter::class, 'last_payment_calculation'), 10, 3); - } + add_filter( 'woocommerce_subscription_get_last_payment_date', array(__CLASS__, 'last_payment' ), 10, 3 ); + if ( is_admin() ) { if ( class_exists( 'WC_Subscriptions' ) && version_compare( WC_Subscriptions::$version, '2.0', '>=' ) ) { self::$wcs_exporter = new WCS_Export_Admin(); @@ -64,21 +63,22 @@ public static function setup_importer() { } /** - * This is to calculate last_payment when the last payment not set by import - * this is done by taking next interval and removing twice. - * Only change from 0 if it was an imported subscription - * - * @param $date - * @param $subscription @class WC_Subscription - * @param $timezone - * @return $date or date of last payment calulation - */ - public static function last_payment_calculation($date, $subscription, $timezone ) { - if ($date == 0 && "importer" === $subscription->__get("created_via")) { - $next_payment = strtotime($subscription->get_date('next_payment')); + * This is to calculate last_payment when the last payment not set by import + * this is done by taking next interval and removing twice. + * Only change from 0 if it was an imported subscription + * + * @param string Or 0 $date A MySQL formatted date/time string in GMT/UTC timezone. + * @param $subscription WC_Subscription + * @param $timezone The timezone of the $datetime param, either 'gmt' or 'site'. Default 'gmt'. + * @return $date OR date of last payment calulation + */ + public static function last_payment($date, $subscription, $timezone ) { + if ( 0 == $date && "importer" === $subscription->created_via ) { + $next_payment = wcs_date_to_time( $subscription->get_date( 'next_payment', $timezone ) ); $next_interval = wcs_add_time( $subscription->billing_interval, $subscription->billing_period, $next_payment ); - $last_payment_cal = $next_payment - ($next_interval - $next_payment); - return gmdate( 'Y-m-d H:i:s',$last_payment_cal); + $last_payment = $next_payment - ( $next_interval - $next_payment ); + + return gmdate( 'Y-m-d H:i:s', $last_payment ); } return $date; }