0

I am looking to show an estimated delivery date on the product page for each delivery option we have. I have read through the code in Shopify Variants by Steph Sharp which would work brilliantly except we would need it to be fixed to the current day up until 3pm and then switch to the next working day after 3pm. (Basically taking away the option for the customer to choose the dispatch day.)

I can’t quite get it to work by butchering this code into our template. This is what I have butchered together which seems to work okay but rather than have MON, TUE, WED, … I want to set them as the future dates. Any advice?

EDIT: Also I heard Palec is after using a timer code with this code too. So I will add that in.

<script language="JavaScript">
function day(a) {
    var date = new Date();

    var days = ["Mon","Tue","Wed","Thur","Fri","Mon","Tue","Wed","Thur","Fri","Mon","Tue","Wed","Thur","Fri"];

    var today = date.getDay();

    if (today == 1) today = 0; //Monday
    if (today == 2) today = 1; //Tuesday
    if (today == 3) today = 2; //Wednesday
    if (today == 4) today = 4; //Thursday
    if (today == 5) today = 5; //Friday
    if (today == 6) today = -1; //Saturday Moved To Monday
    if (today == 0) today = -1; //Sunday Moved To Monday

    h = date.getHours();
    if (h <= 9) h = "0" + h;
    time = h;

    if (time > 15) today++;

    var expected = today + a;

    var main = days[expected];

    document.write('STANDARD DELIVERY ESTIMATE:  ');
    document.write(main);
}
</script>

<body>
<script language="JavaScript">
day(1)
</script>
3
  • I have attempted to merge the code from the previous question into the template to no avail yet but I can work on that. Can what I am explaining even be done... is it possible remove the radial buttons just display the dates and use a static date for todays date which changes to the next day after 3pm.
    – Liam
    Aug 6, 2014 at 17:14
  • I will post what I have put in and where later on if thats okay?
    – Liam
    Aug 6, 2014 at 17:15
  • Just edit your question and add the code you've got so far.
    – morten.c
    Aug 7, 2014 at 0:00

2 Answers 2

1

I would try something like this:

function day(a) {
    var date = new Date();
    var hours = date.getHours();

    // If after 3pm, add 1 day
    if(hours > 15) a++;

    var expectedDeliveryDate = addWeekdays(date, a);
    document.write(expectedDeliveryDate.toDateString() + ' with Standard Delivery');
}

function addWeekdays(fromDate, days) {
    var count = 0;
    while (count < days) {
        fromDate.setDate(fromDate.getDate() + 1);
        if (fromDate.getDay() != 0 && fromDate.getDay() != 6) // Skip weekends
            count++;
    }
    return fromDate;
}

(The code in the addWeekdays function is from this answer on Stack Overflow.)

This code just displays the day name (like the code in your question), but you can format expectedDeliveryDate however you want.

EDIT: I updated my code to use expectedDeliveryDate.toDateString() as specified in the comments. Note that you no longer need the days array or expectedDeliveryDay variable. (You've still got them in your answer but they're not being used.)

0
0

This is my final code, based on Steph Sharp’s answer.

function day(a) {
    var date = new Date();
    var hours = date.getHours();

    // If after 3pm, add 1 day
    if (hours >= 15) a++;

    var expectedDeliveryDate = addWeekdays(date, a);
    document.write(expectedDeliveryDate.toDateString() + ' with Standard Delivery');
}

function addWeekdays(fromDate, days) {
    var count = 0;
    while (count < days) {
        fromDate.setDate(fromDate.getDate() + 1);
        if (fromDate.getDay() != 0 && fromDate.getDay() != 6) // Skip weekends
            count++;
    }
    return fromDate;
}

Also added a timer:

function ShowTime() {
    var now = new Date();
    var hrs = 15 - now.getHours();
    if (hrs < 0) hrs += 24;
    var mins = 60 - now.getMinutes();
    var secs = 60 - now.getSeconds();
    timeLeft = "" + hrs + ' hours ' + mins + ' minutes ' + secs + ' seconds';
    $("#countdown").html(timeLeft);
}

var countdown = setInterval(ShowTime, 1000);
function StopTime() {
    clearInterval(countdown);
}
6
  • 1
    Additonally the question isn't about "a timer" and you just copy and pasted the answer of @steph-sharp with little to no changes. This is more a comment on her answer then an answer itself.
    – morten.c
    Aug 7, 2014 at 15:13
  • I was showing the final code I used (It is my question after all I am the OP) and I wanted to show what code I used in the end. I have edited what Steph but to fully answer the question which shows the "estimated" delivery date formatted as it was requested. I gave thanks to Steph for her involvement in helping me come to the conclusion. The timer is an addition to the same code used. It adds a timer to show how long until the change will take place, I posted this for anyone else that may want to add this to their code.
    – Liam
    Aug 7, 2014 at 17:12
  • @Palec, I will remember that in future. I just thought everybody could see that I was the one that posted the questions and that post. I am not taking any credit for the code I just wanted to show what code I ended up using as I feel it is a good feature to have (that should really be built in) on a shopify store. Thanks everyone.
    – Liam
    Aug 8, 2014 at 7:31
  • @Liam Just edited your answer to improve formatting and fix two issues with the code (StopTime originally did not work, day contained unused code). Now I realized that your code is exactly the same as Steph Sharp’s. I think you should move the addendum with timer to the question and delete your answer as it really adds nothing new to what you asked for. (Timer was not asked for.)
    – Palec
    Aug 8, 2014 at 12:24
  • @palec stop banging on yeah? The code worked fine for me. The timer is an added bonus for anyone that wants to include it. The timer was not part of the question because I was not stuck with the timer. ;)
    – Liam
    Aug 8, 2014 at 14:42

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.