How can I assign/upgrade a membership on the code?

I would like to upgrade members to another membership with just a button. I don't need to have them go thru the upgrade form as this will be a free upgrade(they will only have to agree to terms). I just need a quick simple way to push them to another membership. such as a code to run after a form is submitted. i.e. change_membership($user->ID, 'membershipName'); is there such a thing?

  • Vinod Dalvi

    Hi @victorsxbr,

    Welcome to the WPMU Dev community and thank you for your question.

    To achieve this you can call appropriate Membership plugin functions like following.

    add_subscription( $tosub_id, $tolevel_id = false, $to_order = false, $gateway = 'admin' )
    drop_subscription( $fromsub_id )
    move_subscription( $fromsub_id, $tosub_id, $tolevel_id, $to_order )
    add_level( $tolevel_id )
    drop_level( $fromlevel_id )
    move_level( $fromlevel_id, $tolevel_id, $sub_id = 0 )

    You will find these functions in the following Membership plugin file.

    wp-content/plugins/membership/classes/Membership/Model/Member.php

    Please advise if you have more questions.

    Best Regards,
    Vinod Dalvi

  • Jude

    Hi there @victorsxbr

    Hope you are doing great. Sorry the response took a while longer than expected. Appreciate your patience

    1. Using the gravity forms add on to register users, can I assign a membership to a new registration using the GF form?

    Yes this is possible, if I recall correctly I've seen a couple of threads here where members have gotten it to work smoothly.

    Gravity forms lets me update a usermeta with a value from the form submitted. Can I update anything on the user meta to update the membership

    If you're asking if its possible then the answer is yes. You need to write the AJAX handler that gets the response from your form, validates it and updates the table.

    If you are not comfortable with code, consider our Jobs Board where you can find some good talent that can help you with this.

    Cheers
    Jude

  • Vinod Dalvi

    Hi @victorsxbr,

    I checked the membership plugin code and found the following code which is used to assign the subscription/membership to the user.

    $this->_wpdb->insert( MEMBERSHIP_TABLE_RELATIONS, array(
        'user_id' => $this->ID,
        'level_id' => $tolevel_id,
        'sub_id' => $tosub_id,
        'startdate' => $now,
        'updateddate' => $now,
        'expirydate' => $expires,
        'order_instance' => $level->level_order,
        'usinggateway' => $gateway
    ) );
    // Update users start and expiry meta
    update_user_meta( $this->ID, 'start_current_' . $tosub_id, $start );
    update_user_meta( $this->ID, 'expire_current_' . $tosub_id, $expires_sub );
    update_user_meta( $this->ID, 'using_gateway_' . $tosub_id, $gateway );

    It seems from the above code that you have to update the following usermeta and have to also update the values in the memberhip table m_membership_relationships

    'start_current_' . $tosub_id
     'expire_current_' . $tosub_id
     'using_gateway_' . $tosub_id

    Thanks,
    Vinod Dalvi

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.