Skip to content

Instantly share code, notes, and snippets.

@LMNTL
Last active August 15, 2019 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LMNTL/ef78c69343a5daac9ba9d6e61687bc52 to your computer and use it in GitHub Desktop.
Save LMNTL/ef78c69343a5daac9ba9d6e61687bc52 to your computer and use it in GitHub Desktop.
preserve start date when PMPro WooCommerce subscriptions go into "on hold" status
function my_pmprowoo_active_preserve_startdate( $level_id, $user_id )
{
global $wpdb;
if( $level_id != 0 && did_action( "woocommerce_subscription_status_active" ) )
{
$startdate = get_user_meta( $user_id, "pmprowoo_startdate_$level_id", true );
if( !empty($startdate) )
{
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->pmpro_memberships_users SET startdate = %s WHERE user_id = %d AND status = 'active'", $startdate, $user_id ) );
}
}
}
add_action( "pmpro_after_change_membership_level", "my_pmprowoo_active_preserve_startdate", 10, 2 );
function my_pmprowoo_on_hold_save_startdate( $level_id, $user_id )
{
global $wpdb;
$old_level = pmpro_getMembershipLevelForUser($user_id);
if ( $level_id == 0 && did_action( "woocommerce_subscription_status_on-hold" ) && empty(get_user_meta( $user_id, "pmprowoo_startdate_$old_level->id", true )) )
{
update_user_meta( $user_id, "pmprowoo_startdate_$old_level->id", $old_level->startdate );
}
}
add_action( "pmpro_before_change_membership_level", "my_pmprowoo_on_hold_save_startdate", 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment