Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JarrydLong/e7ebff0f4d2d1362798ed0fc04cd5eee to your computer and use it in GitHub Desktop.
Save JarrydLong/e7ebff0f4d2d1362798ed0fc04cd5eee to your computer and use it in GitHub Desktop.
<?php //do not copy
/**
* Gets the next payment date and marks that as the expiration date for users.
*
* The "next payment" value is an estimate based on the billing cycle of the subscription and the last order date. It may be off from the actual recurring date set at the gateway, especially if the subscription was updated at the gateway.
*
* Remove the // on line 40 to run the update query.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function mypmpro_set_expiration_dates() {
if ( isset( $_REQUEST['set_expiration_dates'] ) ) {
global $wpdb;
$users = $wpdb->get_results( "SELECT * FROM $wpdb->pmpro_memberships_users WHERE `status` = 'active'" );
if ( $users ) {
foreach( $users as $user ) {
$next = pmpro_next_payment( $user->user_id, array( 'success', 'cancelled', '' ) );
if( $next !== FALSE ) {
echo "User ".$user->user_id." will expire on ".date( 'Y-m-d H:i:s', $next )."<br/>";
//They will renew and we need to set an expiration date
$sqlQuery = "UPDATE $wpdb->pmpro_memberships_users SET enddate = '".date( 'Y-m-d H:i:s', $next )."' WHERE user_id = '".$user->user_id."'";
/**
* Remove the comment for the next line when you're sure that the
* expiration dates match up for your users
*/
// $wpdb->query( $sqlQuery );
}
}
exit();
}
}
}
add_action( 'init', 'mypmpro_set_expiration_dates' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment