Created
October 5, 2015 01:45
-
-
Save bekarice/d73fd0162b3586588108 to your computer and use it in GitHub Desktop.
WooCommerce Memberships: Trigger role changes for active <-> inactive membership status changes
This file contains 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
<?php | |
/** | |
* Replaces the Site Member role with the Customer role when a membership becomes inactive | |
* | |
* @param \WC_User_Membership $user_membership the user's membership | |
* @param string $old_status the previous membership status | |
* @param string $new_status the new membership status | |
*/ | |
function sv_update_user_role_with_membership( $user_membership, $old_status, $new_status ) { | |
$user_id = $user_membership->user_id; | |
$wp_user = get_userdata( $user_id ); | |
$roles = $wp_user->roles; | |
// Bail if the member doesn't currently have the Site Member role or is an active member | |
if ( ! in_array( 'site_member', $roles ) || wc_memberships_is_user_active_member( $user_id, $user_membership->plan_id ) ) { | |
return; | |
} | |
$wp_user->remove_role( 'site_member' ); | |
$wp_user->add_role( 'customer' ); | |
} | |
add_action( 'wc_memberships_user_membership_status_changed', 'sv_update_user_role_with_membership', 10, 3 ); | |
/** | |
* Replaces the Customer role with the Site Member role when a membership is reactivated | |
* | |
* @param \WC_User_Membership $user_membership the user's membership | |
* @param string $old_status the previous membership status | |
* @param string $new_status the new membership status | |
*/ | |
function sv_reactivate_member_role( $user_membership, $old_status, $new_status ) { | |
$user_id = $user_membership->user_id; | |
$wp_user = get_userdata( $user_id ); | |
$roles = $wp_user->roles; | |
// Bail if the member currently has the Site Member role, doesn't have the customer role, or is inactive | |
if ( in_array( 'site_member', $roles ) || ! in_array( 'customer', $roles ) || ! wc_memberships_is_user_active_member( $user_id, $user_membership->plan_id ) ) { | |
return; | |
} | |
$wp_user->remove_role( 'customer' ); | |
$wp_user->add_role( 'site_member' ); | |
} | |
add_action( 'wc_memberships_user_membership_status_changed', 'sv_reactivate_member_role', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment