Skip to content

Instantly share code, notes, and snippets.

@mattallan
Last active November 30, 2021 07:34
Show Gist options
  • Save mattallan/e45730807e019343edbf to your computer and use it in GitHub Desktop.
Save mattallan/e45730807e019343edbf to your computer and use it in GitHub Desktop.
Fix for phoebe's subscriptions where the next payment date was set in the AS before daylight savings took affect.
<?php
/**
* Plugin Name: Prospress Script - Reset next payment dates
* Description: A custom plugin for Phoebe's site that will reset all the next payment dates and push them back 1 minute.
* Author: Matt Allan - Prospress Inc.
* Author URI: http://prospress.com/
* Version: 1.0
*/
function phoeb_reset_next_payment() {
$logger = new WC_Logger();
if ( isset( $_GET['phoebe_reset_dates'] ) ) {
// get all active subscriptions
$subscription_ids = get_posts( array(
'post_status' => 'wc-active',
'post_type' => 'shop_subscription',
'posts_per_page' => '-1',
'fields' => 'ids',
)
);
$updated_subscriptions = get_option( '_phoebe_wcs_subscriptions_updated' , array() );
if ( count( $subscription_ids ) == count( $updated_subscriptions ) ) {
echo "<h1>PROSPRESS - UPDATER FINISHED</h1>";
}
foreach ( $subscription_ids as $subscription_id ) {
if ( in_array( $subscription_id, $updated_subscriptions ) ) {
continue;
}
try {
$subscription = wcs_get_subscription( $subscription_id );
$next_payment = $subscription->get_time( 'next_payment' );
$new_next_payment = gmdate( 'Y-m-d H:i:s', $next_payment + 60 ); // add one minute
$subscription->update_dates( array( 'next_payment' => $new_next_payment ) );
$updated_subscriptions[] = $subscription_id;
update_option( '_phoebe_wcs_subscriptions_updated', $updated_subscriptions );
} catch( Exception $e ) {
$logger->add( 'phoebe-date-fixer', 'failed updating ' . $subscription_id . ' error: ' . print_r( $e->getMessage(), true ) ); // log any issues with updating
$updated_subscriptions[] = $subscription_id;
update_option( '_phoebe_wcs_subscriptions_updated', $updated_subscriptions );
}
}
}
}
add_action( 'admin_footer', 'phoeb_reset_next_payment' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment