Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More helpers for Opening Hours for easier code/output #843

Open
terryupton opened this issue Feb 24, 2021 · 0 comments
Open

More helpers for Opening Hours for easier code/output #843

terryupton opened this issue Feb 24, 2021 · 0 comments

Comments

@terryupton
Copy link

terryupton commented Feb 24, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant