• Resolved hektorjg

    (@hektorjg)


    I want to know how i can upgrade manually any subscription.
    I have few online courses, I show the free users part of my web.
    When they finish, i ask them to pay or share with facebook, if they share I want to manually upgrade them with php function but im not able to find it.
    Thanks

    https://ww.wp.xz.cn/plugins/membership/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi @hektorjg,

    Thanks for your question! 🙂

    You could use something like this:

    function myaddusertolevel( $user_id, $level_id ) {
    	if ( class_exists( 'M_Membership' ) ) {
    		$member = new M_Membership($user_id);
    		$member->add_level( $level_id );
    	}
    }

    You can then call that function like so, for example:
    myaddusertolevel( 1, 2);

    That would add user with ID of 1 to your Membership Access Level with an ID of 2.

    How’s that work for ya? 🙂

    Cheer,
    David

    Thread Starter hektorjg

    (@hektorjg)

    Ok, I changed the code a bit, because i want the current user who access this page to be upgraded, so when i call the function i ask to get current user instead of upgradin the user_id you put in the function.

    function myaddusertolevel( $user_id, $level_id ) {
    if ( class_exists( ‘M_Membership’ ) ) {
    $member = new M_Membership($user_id);
    $member->add_level( $level_id );
    }
    }

    $user_id = get_current_user_id();
    myaddusertolevel( $user_id , 3 );
    (I add them to level_id=3)

    The problem now is: The user is in both level_id 1 and 3, and the content restriction for only level_id 3 is not working because (i think) they still belong to level 1.

    So I would like to know how to improve the code so deletes current subscription and upgrades to the one i put.

    Thanks a lot for the help

    Hi @hektorjg,

    A bit of background information I should have mentioned earlier, if you open the following file:
    /wp-content/plugins/membership/classes/Membership/Model/Member.php

    You’ll see all the class methods Membership provides for interacting with levels such as the following:

    public function drop_level( $fromlevel_id )

    That’s used to drop a level from a member.

    Given that, you could perhaps adjust the code like so:

    function moveusertolevel( $user_id, $level_id_to, $level_id_from ) {
    	if ( class_exists( 'M_Membership' ) ) {
    		$member = new M_Membership($user_id);
    		$member->add_level( $level_id_to );
    		$member->drop_level( $level_id_from );
    	}
    }
    
    $user_id = get_current_user_id();
    moveusertolevel( $user_id , 3, 1 );

    That would basically move a user from level id 1 to level id 3.

    Just to add too, with all of this, you can actually see that at its core, Membership is capable of multiple simultaneous access levels and subscriptions.

    It’s actually very powerful in that way, just a bit confusing if you don’t realize that’s how it works. 🙂

    Hope that helps.

    Cheers,
    David

    Thread Starter hektorjg

    (@hektorjg)

    Thanks for all the help, Its helping me a lot, but It’s not working.
    When I use the “new” code, they still belong in the first level.
    I mean:
    Membership level
    Before the code (Free user)
    After the code (Free user, Paid user)
    I cant delete the Free user level with the drop_level. But you mean you can still work with it, I would like to ask for more help :S

    My main problem for this is:
    Two different URL groups

    • for non-suscribers they wont be able to look any of the links
    • if they are Free users they will be able to see URL group 1
    • if they are Paid user they can see URL group 1 and URL group 2

    After the code with my actual configuration, “officially” they belong Free user and Paid member, so they could access Urlgrp 1 from free user and Urlgrp 1&2 from paid user level, but actually they only can access Urlgrp 1.
    How do you think I have to change, whether the code or the restrictions, to make this configuration happen?

    Hi @hektorjg,

    Thanks for your reply. I’m not seeing any reason that the level wouldn’t have been dropped, which would sort this whole issue, I think.

    Could you please verify you’ve got the right level_id?

    Also, this is indeed an access level and not a subscription, correct?

    Further, I only just noticed there’s actually a move method in Membership. The could could be simplified like so:

    function moveusertolevel( $user_id, $level_id_from, $level_id_to ) {
    	if ( class_exists( 'M_Membership' ) ) {
    		$member = new M_Membership($user_id);
    		$member->move_level( $level_id_from, $level_id_to );
    	}
    }
    
    $user_id = get_current_user_id();
    moveusertolevel( $user_id , 1, 2 );

    Perhaps that will just sort it for you?

    Thanks,
    David

    Thread Starter hektorjg

    (@hektorjg)

    Thanks for the help.
    The level_id were correct. From 1 to 3. In addition, the last code you gave me did nothing, the users keep in level_id 1 and they dont become level 3.
    I finally found the solution: you are forced to work with both levels at the same time, as you mentioned before:

    Just to add too, with all of this, you can actually see that at its core, Membership is capable of multiple simultaneous access levels and subscriptions.

    I was blocking the access to the pro features if you are free, and that was my big problem if you want pro and free levels live together in the same user, so I had to “re-formulate” the restrictions so you can access the pro features meanwhile you are free user at the same time.

    So I used one of the previous code, but deleting the “drop_level”

    function addusertolevel( $user_id, $level_id_add ) {
    	if ( class_exists( 'M_Membership' ) ) {
    		$member = new M_Membership($user_id);
    		$member->add_level( $level_id_add );
    	}
    }
    
    $user_id = get_current_user_id();
    addusertolevel( $user_id , 3 );

    Thank you very much for the help.
    Cheers!

    Hi @hektorjg,

    You’re most welcome, glad that’s sorted and thanks for posting back your solution. 🙂

    Cheers,
    David

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Upgrade subscription’ is closed to new replies.