wpsharks / s2member-kb

The s2Member® and s2Member® Pro Knowledge Base
9 stars 4 forks source link

Specifying a future start date for a recurring subscription (aka Custom EOT) #13

Open raamdev opened 9 years ago

raamdev commented 9 years ago

KB Article Creation Checklist

Markdown File: http://wsharks.com/1EqcFzR Published KB Article: http://s2member.com/kb-article/specifying-a-future-start-date-for-a-recurring-subscription-aka-custom-eot/


jaswrks commented 9 years ago

That question has been asked at least 100 times over the past couple years. Great article!

raamdev commented 9 years ago

That question has been asked at least 100 times

Yep, that was my thought as well when I got the support request that triggered my draft above! In fact, I remembered seeing that scenario so many times that I was sure there was already a KB article on it and spent a good couple of minutes searching for one.

garethwyndavies commented 9 years ago

Dear Jason and Raamdev,

I would be really grateful if you could point me in the right direction here. I was very excited when I saw this post because it looked to offer a solution to a problem that I have been banging my head against for a while now - namely, how to get members to pay for a full calendar year (regardless when they join) rather than for a fixed term from the time of payment. The dynamic trial amount php code seems to be the key.

Using your complete example above I have tried to set up the S2Member shortcode so that someone joining / renewing at any point in the year would pay the same amount for that year up to 31st December. In the example below I have said that the trial period should be to 30th December 2015 (free). The fixed term is set as just one day (also free), so in theory anyone joining at any point in the year will have full membership up to the end of the year. I was going to use this to allow current members to join a new website and bring them all into line (end of the calendar year).

However, the set up below is resulting in an EOT two days in the future. In the last test, joined on 10th January 2015 and EOT is set as 12th January 2015. I assume that only the rp = 1 and the 1 day grace period are being calculated.

Please can you advise what may be happening? Many thanks in advance.

Gareth

<?php // Change these to the date you want the annual cycle to officially start $cycle_start_year = '2015'; $cycle_start_month = '12'; $cycle_start_day = '30';

$cdate = mktime(0, 0, 0, $cycle_start_month, $cycle_start_day, $cycle_start_year); $today = time(); $difference = $cdate - $today; if ($difference < 0) { $difference = 0; } $trial_days = floor($difference/60/60/24); // Calculated number of days until yearly cycle should start ?>

[s2Member-Pro-PayPal-Form level="1" ccaps="" desc="364 Days free / then $0.00 USD / One Time (for 1 day access, non-recurring)" ps="paypal" lc="" cc="USD" dg="0" ns="1" custom="a49.030.myftpupload.com" ta="0" tp="<?php echo $trial_days; ?>" tt="D" ra="0" rp="1" rt="D" rr="0" rrt="" rra="2" accept="paypal,visa,mastercard,amex,discover,maestro,solo" accept_via_paypal="paypal" coupon="" accept_coupons="0" default_country_code="" captcha="0" /]

garethwyndavies commented 9 years ago

Hi Raamdev, I am just following up on this having played with it again today. I am no further along but it does seem that the PHP code output is beng picked up by S2Member - or at least it is recognising that a value is being created because if the PHP code gets broken then S2Member recognises that there isn't an appropriate value in place. All I can think is that the PHP code is producing a value of 0. I am not familiar with PHP but have tried to read up on it and am unable to see what the problem may be. Hoping that it is not an issue with ezPHP plugin.

Really hoping that you can help.

Many thanks,

Gareth

raamdev commented 9 years ago

@garethwyndavies When you paste the PHP code and the shortcode into your Post/Page, are you using the Visual mode or the Text mode? You MUST be using the Text mode, otherwise the PHP may become corrupt:

2015-01-12_16-45-28

If you are using the Text mode, then I suggest disabling all of your plugins (except s2Member and ezPHP) and switching to a default WordPress theme (such as TwentyFifteen) and then re-attempting to create the page with the PHP code + s2Member Shortcode. It may be that another plugin or your theme is introducing a conflict.

jaswrks commented 9 years ago

Note: the ezPHP plugin also supports this syntax which can be helpful at times; i.e. to avoid theme/plugin conflicts; or conflicts with your WP editor causing corruption of < and/or > symbols.

[php]
// Change these to the date you want the annual cycle to officially start 
$cycle_start_year = '2015';
$cycle_start_month = '2';
$cycle_start_day = '1';

$cdate = mktime(0, 0, 0, $cycle_start_month, $cycle_start_day, $cycle_start_year);
$today = time();
$difference = $cdate - $today;
if ($difference < 0) { $difference = 0; }
$trial_days = floor($difference/60/60/24); // Calculated number of days until yearly cycle should start
[/php]
garethwyndavies commented 9 years ago

Hello Both, Thanks for responding. I am using the text editor. I noticed that things could get corrupted if switching to visual, even to take a look, and so I don't do that. I have tried using other php plugins and also the [php] syntax... but I get the same effect. I am presently using 'php code for posts' which is otherwise a good option for me because I will need to change the dates in the code on mutiple pages in the future and this means I only have to make the change in one location. I am using X-theme and will try switching theme and disabling other plugins as suggested.

Gareth

garethwyndavies commented 9 years ago

Hi again, Right - changed the theme and deactiviate all other plugins as suggested. Used ezphp again and set it up just as you suggest (below). Still getting the same result.

I assume that you have tested the code and it is working for you so can you see anything obvious in my set up that I have missed? Below is the set up I have just tested with. Is there any difference because your example was set just 2 months in the future while mine is closer to a year? I don't see why it would be but I am strating to clutch as straws here.

Thanks

Gareth

[php] // Change these to the date you want the annual cycle to officially start $cycle_start_year = '2015'; $cycle_start_month = '12'; $cycle_start_day = '30';

$cdate = mktime(0, 0, 0, $cycle_start_month, $cycle_start_day, $cycle_start_year); $today = time(); $difference = $cdate - $today; if ($difference < 0) { $difference = 0; } $trial_days = floor($difference/60/60/24); // Calculated number of days until yearly cycle should start [/php]

[s2Member-Pro-PayPal-Form level="1" ccaps="" desc="364 Days free / then $0.00 USD / One Time (for 1 day access, non-recurring)" ps="paypal" lc="" cc="USD" dg="0" ns="1" custom="a49.030.myftpupload.com" ta="0" tp="<?php echo $trial_days; ?>" tt="D" ra="0" rp="1" rt="D" rr="0" rrt="" rra="2" accept="paypal,visa,mastercard,amex,discover,maestro,solo" accept_via_paypal="paypal" coupon="" accept_coupons="0" default_country_code="" captcha="0" /]

garethwyndavies commented 9 years ago

I have tested this on another installation of wordpress/s2memberpro and get the same effect. Only the rp=1 value seems to be calculated. The PHP code seems to be returning a value of 0. Is there any way for me to test what value the code is actually producing? S2member says if a non numeric output is produced so the PHP code is obviously producing something. I have also tried disabling visual editor as some people say that that can make the php unstable - no effect. I have also used the example set up above (just making the ra=0) and again, the dynamic eot is not being picked up. Any help appreciated. Thanks, Gareth

raamdev commented 9 years ago

@garethwyndavies What version of PHP are you running on the server?

garethwyndavies commented 9 years ago

@raamdev My server is running PHP version 5.4.16 and MySQL version 5.5.40

garethwyndavies commented 9 years ago

@raamdev what do you think? PHP version seems new enough.

raamdev commented 9 years ago

@garethwyndavies I just copy/pasted your code into my test install for s2Member and it's working as expected.

I also have this line underneath my shortcode, just to see what value the tp="" attribute is being set to:

<?php echo "days: ". $trial_days; ?>

When I view the page, it shows days: 354, which looks correct to me. That's setting up the form with 354 days as a trial period.

I'm using PHP 5.4.10, so I don't think your issue has anything to do with the PHP version.

garethwyndavies commented 9 years ago

@raamdev Many thanks for the test line. It is producing days: 349 for me, which is about right if the trial period is to 30th December. I guess is proves that your code is right (obviously), and should in theory be picked up by S2member, so I guess that I am back to making sure the S2member set up is right. Do you think that making the ra=0 and non recuring could be doing anything?

Sorry, really clutching. I can't help but think that I am missing something really obvious here.

Thanks for your assistance and in coming up with this idea, which I think will be of benefit to quite a few people.. if they can geting it going better than me.

garethwyndavies commented 9 years ago

@raamdev @jaswsinc I may be on the right track now. I have tried putting in $0.01 for the 'ta' and 'ra' values. This results in an EOT not being created immediately. I assume that this is because s2 waits for PayPal to say when the EOT should be after taking the money for the ra.

In my earlier tests I was using 0 values for ra and ta. S2 did not seem to do anything with the tp value... because PayPal was not involved? ... and immediately created an EOT. I am guessing this is because no-one would have a reason to set up a tp without a later charge coming into play?

I will set up a tp for 2 days as a test, with a 1cent charge through PayPal to see if everything works as it should and an EOT get created.

raamdev commented 9 years ago

@garethwyndavies

Do you think that making the ra=0 and non recuring could be doing anything?

Yes, I believe that was your issue. You should be able to have ta="0" (Trial Amount) to provide a free Trial, but the recurring amount for the subscription must be non-zero (e.g., ra="0.01"), otherwise the Payment Gateway never comes into effect.

Thank you for reporting this and working with us here! I'll add a note about this to the KB draft to help avoid confusion about this issue.

raamdev commented 9 years ago

@garethwyndavies Actually, I take that back. You should be able to make this work even with free registration forms (i.e., no payment gateway involved). However, I believe your issue was related to the fact that you were specifying ra="", rp="", and rt="" values in your shortcode while attempting to create a free subscription (ra="0").

If you want to use the technique described in the article above to create a dynamic EOT for a free subscription, you'd use the following shortcode (note the tp="" attribute modified to use the dynamic days):

[s2Member-Pro-PayPal-Form register="1" level="0" ccaps="" desc="Signup now, it's Free!" custom="example.com" tp="<?php echo $trial_days; ?>" tt="D" captcha="clean" /]

This creates a Free Subscription form that results in the account expiring (EOT) after $trial_days.

garethwyndavies commented 9 years ago

@raamdev There should be a special word expressing that euphoric feeling you get when an IT problem gets resolved and the gratitude you feel to the person who helped you find the answer. Until then, thank you.

raamdev commented 9 years ago

@garethwyndavies :) You're welcome! Thank you for helping to improve this KB article!

garethwyndavies commented 9 years ago

@raamdev I wonder, would it be possible to also have the cycle start dates changing dynamically? For instance, on my website I will use this code on every payment gateway so that members renewing later in the year or joining part way through a year are kept in sync with the calendar year. This means remembering to change the code at the start of every year to add one year to $cycle_start_year = 'value';

but I will not need to change the month and year.

instead of 'value' is it possible to make this the current year?

Would "<?php echo date("Y"); ?>" work?

Would be really powerful if annual updating could be dynamic. What do you think?

raamdev commented 9 years ago

@garethwyndavies Yes, you could change it like this:

$cycle_start_year = date('Y'); // Always use the current year
raamdev commented 8 years ago

Need to update this to mention that this technique is not limited to recurring subscriptions, but also applies to fixed-term subscriptions: https://github.com/websharks/s2member/issues/835#issuecomment-169315727

Mugane commented 3 years ago

This doesn't work. I've set the start date to Jan 1 of this year, for a 1-year term, but the subscription in Stripe always starts today. Ugh. Please advise. We need a 1-YEAR term that renews on 1/1/next year. This should not be so complicated...