Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate last_payment for imported subscriptions #146

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions wcs-importer-exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public static function init() {
*/
public static function setup_importer() {

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();
Expand All @@ -60,11 +62,33 @@ 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 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 = $next_payment - ( $next_interval - $next_payment );

return gmdate( 'Y-m-d H:i:s', $last_payment );
}
return $date;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the $timezone is being ignored here, if someone is requesting $subscription->get_date( 'last_payment', 'site' ), this will return an incorrect date. It needs to also check 'gmt' != strtolower( $timezone ) and when that's the case, it should change the $last_payment_cal value to be in the site's timezone using something like:

$last_payment_cal = get_date_from_gmt( $last_payment_cal )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

corrected, the downgrade/upgrade looks at gmt time it seems, but its good to handle both, also updated documentation to match from other locations in subscriptions/woocommerce.

}

/**
* Include Docs & Settings links on the Plugins administration screen
*
* @since 1.0
* @param mixed $links
* @return array
*/
public static function action_links( $links ) {

Expand Down