PriMoThemes — now s2Member® (official notice)

This is now a very OLD forum system. It's in READ-ONLY mode.
All community interaction now occurs at WP Sharks™. See: new forums @ WP Sharks™

Display account variables in theme

s2Member Plugin. A Membership plugin for WordPress®.

Display account variables in theme

Postby theone » April 25th, 2011, 9:38 pm

I want to display the number of remaining days a subscriber has on my Members Options page.

Can this be done? If so, how would I do it?

I'm not very good at PHP so if someone could throw some code at me that would be greatly appreciated :)
User avatar
theone
Registered User
Registered User
 
Posts: 47
Joined: October 3, 2010

Re: Display account variables in theme

Postby Cristián Lávaque » April 25th, 2011, 10:27 pm

You can work with this to figure that out:

Code: Select all
get_user_option('s2member_auto_eot_time'S2MEMBER_CURRENT_USER_ID); 


Let me know if that helps. :)
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Display account variables in theme

Postby theone » April 26th, 2011, 12:25 am

That function doesn't return anything for me......

Code: Select all
<?php echo get_user_option('s2member_auto_eot_time'S2MEMBER_CURRENT_USER_ID); ?>
Last edited by Cristián Lávaque on April 26th, 2011, 1:22 am, edited 1 time in total.
Reason: Improve code readability. http://www.primothemes.com/forums/viewtopic.php?f=36&t=2780
User avatar
theone
Registered User
Registered User
 
Posts: 47
Joined: October 3, 2010

Re: Display account variables in theme

Postby Cristián Lávaque » April 26th, 2011, 1:22 am

You're an admin, make sure you try with a account that has an EOT time.
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Display account variables in theme

Postby theone » April 26th, 2011, 10:46 am

I tested on a Level 1 s2member account and still no return. Why would that be?
User avatar
theone
Registered User
Registered User
 
Posts: 47
Joined: October 3, 2010

Re: Display account variables in theme

Postby Cristián Lávaque » April 26th, 2011, 11:25 am

Does that account have an auto_eot_time value? Check in the account's profile WP Admin -> Users -> [user] -> Automatic EOT Time
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Display account variables in theme

Postby theone » April 26th, 2011, 12:59 pm

Ah, yes, that was problem as I had manually created the user.

Now I get a return but it shows something like '1303840859'.

What do these numbers mean?

Also, am I correct to assume that there's no defined variable to output the time remaining and that some additional programing would be required to translate the EOT time into the number of remaining days?
User avatar
theone
Registered User
Registered User
 
Posts: 47
Joined: October 3, 2010

Re: Display account variables in theme

Postby Cristián Lávaque » April 26th, 2011, 1:23 pm

That is a Unix timestamp, which is in seconds.

You can find out the remaining time substracting that time from the current time and turn the seconds into days. Something like

Code: Select all
$remaining_days = (time() - get_user_option('s2member_auto_eot_time')) / 86400


http://php.net/time
http://www.google.com/search?q=1+day+to+seconds

I hope that helps. :)
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Display account variables in theme

Postby theone » April 26th, 2011, 4:56 pm

Thanks but I wasn't able to get it to work. I still get unix timestamps. Shows something like....

'15090.9007292'

Code: Select all
<?php
$remaining_days = (time() - get_user_option('s2member_auto_eot_time')) / 86400;

echo "<p>$remaining_days</p>";

?>


My brain isn't good at comprehending these types of equations. I've tried many time to learn PHP but some people (myself) are just not made to write code.

I guess I'll just have to imagine how convenient it would have been to be able to allow my customers to know how many days remain in their subscription.
User avatar
theone
Registered User
Registered User
 
Posts: 47
Joined: October 3, 2010

Re: Display account variables in theme

Postby Cristián Lávaque » April 26th, 2011, 10:04 pm

I'm very sorry, I put the times backwards. :P

Could you try this?

Code: Select all
$remaining_days = floor((get_user_option('s2member_auto_eot_time') - time()) / 86400);  


I added floor to round the fractions down. http://php.net/floor
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Display account variables in theme

Postby theone » April 27th, 2011, 12:59 am

I tried that but it returns a negative integer '-15092'
User avatar
theone
Registered User
Registered User
 
Posts: 47
Joined: October 3, 2010

Re: Display account variables in theme

Postby Cristián Lávaque » April 27th, 2011, 1:13 am

Are you trying it with the account that does have an EOT?

Could you tell me what this outputs?

Code: Select all
<?php echo get_user_option('s2member_auto_eot_time'); ?>
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Display account variables in theme

Postby theone » April 27th, 2011, 10:45 am

Your right, there was no EOT but I did set one (for one week from now) and everything works great.

Thanks so much!

JUST ONE THING - How do I prevent a negative integer from showing if someone is beyond their EOT? I just want it to say 0. Any ideas on that?

ALSO, I have s2member set to demote a member to 'level 0'. We determined that an EOT must be set and that the user must be at least "level 1" for the data to return properly. So, how would I fix it so that if the user = 'level 0' to always display '0' and not a negative number? Because right now, if a user is a "subscriber" and no EOT is set I get something like '-15092'. I want to replace that with '0'. (I hope that was clear enough).

Im thinking that some sort of 'if / else' statement would do it. But, again me and PHP don't get along very well. I'd appreciate it if you could hold me hand through this one :)
User avatar
theone
Registered User
Registered User
 
Posts: 47
Joined: October 3, 2010

Re: Display account variables in theme

Postby theone » April 27th, 2011, 3:39 pm

I actually managed to figure this out myself (against all odds) :)

This will allow me to display a 'Subscription has expired!' message if the amount is anything less than 0.

Code: Select all
<?php
$remaining_days = floor((get_user_option('s2member_auto_eot_time') - time()) / 86400);
if ($remaining_days<="0")
  echo "Subscription has expired!";
else
  echo "$remaining_days";
  print ' day' . ($remaining_days == 1 ? '' : 's');
  print ' left';
?>


BUT, what if I want to be able to tell the difference between an account that has a few hours left and one that has expired. How would I do that?
User avatar
theone
Registered User
Registered User
 
Posts: 47
Joined: October 3, 2010

Re: Display account variables in theme

Postby Cristián Lávaque » April 27th, 2011, 5:03 pm

Well done for solving it! :)

If there's just one day left, you can assume he has only ours before it's up. Here's how I'd write it:

Code: Select all
if ($days = get_user_option('s2member_auto_eot_time'))
{
    $days = floor(($days - time()) / 86400);

    if ($days < 1)
        echo 'Your subscription has expired.';
    elseif ($days == 1)
        echo 'You have a few hours left, renew now!';
    else
        echo 
'You have ', $days, ' days left.';
}
 


Does it help?
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Display account variables in theme

Postby theone » April 27th, 2011, 6:14 pm

It makes sense but trying to execute the script gets no return.

This is with EOT set mind you.
User avatar
theone
Registered User
Registered User
 
Posts: 47
Joined: October 3, 2010

Re: Display account variables in theme

Postby Cristián Lávaque » April 27th, 2011, 6:26 pm

I see... Does it work if you just have this?

Code: Select all
$days floor((get_user_option('s2member_auto_eot_time') - time()) / 86400);

if (
$days 1)
    echo 
'Your subscriptions has expired.';
else if (
$days == 1)
    echo 
'You have a few hours left, renew now!';
else
    echo 
'You have '$days' days left.'
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Display account variables in theme

Postby theone » April 27th, 2011, 7:02 pm

Ok, I got it to work (had to log out and back in).

Here's the problem I see what that script.

Code: Select all
if ($days < 1) echo 'Your subscription has expired.';

elseif ($days == 1) echo 'You have a few hours left, renew now!';


This actually doesn't work since we are counting in days. Anything from 00:00:00-23:59:59 hours is equal to 0. Anything from 24:00:00-47:59:59 is equal to '1'.

This creates a problem because if there are 17 hours left that person is going to see "Your subscription has expired.".

If the person has 47 hours left they will see "You have a few hours left, renew now!."

Do you understand the problem? I hope I was able to explain that clearly. Any ideas on a fix for this?
User avatar
theone
Registered User
Registered User
 
Posts: 47
Joined: October 3, 2010

Re: Display account variables in theme

Postby Cristián Lávaque » April 27th, 2011, 8:01 pm

Yes, I understand. Then instead of rounding down with floor, round it up with ceil.
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Display account variables in theme

Postby theone » April 27th, 2011, 8:29 pm

That's the ticket! Thanks so much for the assistance, greatly appreciated!

For anyone who stumbles upon this thread and wants to cut to the chase.......

Code: Select all
<?php
       if ($days = get_user_option('s2member_auto_eot_time'))
    {
        $days = ceil(($days - time()) / 86400);

        if ($days < 1)
            echo 'Your subscription has expired.';
        elseif ($days == 1)
            echo 'Your subscription will end tomorrow!';
        else
            echo 'You have ', $days, ' days left.';
    }
?>
User avatar
theone
Registered User
Registered User
 
Posts: 47
Joined: October 3, 2010

Re: Display account variables in theme

Postby theone » April 27th, 2011, 8:37 pm

Just one more thing. How can I handle a member that has been demoted and no longer has an EOT? Right now there is no return for members without a EOT. I'd like to say "Your subscription has expired!" for those without an EOT.
User avatar
theone
Registered User
Registered User
 
Posts: 47
Joined: October 3, 2010

Re: Display account variables in theme

Postby Cristián Lávaque » April 27th, 2011, 10:42 pm

I'm glad that did what you want. For those without EOT, try this (notice the else part at the end):

Code: Select all
if ($days get_user_option('s2member_auto_eot_time'))
{
    
$days ceil(($days time()) / 86400);

    if (
$days 1)
        echo 
'Your subscription has expired.';
    elseif (
$days == 1)
        echo 
'Your subscription will end tomorrow!';
    else
        echo 
'You have '$days' days left.';
} else {
    echo 
'Your subscription has expired.';
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Display account variables in theme

Postby theone » April 27th, 2011, 11:38 pm

THANKS! That is precisely what I needed.

This will really help clarify my members options page.
User avatar
theone
Registered User
Registered User
 
Posts: 47
Joined: October 3, 2010

Re: Display account variables in theme

Postby Cristián Lávaque » April 28th, 2011, 12:43 am

Good. :)
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Display account variables in theme

Postby theone » April 29th, 2011, 8:54 pm

Cristián Lávaque wrote:Yeah, it makes sense, but then you also need to figure out which of the two you're dealing with so you know if you are talking about 1 or 3 months.


Ok, first off, how do we call on a users last payment? And then what? do we check it against a null value?
User avatar
theone
Registered User
Registered User
 
Posts: 47
Joined: October 3, 2010

Next

Return to s2Member Plugin

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 2 guests

cron