How can I detect if user has subscribed Memberpress product already
Asked Answered
C

3

8

How can I detect if user has already bought a memberpress product.

I'm searching something like this:

if(have_subscription){ 
   //code to add button
}else{
   // code to add something else
}
Cabby answered 20/4, 2015 at 9:57 Comment(0)
P
10

This should be pretty straight forward using MemberPress' built in capabilities:

if(current_user_can('memberpress_authorized')) {
  // Do authorized only stuff here
}
else {
  // Do unauthorized stuff here
}

MemberPress also adds capabilities for each MemberPress Membership so you could also do something like this:

if(current_user_can('memberpress_product_authorized_123')) {
  // Do authorized only stuff here
}
else {
  // Do unauthorized stuff here
}

In the second example the number 123 is the id of the MemberPress membership.

Playreader answered 20/4, 2015 at 18:55 Comment(2)
is there a list womewhere with what capabillities that are available for use?Rossner
Any way to check the active plan? Or return "123"?Hennessy
T
10

The answer from 2015 didn't work for me but is a top search result. I thought I should share the result of my search here for others in the future.

Also, I think that "product_authorized" capability only checked if a purchase was made, not verifying the expiration date.

So here is how MemberPress determines if active, inactive, or none:

$mepr_user = new MeprUser( $user_id );

if( $mepr_user->is_active() ) {
    // Active
}else if($mepr_user->has_expired()) {
    // Expired
}else {
    // Never a member
}

has_expired() can return true even if the user is active from a separate transaction so don't rely on that alone.

If you need to check a specific membership you can use the following:

$user_id = 123;
$membership_id = 5000;

$mepr_user = new MeprUser( $user_id );

$mepr_user->is_already_subscribed_to( $membership_id ); // true or false

// OR

$mepr_user->is_active_on_membership( $membership_id ); // true or false

is_already_subscribed_to() accepts only a product id

is_active_on_membership() accepts any of: product id, MeprProduct, MeprTransaction, or MeprSubscription

You can also get all of a user's active subscriptions with:

$mepr_user->active_product_subscriptions( 'ids' ); // return array of ids
Tea answered 26/2, 2021 at 9:56 Comment(0)
W
-5

Open your WordPress website and:

  • Select Plugins > Add New
  • Click on Upload Plugin > Choose file
  • Select the MemberPress plugin from your saved files
  • Click Install Plugin > Activate

You will now find MemberPress has been added to your WordPress dashboard.
This should help.

Welsh answered 1/5, 2021 at 19:23 Comment(1)
This is not an answer. The question is completely unrelated to "how to install Memberpress plugin in Wordpress"Kingly

© 2022 - 2024 — McMap. All rights reserved.