How to handle a block of data values in theme settings? (or other ways to do it?)

Highlighted
Tourist
4 0 1

I need to have a bunch of related configuration settings (in this case the opening/closing hours on each day of the week) so that these can be applied consistently through the store's pages (here, showing the opening hours on the frontpage, showing a picker for collection times on the cart, etc).

The obvious place to put them seemed to be the theme settings, although that just gives a set of individual items rather than any structure, and ends up with a rather verbose settings_schema.json with the same picker values repeated for 14 items in my case.

Anyhow, I've done that and I now have settings.mon_open, settings.mon_close, settings.tue_open, settings.tue_close etc that I can use in the pages of the theme.  However, that then leads to huge amounts of repetetive code each time I come to use them, with big case statements (each 'when' clause being almost the same), or strings of if/then for each separate day: I really want to write this code for one day only and either wrap it in a for loop or index it with the current day as applicable.

The best way I have come up with so far is to turn my configuration data into an array like this:

              {% capture tmp %}{{settings.mon_open}},{{settings.tue_open}},{{settings.wed_open}},{{settings.thu_open}},{{settings.fri_open}},{{settings.sat_open}},{{settings.sun_open}}{% endcapture %}
              {% assign open_times = tmp | split: ',' %}
              {% capture tmp %}{{settings.mon_close}},{{settings.tue_close}},{{settings.wed_close}},{{settings.thu_close}},{{settings.fri_close}},{{settings.sat_close}},{{settings.sun_close}}{% endcapture %}
              {% assign close_times = tmp | split: ',' %}

 

I can then use open_times[forloop.index0] etc.

However, this all seems fairly inefficient, and I'd be glad of any alternative suggestions. 

0 Likes