Jump to content

Show message outside opening hours


Recommended Posts

Hi,

 

I have a brick&mortar shop along with my online shop.

I want to show on the product page, let's say under the "Add to cart" button, a message saying that the store is open/closed, according to the opening hours of the offline shop.

 

I have found a coding example here https://3v4l.org/jdi2s, but I don't know how to implement it on the online shop.

 

The code to find out if the store is open/closed is this:

<?php

/**
 * Based on the following business hours:
 * (Note : I setup the hours for each day if they carry-over)
 * everyday is open from 09:00 AM - 12:00 AM
 * Sun/Sat open extra from 12:00 AM - 01:00 AM
 */
$storeSchedule = [
    'Sun' => ['12:00 AM' => '01:00 AM', '09:00 AM' => '12:00 AM'],
    'Mon' => ['09:00 AM' => '12:00 AM'],
    'Tue' => ['09:00 AM' => '12:00 AM'],
    'Wed' => ['09:00 AM' => '12:00 AM'],
    'Thu' => ['09:00 AM' => '12:00 AM'],
    'Fri' => ['09:00 AM' => '12:00 AM'],
    'Sat' => ['12:00 AM' => '01:00 AM', '09:00 AM' => '12:00 AM']
];

// current OR user supplied UNIX timestamp
$timestamp = time();

// default status
$status = 'closed';

// get current time object
$currentTime = (new DateTime())->setTimestamp($timestamp);

// loop through time ranges for current day
foreach ($storeSchedule[date('D', $timestamp)] as $startTime => $endTime) {

    // create time objects from start/end times
    $startTime = DateTime::createFromFormat('h:i A', $startTime);
    $endTime   = DateTime::createFromFormat('h:i A', $endTime);

    // check if current time is within a range
    if (($startTime < $currentTime) && ($currentTime < $endTime)) {
        $status = 'open';
        break;
    }
}

echo "We are currently: $status";

Please help.

Thanks!

Link to comment
Share on other sites

I believe a module can be created that could accomplish this.

 

One thing that I don't believe the code sample accounts for is the timezone.  This code would use the timezone of the server hosting your store, and not the timezone where your store is actually located.  For example, your business might be in NY, which is eastern time zone, but your server may be located in California, which is pacific time zone.  This needs to be accounted for in order for the code to work properly.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...