Issue
Basically, I want to show "FREE delivery {delivery-estimate}. Order within {time-counter}" above my add to cart button on my Shopify page. How can I do it?
Note:
- delivery estimate should be after 7 days as of the current date.
- time counter should refresh at 1:00 pm, and if the time is after 1:00 pm I want to add one more day to the estimated date.
Here is what I found before, but I cannot change it to the format I initially wanted.
<script language="JavaScript">
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;
}
</script>
<body>
<script language="JavaScript">
day(1)
</script>
Solution
Try something like this and you can filter this according to your needs:
const date = new Date()
const hours = date.getHours()
const newDate = new Date(date);
const deliveryEstimate = hours < 13 ? new Date(newDate.setDate(newDate.getDate() + 7)) : new Date(newDate.setDate(newDate.getDate() + 8))
const orderWithinValue = hours < 13 ? 13-hours : hours-13
console.log(`FREE delivery by ${deliveryEstimate.toISOString().split('T')[0]}. Order within ${orderWithinValue} hours`)
Your problem statement will have just a couple of "if condtions" and you need to handle them accordingly.
Answered By - Prateek Goyal Answer Checked By - Candace Johnson (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.