I’ve set up this opening hours timetable for my website, but I wanted to get rid of unnecessary javascript and therefore wanted to do with liquid only.
I’m not very familiar with it but could anyone maybe point me in the right direction? Is it possible to do "if certain day between this and that time" conditions?
Thanks a lot for helping out,
https://codepen.io/EliasUUUU/pen/GRyMgyj
var currentDate = new Date();
var weekday = [];
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var currentDay = weekday[currentDate.getDay()];
var currentTimeHours = currentDate.getHours();
currentTimeHours = currentTimeHours < 10 ? "0" + currentTimeHours : currentTimeHours;
var currentTimeMinutes = currentDate.getMinutes();
var timeNow = currentTimeHours + "" + currentTimeMinutes;
var currentDayID = "#" + currentDay; //gets todays weekday and turns it into id
$(currentDayID).toggleClass("today"); //this works at hightlighting today
var openTimeSplit = $(currentDayID).children('.opens').text().split(":");
var openTimeHours = openTimeSplit[0];
openTimeHours = openTimeHours < 10 ? "0" + openTimeHours : openTimeHours;
var openTimeMinutes = openTimeSplit[1];
var openTimex = openTimeSplit[0] + openTimeSplit[1];
var closeTimeSplit = $(currentDayID).children('.closes').text().split(":");
var closeTimeHours = closeTimeSplit[0];
closeTimeHours = closeTimeHours < 10 ? "0" + closeTimeHours : closeTimeHours;
var closeTimeMinutes = closeTimeSplit[1];
var closeTimex = closeTimeSplit[0] + closeTimeSplit[1];
if (timeNow >= openTimex && timeNow <= closeTimex) {
$(".openorclosed").toggleClass("open");
} else {
$(".openorclosed").toggleClass("closed");
}
.openinghours {
margin:10px;
padding:0 10px 0 10px;
overflow: hidden;
display: inline-block;
}
.openinghourscontent {
float:left;
}
.openinghourscontent h2 {
display:block;
text-align:center;
margin-top:.33em;
}
.today {
color: #8AC007;
}
.opening-hours-table tr td:first-child {
font-weight:bold;
}
#open-status {
display:block;
margin-top:-1em;
text-align:center;
}
.openorclosed:after {
content:" åbent på disse tider:";
}
.open {
color:green;
}
.open:after {
content:" Åbent";
color: #6C0;
}
.closed:after {
content:" Lukket";
color: red;
}
.opening-hours-table tr td {
padding:5px;
}
<section class="openinghours">
<div class="openinghourscontent section">
<div class="header">
<h2></h2>
<span id="open-status"><small class="openorclosed">Vi har </small></span>
</div>
<table class="opening-hours-table">
<tr id="Monday" itemprop="openingHours" title="Open Monday at 9am to 6pm">
<td>Mandag</td>
<td class="opens">12:00</td>
<td>-</td>
<td class="closes">19:00</td>
</tr>
<tr id="Tuesday" itemprop="openingHours" title="Open Tuesday at 9am to 6pm">
<td>Tirsdag</td>
<td class="opens">12:00</td>
<td>-</td>
<td class="closes">22:00</td>
</tr>
<tr id="Wednesday" itemprop="openingHours" title="Open Wednesday at 9am to 6pm">
<td>Onsdag</td>
<td class="opens">12:00</td>
<td>-</td>
<td class="closes">19:00</td>
</tr>
<tr id="Thursday" itemprop="openingHours" title="Open Thursday at 9am to 8pm">
<td>Torsdag</td>
<td class="opens">12:00</td>
<td>-</td>
<td class="closes">19:00</td>
</tr>
<tr id="Friday" itemprop="openingHours" title="Open Friday at 9am to 6pm">
<td>Fredag</td>
<td class="opens">12:00</td>
<td>-</td>
<td class="closes">19:00</td>
</tr>
<tr id="Saturday" itemprop="openingHours" title="Open Saturday at 10am to 6pm">
<td>Lørdag</td>
<td class="opens">12:00</td>
<td>-</td>
<td class="closes">19:00</td>
</tr>
<tr id="Sunday" itemprop="openingHours" title="Open Sunday at 11am to 4pm">
<td>Søndag</td>
<td class="closed"> </td>
<td></td>
<td class="closes"></td>
</tr>
</table>
<script>
(function(e, t, n, r) {
if (e) return;
t._appt = true;
var i = n.createElement(r),
s = n.getElementsByTagName(r)[0];
i.async = true;
i.src = '//dje0x8zlxc38k.cloudfront.net/loaders/s-min.js';
s.parentNode.insertBefore(i, s)
})(window._appt, window, document, "script")
</script>
</div>
</section>
2
Answers
So, I ended up using Fabio's method, but I wanted to avoid using metafields even though it probably is the better solution. For the interested the code can be found below. The example here is timetable for a week with monday closed tuesday-friday 12-19, saturday 11-19 and sunday closed. The current day is highlighted, also out of opening hours, while open/closed status is shown on top of the table switching between green/red.
The tip is to use the filter
date
This is a quick example
You can check here the available formats http://www.strfti.me/
Note that
current_day
andcurrent_hour
are both strings so you need to compare strings with strings.It would be easier if the days are saved in a metadata so you can use them as an object.