Last active
August 15, 2019 23:39
-
-
Save LMNTL/ef78c69343a5daac9ba9d6e61687bc52 to your computer and use it in GitHub Desktop.
preserve start date when PMPro WooCommerce subscriptions go into "on hold" status
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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