1

Anyone using the "Store Hours" plugin know how to print out day open and close hours details?

I have tried this but not outputting anything.

{% for openTime in entry.openTimes %}
    {{openTime.day.open}}<br>
{% endfor %}  

Note: openTimes is a field of StoreHours type.

Any help will be appreciated.

  • Have you reached out the to plugin developer? Is there a link (github or otherwise) for this plugin? – Adam McCombs Sep 19 '14 at 3:16
  • Pixel and Tonic are the developers of this plugin. I thought this is the best place to ask. On the github page there is no info about outputting in twig template – Silvanus Matiku Sep 19 '14 at 3:29
5

The openTime data is being stored as a numerically-indexed array. You may even want to create a mapping of those days to make rendering each day easier:

{% set days = {
    0: 'Sunday',
    1: 'Monday',
    2: 'Tuesday',
    3: 'Wednesday',
    4: 'Thursday',
    5: 'Friday',
    6: 'Saturday'
} %}

Then you can adjust your for loop like this:

{% for dayIndex,openTime in entry.openTimes %}

    {% set day = days[dayIndex] %}
    {% set hours = openTime.open|date('g:i a') ~ ' - ' ~ openTime.close|date('g:i a') %}

    {{ day }}: {{ hours }}<br/>

{% endfor %}

Pro Tip:

If you have dev mode enabled, you can use dump to see what a variable contains.

{{ dump(anyVariable) }}

That's how I figured out the underlying structure of the openTime data.

| improve this answer | |
6

Following from Lindsey D's answer, I just wanted to point out you can skip manually mapping the weekday names by calling craft.i18n.getLocaleData().getWeekDayName() like so:

{% set day = craft.i18n.getLocaleData().getWeekDayName(dayIndex) %}

It's also worth mentioning, you can check for empty values to indicate the store is closed.

E.g.,

{% for dayIndex,dayHours in companyInfo.storeHours %}

  {% set day = craft.i18n.getLocaleData().getWeekDayName(dayIndex) %}

  {{day}}: {{ (dayHours.open is empty) ? 'CLOSED' : dayHours.open|date('g:i a') ~ '-' ~ dayHours.close|date('g:i a') }}

  <br>

{% endfor %}
| improve this answer | |
  • Is there a way to get {% set day = craft.i18n.getLocaleData().getWeekDayName(dayIndex) %} to start on Monday instead of Sunday? – Andrea DeMers Mar 7 '16 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.