Ad

Chose Which (shopify) Collection To Loop Through With Schema In Liquid?

- 1 answer

So I am trying to loop through a collection that the shopify store user can choose in the settings. Right now everything works if I assign a certain collection but that doesn't let me change it dynamically.

<div id="slideshow">
  <div class="wrapper">
    <div class="rslides_container">
        <ul class="rslides" id="slider1">
            {% assign _collection = collections['new-products'] %}
            {% for product in _collection.products %}
              <li class="slideshow-item">
                  <a target="_blank" rel="nofollow noreferrer" href="{{ product.url }}"><img src="{{  product.featured_image | img_url: 'medium' | format: 'jpg' }}" alt="{{ block.settings.slide_heading }}"></a>            
                  <h3>{{ product.title }}</h3>
                  <h4>{{ product.price | money | remove: '.00' }}</h4>
                  <p>{{ product.description }}</p>
                  {% include 'buybutton' %}
              </li>

            {% endfor %} 
            </ul>
        </div><!-- #slideshow-container -->
    </div>
</div><!-- #slideshow -->
{% schema %}
  {
    "name": "Slideshow",
    "settings": [
        {
            "type": "collection",
            "id": "feature_collection",
            "label": "Cho: "collection",
            "id": "feature_collection",
            "label": "Chose Collection to show on slideshow"
        }
    ]
  }
{% endschema %}

I thought changing the for loop to:

 {% for product in collections[settings.feature_collection].products %}

Would work but then it just shows nothing on the slider no matter what collection I choose in the theme customizer. Has anyone done this or know how to do it? Thanks!

Ad

Answer

I figured it out. I left out one part in the loop statement.

{% for product in collections[section.settings.feature_collection].products %}

I was missing the word section before .settings

Ad
source: stackoverflow.com
Ad