d
Topic
Posts:
1
7 months ago

How to use an unless statement before a for loop begins

I have a related products snippet that I am using to display 3 recent products that are inside of a "related" collection. I am using the following logic:

{% assign: thisproduct = product.title %}
{% if collections.related.products.size > 0 %}
<!-- START RELATED -->
<div id="related">
  <h6>{% if settings.related.size > 0 %}{{ settings.related | escape }}{% else %}You also might like:{% endif %}</h6>
		<!-- START WINDOW -->
		<div id="window">
			<ul class="clear">
              {% for product in collections.related.products limit:3 %}
              {% unless product.title == thisproduct %}
				<li>
					<div class="image clear">
						<a href="{{ product.url | within: collections.all }}"><img src="{{ product.images[0] | product_img_url: "medium" }}" alt="{{ product.title | escape }}" /></a>
					</div>
					<h4><a href="{{ product.url | within: collections.all }}">{{ product.title | escape }}</a></h4>
					<p>{{ product.price | money }}</p>
				</li>
              {% endunless %}
              {% endfor %}
			</ul>
		</div>
		<!-- END WINDOW -->
</div>
<!-- END RELATED -->{% else %}
<p id="unrelated"><a href="/admin/custom_collections">Create a collection</a> with the handle of <strong>related</strong> and add some products to it for them to be displayed here.</p>{% endif %}

I am having trouble with the 

{% for product in collections.related.products limit:3 %}
              {% unless product.title == thisproduct %}

section.

I am running into a situation where if the product is towards the beginning of the collection, when the for loop is ran, it is included as one of the three products in the array, but is then excluded with the unless statement, so only two products show up.

I want to be able to filter the current product during the for loop so that I will always get 3 products displaying.

Alternatively, if anybody has suggestions as to how to randomize the selection, that would also be wonderful. 

Thoughts? Am I missing something?

i
Replies
Posts:
5632
7 months ago

You need to fetch 4 products and show the 4th conditionally, like so:

{% assign number_of_related_products_to_show = 3 %}
{% assign current_product = product %}
{% assign current_product_found = false %}
{% assign number_of_related_products_to_fetch = number_of_related_products_to_show | plus: 1 %}

{% if collections.related.products.size > 0 %}
<div id="related">
  <h6>{% if settings.related.size > 0 %}{{ settings.related }}{% else %}You also might like:{% endif %}</h6>
  <div id="window">
    <ul class="clear">
    {% for product in collections.related.products limit: number_of_related_products_to_fetch %}
      {% if product.handle == current_product.handle %}
        {% assign current_product_found = true %}
      {% else %}
        {% unless current_product_found == false and forloop.last %}
        <li>
          <div class="image clear">
            <a href="{{ product.url | within: collections.all }}"><img src="{{ product.images[0] | product_img_url: "medium" }}" alt="{{ product.title | escape }}" /></a>
          </div>
          <h4><a href="{{ product.url | within: collections.all }}">{{ product.title | escape }}</a></h4>
          <p>{{ product.price | money }}</p>
        </li>
        {% endunless %}
      {% endif %}
    {% endfor %}
    </ul>
  </div><!-- end #window -->
</div><!-- end #related -->
{% endif %}

 

Caroline from http://11heavens.com ∴ mllegeorgesand AT gmail DOT com

Log in or sign up for an account to reply.

This thread has been closed! You will not be able to reply.