After some digging around the code I found the solution:
function _dbg_log( $data ) {
// error_log( print_r( $data, true ) );
}
function get_membership_roles() {
return array(
// <name of item> => <slug of wordpress user role>
"Class Membership" => "class_member_gp",
"Centre Membership" => "centre_member_gp",
"Study Membership" => "study_member_gp",
"Great Benefactor" => "great_benefactor_gp",
);
}
function edit_member_role( $method, $subscription ) {
$roles = array(
// <name of item> => <slug of wordpress user role>
"Class Membership" => "class_member_gp",
"Centre Membership" => "centre_member_gp",
"Study Membership" => "study_member_gp",
"Great Benefactor" => "great_benefactor_gp",
);
$user_id = $subscription->get_customer_id();
$mem_product = get_the_title( $subscription->get_product_id() );
_dbg_log( "Called edit_member_role with {$method}, {$user_id}, {$mem_product}" );
$user = get_userdata( $user_id );
if ( $user == false ) {
return;
}
_dbg_log( "edit_member_role found a user with ID {$user_id}" );
if ( !array_key_exists( $mem_product, $roles ) ) {
_dbg_log( "edit_member_role did not find a user role for product {$mem_product}" );
return;
}
_dbg_log( "edit_member_role found a user role for product {$mem_product} => {$roles[$mem_product]}. Using {$method}" );
$user->$method( $roles[$mem_product] );
}
/*
add_action( 'getpaid_new_subscription', 'set_user_role_on_new_subscription', 10, 1 );
function set_user_role_on_new_subscription( $subscription ) {
_dbg_log( 'Called set_user_role_on_new_subscription' );
edit_member_role( 'add_role', $subscription );
}
*/
add_action( 'getpaid_subscription_status_changed', 'set_user_role_on_change_subscription_status', 10, 3 );
function set_user_role_on_change_subscription_status( $subscription, $from, $to ) {
_dbg_log( 'Called set_user_role_on_change_subscription_status' );
if ( in_array( $to, array('active', 'trialling')) ) {
$method = 'add_role';
} else {
$method = 'remove_role';
}
edit_member_role( $method, $subscription );
}
// do we need to hook into both of these, or just getpaid_delete_subscription?
add_action( 'getpaid_delete_subscription', 'set_user_role_on_delete_subscription', 10, 1 );
add_action( 'getpaid_authenticated_admin_action_subscription_manual_delete', 'set_user_role_on_delete_subscription', 10, 1 );
function set_user_role_on_delete_subscription( $subscription ) {
_dbg_log( 'Called set_user_role_on_delete_subscription' );
edit_member_role( 'remove_role', $subscription );
}
/*
add_action( 'getpaid_update_subscription', 'set_user_role_on_update_subscription', 10, 1 );
function set_user_role_on_update_subscription( $subscription ) {
}
*/
-
This reply was modified 1 year, 5 months ago by kmcwepc.