Shopify预计送货

时间:2014-08-06 15:59:50

标签: javascript shopify liquid

我希望在产品页面上显示我们每个交付选项的预计交货日期。我已经阅读了Steph Sharp的Shopify Variants中的代码,除了我们需要将它固定到当天直到下午3点,然后在下午3点之后切换到下一个工作日之后,它将工作得非常出色。 (基本上取消了客户选择发货日的选项。)

我无法通过将此代码嵌入到我们的模板中来实现它。这就是我一起宰了一下似乎工作正常而不是MON,TUE,WED ......我希望将它们设置为未来日期。有什么建议?

编辑:我也听说Palec在使用此代码的定时器代码之后也是如此。所以我会在中添加。

<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>

2 个答案:

答案 0 :(得分:1)

我会尝试这样的事情:

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;
}

addWeekdays函数中的代码来自Stack Overflow上的this answer。)

此代码只显示日期名称(与问题中的代码一样),但您可以根据需要格式化expectedDeliveryDate

编辑:我更新了我的代码,以使用评论中指定的expectedDeliveryDate.toDateString()。请注意,您不再需要days数组或expectedDeliveryDay变量。 (你仍然可以在your answer中找到它们但是它们没有被使用。)

答案 1 :(得分:0)

这是我的最终代码,基于Steph Sharp的答案。

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;
}

还添加了一个计时器:

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);
}