Home › Forums › Front-end Issues › Restaurant opening hours with exceptions
Please help me with below code. The code works 99% (only 1 if statement always returns FALSE)
I have an option page with restaurant opening hours and exceptions. I want to show a OPEN or CLOSED icon (via shortcode) regarding the current opening-status.
// Display opening status as icon
function yl_opening_status_icon_shortcode() {
if (have_rows('booking_settings_opening_hours', 'booking_settings')) {
while (have_rows('booking_settings_opening_hours', 'booking_settings')) { the_row();
$today_date = date('Y-m-d'); // DATE OF TODAY
$today_day = date('w'); // WEEKDAY OF TODAY
$opening_days = get_sub_field('booking_settings_opening_hours_day'); // MULTI SELECT FOR OPENING WEEK DAYS
}
}
if (have_rows('booking_settings_exceptions', 'booking_settings')) {
while (have_rows('booking_settings_exceptions', 'booking_settings')) { the_row();
$exceptions = get_sub_field('booking_settings_exceptions_date'); // DATE FIELD
$exception = str_replace('/', '-', $exceptions); // BEING SURE ALL DATE FORMATS ARE EQUAL
$whole_day = get_sub_field('booking_settings_exceptions_whole_day'); // YES-NO FIELD
$reason = get_sub_field('booking_settings_exceptions_reason'); // SELECT BOX
$exception_date = date("Y-m-d", strtotime($exception)); // GETTING THE EXACT SAME DATE STRING
}
}
if (in_array($today_date, $exception_date)) { // THIS IF STATEMENT ALWAYS RETURNS FALSE, EVEN IF ITS TRUE
if ($whole_day) {
$output = '
<div>
<div class="uk-grid-small uk-flex-middle" uk-grid>
<div class="uk-width-1-4@s">
<img src="/wp-content/uploads/sites/3/2021/10/Restaurant-Old-Dutch-alert-icon-dicht.svg">
</div>
<div class="uk-width-expand uk-visible@s">
CLOSED
</div>
</div>
</div>
';
} else {
$output = '
<div>
<div class="uk-grid-small uk-flex-middle" uk-grid>
<div class="uk-width-1-4@s">
<img src="/wp-content/uploads/sites/3/2021/10/Restaurant-Old-Dutch-alert-icon-open.svg">
</div>
<div class="uk-width-expand uk-visible@s">
OPEN
</div>
</div>
</div>
';
}
} else {
if (in_array($today_day, $opening_days)) { // THIS IF STATEMENT WORKS
$output = '
<div>
<div class="uk-grid-small uk-flex-middle" uk-grid>
<div class="uk-width-1-4@s">
<img src="/wp-content/uploads/sites/3/2021/10/Restaurant-Old-Dutch-alert-icon-open.svg">
</div>
<div class="uk-width-expand uk-visible@s">
OPEN
</div>
</div>
</div>
';
} else {
$output = '
<div>
<div class="uk-grid-small uk-flex-middle" uk-grid>
<div class="uk-width-1-4@s">
<img src="/wp-content/uploads/sites/3/2021/10/Restaurant-Old-Dutch-alert-icon-dicht.svg">
</div>
<div class="uk-width-expand uk-visible@s">
CLOSED
</div>
</div>
</div>
';
}
}
return $output;
}
add_shortcode('yl_opening_status_icon','yl_opening_status_icon_shortcode');
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.