• Resolved stephunique

    (@stephunique)


    Hello PMP

    Here is a new question on a new topic as requested, edited to have my current question.

    I have a question regarding the information provided here: https://www.ads-software.com/support/topic/change-memberships-level-vs-downgrade-user-do-i-need-both/

    The original author of that question asked about changing a user role when someone downgrades. Currently I am testing a subscription plugin where users who sign up are given a new user role additional to what their original user role was. This means they will have two roles at the same time. The problem is, if different user roles are given different capabilities, there will be a conflict. Then when they downgrade because their subscription expired and they didn’t want to renew, they will get the website’s default role, which is not what I want.

    My question is this: In PMP (with the code above, or some other way), is it possible to change user roles to different roles (ie, replace their original user role, rather than add a new one, so the user has only one role to avoi d conflicts) for different subscriptions?types?(not membership?levels) after their subscription expires, then when the user downgrades, they get their orginal role back?

    For example: if I let people sign up as a student using a normal sign up process outside of PMP, then let them upgrade to a subscription via PMP to become a Premium Student, and other people to sign up as teachers via a normal sign up process outside of PMP, who can then upgrade to a subscription via PMP to become a Premium Teacher, the two types of subscriptions (student vs teachers) are not the same and each cannot be downgraded to be each other since their respective roles are not related and not higher level versions of each other.

    If a Premium Student subscription expires, I’d like them to downgrade to back to their original student role, and when a Premium Teacher subscription expires, I’d like them to downgrade to their original teacher role, the two roles are not the same, and not the website default one.

    Thank you

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support Jarryd Long

    (@jarryd-long)

    Hi there, thank you for reaching out to the Paid Memberships Pro team.

    Changing a role based on the member’s membership level would be best suited by using the PMPro Roles Add On (https://www.paidmembershipspro.com/add-ons/pmpro-roles/)

    You can assign a role to each level and when a member expires or cancels their membership they will use the core WP default role set under Settings > General

    Kind Regards,
    Jarryd
    Support Manager at Paid Memberships Pro

    Thread Starter stephunique

    (@stephunique)

    Hello Jarryd,

    Thank you for the suggestion. I am already aware of the add-on. But upon membership expiration, the user role changes to the website default role, correct?

    Is there a way to let them get back the role they had before signing up to a membership rather than gain the website’s default role, because that role might not always be the same as the website default one. Like in my example, a student who signs up to premium student membership and gains a premium student role, should be downgraded to a student role, and a teacher who signs up to a premium teacher role should be downgraded to a premium teacher role upon membership expiry. The two are not the same as each other, and are not the same as the website default role, which could simply be “Subscriber”.

    In the article you linked to about the Roles addon, there is this line of information:

    “Runs after a user’s role has changed, providing access to the user object, their previous levels, old roles, and new roles for additional processing or logic.”

    It does not say how to take advantage of that though. Could you point me in the right direction as to how I can take advantage of knowing what their previous role was and give them that role when their new membership role expires?

    • This reply was modified 4 months, 4 weeks ago by stephunique.
    Plugin Support Jarryd Long

    (@jarryd-long)

    Unfortunately there isn’t a filter in place for us to turn off the process of changing the user’s role back to the default role.

    The code recipe shared previously could be adjusted however I haven’t tested this with the Roles Add On to see how they behave together. Additional development is required to make this recipe work for your use case.

    function my_pmpro_change_role_when_expire( $user_id, $level_id ) {

    $wp_user_object = new WP_User( $user_id );

    // Bail for admins.
    if ( in_array( 'administrator', $wp_user_object->roles ) ) {
    return;
    }

    //Get the role of the level they are expiring from

    $roles = PMPRO_Roles::get_roles_for_level( $level_id );
    if( is_array( $roles ) && ! empty( $roles ) ){
    foreach( $roles as $role_key => $role_name ){
    $wp_user_object->add_role( $role_key );
    }
    }


    return $user_id;

    }
    add_filter( 'pmpro_membership_post_membership_expiry', 'my_pmpro_change_role_when_expire', 10, 2 );
    Thread Starter stephunique

    (@stephunique)

    Okay I see, thank you for letting me know.

    Can you tell me what the code you posted in your last reply does? I am not a programmer so I am not sure what it is for and what it dose since your reply didn’t specifically say what it is.

    Thank you

    Plugin Support Jarryd Long

    (@jarryd-long)

    This code recipe runs when a member’s membership expires – it uses the level ID that has expired and gets the role associated with it. It then assigns the role back to that user.

    Kind Regards,
    Jarryd
    Support Manager at Paid Memberships Pro

    Thread Starter stephunique

    (@stephunique)

    Hello Jarryd,

    Thank you for your reply. I am still a little confused how to achieve my goal. I will have more than one type (or groups) of memberships on my website, in my previous example, a group for students and a group for teachers. Each group should have more than one access level, eg a bronze, silver and gold student level and a bronze, silver and gold teacher level. Each level is given their own unique custom made user role. This is because I will be granting capabilities based on user role only. My goal is that when a subscriber for a given membership type and level (eg students gold level with student gold role, or teachers gold level with teachers gold role) expire, they are downgraded to a different user role.

    According to code recipe #2 here: https://www.paidmembershipspro.com/change-membership-level-on-cancellation-or-expiration/, it seems that you can allocated a single role to ALL memberships that expire. Obviously that is not what I want because students and teachers each need to be downgraded to their respective roles, they cannot both have the same roles. So I don’t think this would work.

    In your last code recipe in this thread, you said the code “uses the level ID that has expired and gets the role associated with it. It then assigns the role back to that user.” Maybe I misunderstood you but if you get the role associated with the membership level that just expired, and allocated that back to the user, then wouldn’t they have the same user role as when they were subscribed to a membership? And if so, there would be no downside to them for downgrading or letting their membership expire and no incentive to upgrade since the downgraded membership role is exactly the same as the upgraded one, but cheaper. If this is true, then this also does not solve my problem.

    Essentially what I want is when a user who signs up to say a Student Gold membership, they get a Student Gold role that lets them do whatever I grant users with student gold roles do. When their membership expires, they are downgraded to say Student Bronze membership with a Student Bronze role that lets them do what Student Bronze user roles do, but not what Student Gold users do. At the same time, people with a Teachers Gold membership gets a Teachers Gold role and is granted capabilities that all Teachers Gold roles are given. When Teachers Gold memberships expire, they are given Teachers Bronze user memberships/roles and lose access to Teachers Gold user role capabilities. Student Gold and Teachers Gold roles have entirely different capabilities and can not be given a default same role upon expiry.

    Plugin Support Jarryd Long

    (@jarryd-long)

    Thank you for your feedback. The recipe gets the role associated with the level that expires at that point in time and then assigns back the role to that user.

    While we can offer development advice as I have done on the above, further development and customizations to this code recipe would require a developer if you would like to pursue this further.

    If you are not comfortable with making these changes yourself, we recommend posting a project on https://jobs.wordpress.net or reaching out to a developer in your local area for further assistance.

    Kind Regards,
    Jarryd
    Support Manager at Paid Memberships Pro

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.