This script is great for situations where you have a subscription that is only run at regular intervals during the year, quarterly for example, and you need all of the subscriptions to run on the same day.
This script assumes that you're using an add to cart form, so you might have something like this inside your form:
<input type="hidden" name="name" value="subscription" /> <input type="hidden" name="price" value="20" /> <input type="hidden" name="sub_frequency" value="3m" /> <input type="hidden" name="sub_startdate" value="" id="subscription_startdate" />
Important to note that an ID of “subscription_startdate” is given to the sub_startdate input.
<script type="text/javascript"> function padDate(dp) { return (dp < 10 ? '0' : '') + dp; } jQuery(document).ready(function() { var shipments = [[2,1],[5,1],[8,1],[11,1]]; // [month,day] var curDate = new Date(); var m = curDate.getMonth() + 1; var y = curDate.getFullYear(); var nextShipment = 0; while (nextShipment == 0) { for (var d in shipments) { if (shipments[d][0] == m) { if (shipments[d][1] > curDate.getDate() || (curDate.getMonth() + 1) != m) { nextShipment = [y,padDate(shipments[d][0]),padDate(shipments[d][1])]; break; } else { var nd = (parseInt(d)+1); if (nd <= shipments.length-1) { nextShipment = [y,padDate(shipments[nd][0]),padDate(shipments[nd][1])]; break; } else { y++ nextShipment = [y,padDate(shipments[0][0]),padDate(shipments[0][1])]; break; } } } } if (m == 12) { m = 1; y++; } else { m++; } } jQuery("#subscription_startdate").val(nextShipment.join("")); }); </script>
To customise the script to your needs, at the most basic level you'll just need to change the shipments variable to match your dates. So for example, if you have four shipments on the 10th of January, April, July and October, your array would be:
var shipments = [[1,10],[4,10],[7,10],[10,10]];
There is no need to include year's in the array, the script works it out for you.