Table of Contents
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.
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?
- Takes the current date, and loops through all of the shipment dates and checks if any months match the current month
- If it does, it checks if the next shipment is after today.
- If it is, it marks that shipment as the next shipment.
- If not, it checks if there is another shipment this year.
- If there is, it uses the next shipment
- If not, it uses the first shipment and sets the year as the following year.
- 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)