Page 1 of 2

Display account variables in theme

PostPosted: April 25th, 2011, 9:38 pm
by theone
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 :)

Re: Display account variables in theme

PostPosted: April 25th, 2011, 10:27 pm
by Cristián Lávaque
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. :)

Re: Display account variables in theme

PostPosted: April 26th, 2011, 12:25 am
by theone
That function doesn't return anything for me......

Code: Select all
<?php echo get_user_option('s2member_auto_eot_time'S2MEMBER_CURRENT_USER_ID); ?>

Re: Display account variables in theme

PostPosted: April 26th, 2011, 1:22 am
by Cristián Lávaque
You're an admin, make sure you try with a account that has an EOT time.

Re: Display account variables in theme

PostPosted: April 26th, 2011, 10:46 am
by theone
I tested on a Level 1 s2member account and still no return. Why would that be?

Re: Display account variables in theme

PostPosted: April 26th, 2011, 11:25 am
by Cristián Lávaque
Does that account have an auto_eot_time value? Check in the account's profile WP Admin -> Users -> [user] -> Automatic EOT Time

Re: Display account variables in theme

PostPosted: April 26th, 2011, 12:59 pm
by theone
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?

Re: Display account variables in theme

PostPosted: April 26th, 2011, 1:23 pm
by Cristián Lávaque
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. :)

Re: Display account variables in theme

PostPosted: April 26th, 2011, 4:56 pm
by theone
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.

Re: Display account variables in theme

PostPosted: April 26th, 2011, 10:04 pm
by Cristián Lávaque
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

Re: Display account variables in theme

PostPosted: April 27th, 2011, 12:59 am
by theone
I tried that but it returns a negative integer '-15092'

Re: Display account variables in theme

PostPosted: April 27th, 2011, 1:13 am
by Cristián Lávaque
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'); ?>

Re: Display account variables in theme

PostPosted: April 27th, 2011, 10:45 am
by theone
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 :)

Re: Display account variables in theme

PostPosted: April 27th, 2011, 3:39 pm
by theone
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?

Re: Display account variables in theme

PostPosted: April 27th, 2011, 5:03 pm
by Cristián Lávaque
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?

Re: Display account variables in theme

PostPosted: April 27th, 2011, 6:14 pm
by theone
It makes sense but trying to execute the script gets no return.

This is with EOT set mind you.

Re: Display account variables in theme

PostPosted: April 27th, 2011, 6:26 pm
by Cristián Lávaque
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.'

Re: Display account variables in theme

PostPosted: April 27th, 2011, 7:02 pm
by theone
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?

Re: Display account variables in theme

PostPosted: April 27th, 2011, 8:01 pm
by Cristián Lávaque
Yes, I understand. Then instead of rounding down with floor, round it up with ceil.

Re: Display account variables in theme

PostPosted: April 27th, 2011, 8:29 pm
by theone
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.';
    }
?>

Re: Display account variables in theme

PostPosted: April 27th, 2011, 8:37 pm
by theone
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.

Re: Display account variables in theme

PostPosted: April 27th, 2011, 10:42 pm
by Cristián Lávaque
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.';

Re: Display account variables in theme

PostPosted: April 27th, 2011, 11:38 pm
by theone
THANKS! That is precisely what I needed.

This will really help clarify my members options page.

Re: Display account variables in theme

PostPosted: April 28th, 2011, 12:43 am
by Cristián Lávaque
Good. :)

Re: Display account variables in theme

PostPosted: April 29th, 2011, 8:54 pm
by theone
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?