You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So I have been trying to output the opening hours for a client on their contact page.
I was migrating an old site to Craft 3 and came across this. Previously this seemed much easier to do in Craft 2 SEOmatic as the code below will show.
Old SEOmatic (2) code:
{% for openingHours in seomaticIdentity.openingHoursSpecification %}
<span>{% for day in openingHours.dayOfWeek %}{{ day }}{% endfor %}:</span> {{ openingHours.opens|date('g:ia')|replace({':00' : ''}) }} - {{ openingHours.closes|date('g:ia')|replace({':00' : ''}) }}<br />
{% endfor %}
New SEOmatic (3) Code:
{% for openingHours in seomatic.site.identity.localBusinessOpeningHours %}
{% set dayOfTheWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] %}
{{ dayOfTheWeek[loop.index0] }}:
{% if openingHours.open.date is defined %}
{{ openingHours.open.date|date('g:ia')|replace({':00' : ''}) }}
-
{% endif %}
{% if openingHours.close.date is defined %}
{{ openingHours.close.date|date('g:ia')|replace({':00' : ''}) }}
{% endif %}
{% if openingHours.open.date is not defined and openingHours.close.date is not defined %}
Closed
{% endif %}
<br />
{% endfor %}
By default the array outputs Sunday -> Saturday, but typically I would like to output Monday -> Sunday. So this adds even more code to manipulate this.
{% for openingHours in seomatic.site.identity.localBusinessOpeningHours[1:] %}
{% set dayOfTheWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] %}
{{ dayOfTheWeek[loop.index0] }}:
{% if openingHours.open.date is defined %}
{{ openingHours.open.date|date('g:ia')|replace({':00' : ''}) }}
-
{% endif %}
{% if openingHours.close.date is defined %}
{{ openingHours.close.date|date('g:ia')|replace({':00' : ''}) }}
{% endif %}
{% if openingHours.open.date is not defined and openingHours.close.date is not defined %}
Closed
{% endif %}
<br />
{% endfor %}
{% for openingHours in seomatic.site.identity.localBusinessOpeningHours[0:1] %}
Sunday:
{% if openingHours.open.date is defined %}
{{ openingHours.open.date|date('g:ia')|replace({':00' : ''}) }}
-
{% endif %}
{% if openingHours.close.date is defined %}
{{ openingHours.close.date|date('g:ia')|replace({':00' : ''}) }}
{% endif %}
{% if openingHours.open.date is not defined and openingHours.close.date is not defined %}
Closed
{% endif %}
{% endfor %}
So I think it would be really good to have a couple of additional helpers/options to make this slightly easier to achieve in twig.
Here are my thoughts:
A helper to get the day of the week. Currently, you need to map the index to an array of the weekdays.
In old SEOMatic there was an option to call dayOfWeek This alone would reduce the need to set a separate array and map this. It looks like you are already doing this for the Schema data anyway.
A helper to order the array from Monday -> Sunday. Currently the array goes from Sunday -> Saturday. Even though in the backend the order shows as Monday -> Sunday.
Just these alone would make this a more pleasant and simplified experience of getting the opening hours.
I know you can get some of this from the seomatic.jsonLd.get('identity')
{% set identity = seomatic.jsonLd.get('identity') %}
{{ dump(identity.openingHoursSpecification) }}
Which I did try that as well. But it omits days if there is no time set. Which then also causes issues with looping the array. As i'd like to show 'Closed' if no time is set.
So I went back to the seomatic.site.identity.localBusinessOpeningHours instead.
Many Thanks.
The text was updated successfully, but these errors were encountered:
So I have been trying to output the opening hours for a client on their contact page.
I was migrating an old site to Craft 3 and came across this. Previously this seemed much easier to do in Craft 2 SEOmatic as the code below will show.
Old SEOmatic (2) code:
New SEOmatic (3) Code:
By default the array outputs Sunday -> Saturday, but typically I would like to output Monday -> Sunday. So this adds even more code to manipulate this.
So I think it would be really good to have a couple of additional helpers/options to make this slightly easier to achieve in twig.
Here are my thoughts:
A helper to get the day of the week. Currently, you need to map the index to an array of the weekdays.
In old SEOMatic there was an option to call
dayOfWeek
This alone would reduce the need to set a separate array and map this. It looks like you are already doing this for the Schema data anyway.A helper to order the array from Monday -> Sunday. Currently the array goes from Sunday -> Saturday. Even though in the backend the order shows as Monday -> Sunday.
Just these alone would make this a more pleasant and simplified experience of getting the opening hours.
I know you can get some of this from the
seomatic.jsonLd.get('identity')
Which I did try that as well. But it omits days if there is no time set. Which then also causes issues with looping the array. As i'd like to show 'Closed' if no time is set.
So I went back to the
seomatic.site.identity.localBusinessOpeningHours
instead.Many Thanks.
The text was updated successfully, but these errors were encountered: