Add to cart form
Set the sub_startdate to the next closest date from a group of dates
0.6.0, 0.7.0, 0.7.1, 0.7.2, 1.0, 1.1
snippets, subscription, addtocart, form, advance

Set the sub_startdate to the next closest date from a group of dates

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.

Step 1: Setup the form

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.

Step 2: Add the javascript

<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])];
        } else {
          var nd = (parseInt(d)+1);
          if (nd <= shipments.length-1) {
            nextShipment = [y,padDate(shipments[nd][0]),padDate(shipments[nd][1])];
          } else {
            nextShipment = [y,padDate(shipments[0][0]),padDate(shipments[0][1])];
    if (m == 12) {
      m = 1;
    } else {

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.

What does this script do?

  1. Takes the current date, and loops through all of the shipment dates and checks if any months match the current month
  2. If it does, it checks if the next shipment is after today.
  3. If it is, it marks that shipment as the next shipment.
  4. If not, it checks if there is another shipment this year.
  5. If there is, it uses the next shipment
  6. If not, it uses the first shipment and sets the year as the following year.
  7. If the current month doesn't match any shipments, it adds a month and loops through again (and adding a year if the loop reaches December)

Site Tools