craftcms / store-hours

Manage business hours with Craft CMS.
https://plugins.craftcms.com/store-hours
MIT License
61 stars 15 forks source link

storeHours|length is always 7 #16

Closed pixeldeluxe closed 7 years ago

pixeldeluxe commented 7 years ago

How do I Check is none of the storeHours is filled? When you save the storeHours|length will be 7 instead of 0.

{% if entry.storeHours|length %} ... {% endif %}

brandonkelly commented 7 years ago

The field definitely isn’t optimized for this use case, but you could loop through each of the days and check their values:

{% set hasHours = false %}
{% for day in entry.storeHours %}
    {% if not hasHours and (day.open or day.close) %}
        {% set hasHours = true %}
    {% endif %}
{% endfor %}

Bonus points if you use the {% break %} tag provided by the MN Twig Perversion plugin:

{% set hasHours = false %}
{% for day in entry.storeHours %}
    {% if day.open or day.close %}
        {% set hasHours = true %}
        {% break %}
    {% endif %}
{% endfor %}