• Resolved Generosus

    (@generosus)


    The code snippet provided below generates the shortcode “[business_hours]” which can be used to display a message on the front end indicating if a business is open or closed based on normal business hours.

    We would like to modify it to add holidays together with the front end message: “CLOSED FOR THE HOLIDAY”

    We do not need to use this plugin in its entirety.

    Any experts available to help?

    Thank you!

    ———–

    function business_hours_shortcode($atts) {
    if (is_page ('contact')) {
    // Set timezone - change this to match your own.
    date_default_timezone_set('America/Chicago');

    // An array of your opening hours.
    $opening_hours = array(
    'Monday' => array('07:30', '17:30'),
    'Tuesday' => array('07:30', '17:30'),
    'Wednesday' => array('07:30', '17:30'),
    'Thursday' => array('07:30', '17:30'),
    'Friday' => array('07:30', '17:30'),
    'Saturday' => array('Closed'),
    'Sunday' => array('Closed'),
    );

    // Get current day and time.
    $current_day = date('l');
    $current_time = date('H:i');

    // Get today's opening hours.
    $today_hours = $opening_hours[$current_day];

    // Check if we have opening hours for today.
    if (!isset($today_hours) || count($today_hours) < 2) {
    return '<span style="color:#ffffff;background-color:#e14d43;padding:6px 12px;border-radius:3px;">CLOSED TODAY</span>';
    }

    // Check if current time is between opening hours.
    if ($current_time >= $today_hours[0] && $current_time <= $today_hours[1]) {
    return '<span style="color:#ffffff;background-color:#00c853;padding:6px 12px;border-radius:3px;">CURRENTLY OPEN</span>';
    } else {
    return '<span style="color:#ffffff;background-color:#e14d43;padding:6px 12px;border-radius:3px;">CURRENTLY CLOSED</span>';
    }
    }
    }
    add_shortcode('business_hours', 'business_hours_shortcode');
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Design Extreme

    (@designextreme)

    @generosus I am not sure if I follow why conditional text could not do the same here – or this along with the recent Shortcode:

    [open_special]
    [open_text]
    <h2>Holiday opening hours</h2>
    <p>
    %if_open% <span class="special-open">We are currently open</span>
    %else% <span class="special-closed">Closed today</span>
    %end%
    </p>
    [/open_text]
    [/open_special]

    This is a simplified version – you can add more parameters to the open_special Shortcode that will extend it.

    Thread Starter Generosus

    (@generosus)

    Hi @designextreme,

    Wow! Thanks for the quick reply.

    Not expert coders here, but are definitely trying to come up with a conditional that can be added to the above code snippet without using the shortcodes you provided.

    The modification would also have to include a function and/or array (?) where we can enter our specific holiday dates.

    Makes sense?

    Again, thank you!

    PS: We tried to integrate portions of the code provided here, but couldn’t get them to work.

    Plugin Author Design Extreme

    (@designextreme)

    @generosus Thanks for your suggestion, but I’m unsure why this is posted in the plugin’s support forum as it doesn’t relate to the plugin’s functionality, rather how to avoid using it.

    If you want to simply source the data – please use the following PHP code and apply the logic from there:

    <?php

    // This code should be placed/called within functions.php or similar

    $regular = get_option('we_are_open_regular', array()); // Array of regular opening hours
    $special = get_option('we_are_open_special', array()); // Array of special opening hours

    // Do things with the arrays
    Thread Starter Generosus

    (@generosus)

    Hi @designextreme,

    That was helpful. Thank you. We solved our issue. See below (added new code, in bold).

    Cheers!

    ————-

    function business_hours_shortcode($atts) {
    if (is_page ('contact')) {
    // Set timezone - change this to match your own.
    date_default_timezone_set('America/Chicago');

    // An array of your opening hours.
    $opening_hours = array(
    'Monday' => array('07:30', '17:30'),
    'Tuesday' => array('07:30', '17:30'),
    'Wednesday' => array('07:30', '17:30'),
    'Thursday' => array('07:30', '17:30'),
    'Friday' => array('07:30', '17:30'),
    'Saturday' => array('Closed'),
    'Sunday' => array('Closed'),
    );

    // An array of your holidays (format: month/day - add zero before single-digit month or day)
    $holidays = array('01/01', '05/26', '07/04', '07/05', '09/02', '11/11', '1/28', '11/29', '12/24', '12/25', '12/31');


    // Get current date, day and time.
    $current_date = date('m/d');
    $current_day = date('l');
    $current_time = date('H:i');

    // Get today's opening hours.
    $today_hours = $opening_hours[$current_day];

    // Check if today is a holiday.
    if (in_array($current_date,$holidays)) {
    return '<span style="font-weight:600;color:#ffffff;background-color:#e14d43;opacity:.7;padding:6px 12px;border-radius:3px;">CLOSED FOR HOLIDAY</span>';
    }


    // Check if we have opening hours for today.
    if (!isset($today_hours) || count($today_hours) < 2) {
    return '<span style="font-weight:600;color:#ffffff;background-color:#e14d43;opacity:.7;padding:6px 12px;border-radius:3px;">CLOSED TODAY</span>';
    }

    // Check if current time is between opening hours.
    if ($current_time >= $today_hours[0] && $current_time <= $today_hours[1]) {
    return '<span style="font-weight:600;color:#ffffff;background-color:#00c853;opacity:.7;padding:6px 12px;border-radius:3px;">CURRENTLY OPEN</span>';
    } else {
    return '<span style="font-weight:600;color:#ffffff;background-color:#e14d43;opacity:.7;padding:6px 12px;border-radius:3px;">CURRENTLY CLOSED</span>';
    }
    }
    }
    add_shortcode('business_hours', 'business_hours_shortcode');
    Plugin Author Design Extreme

    (@designextreme)

    @generosus Thanks for your code. However, I’m not sure this is a good home for this as it entirely side-steps anything offered by the We’re Open! plugin.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.