<p>Here's a simple solution that might be viable for your scenario:</p>

<pre><code>{% set selected = false %}
{% for purchasable in product.variants %}
    {% if selected == false and (purchasable.stock &gt; 0 or purchasable.unlimitedStock) %}
        {# output selected radio #}
        {% set selected = true %}
    {% else %}
        {# output unselected radio #}
    {% endif %}
{% endfor %}
</code></pre>

<p>Essentially set a variable (<code>selected</code> in this case) that once set cannot be re-set, and is only set under the circumstances you mentioned (stock available or unlimitedStock set to true).</p>

CC BY-SA 3.0
2016-10-11T20:48:16.423Z
That did the trick perfectly! Can't believe how simple that was, borderline ashamed I didn't think of it.
Anders
CC BY-SA 3.0
2016-10-10T16:00:42.430Z
2016-10-10T16:00:42.430Z
Stephen Hamilton
<p>Check out the <a href="https://github.com/marionnewlevant/craft-twig_perversion" rel="nofollow">MN Twig Perversion</a> plugin. It adds a <code>break</code> Twig tag, which allows you to escape from a loop once you have the data you need. </p>

CC BY-SA 3.0
2016-10-09T00:36:41.790Z
Trying to avoid adding too many plugins, but this seems useful nonetheless. Many thanks for the tip!
Anders
CC BY-SA 3.0
2016-10-08T14:49:57.203Z
2016-10-08T14:49:57.203Z
Lindsey D
<p>Realizing Twig can't escape a <code>for</code> loop, how would one go about setting a variable for the current item and not the following?</p>

<p>Scenario;
Color swatches are radio buttons, if default purchasable stock &lt;= 0 and default purchasable unlimitedStock == false, the next radio button should be checked, and if this has stock &lt;= 0 and so on, you get the idea, the next would be checked.</p>

<p>I can't seem to find a way to only check the next, and not all the following radio buttons if previous purchasable in the loop is out of stock.</p>

<p>A lot of checking of the checkers being checked here. Hah.</p>

<plugin-craftcommerce>
Any ideas on how to set/output something if the previous variant in a for loop has a parameter?
# Any ideas on how to set/output something if the previous variant in a for loop has a parameter?

(Asked by: username_0 on 2016-10-08)

Realizing Twig can't escape a `for` loop, how would one go about setting a variable for the current item and not the following?

Scenario;
Color swatches are radio buttons, if default purchasable stock <= 0 and default purchasable unlimitedStock == false, the next radio button should be checked, and if this has stock <= 0 and so on, you get the idea, the next would be checked.

I can't seem to find a way to only check the next, and not all the following radio buttons if previous purchasable in the loop is out of stock.

A lot of checking of the checkers being checked here. Hah.

**Tags:** plugin-craftcommerce

---

## 2 answers

### Answer by username_1 on 2016-10-10. Score: 1 | Accepted answer

Here's a simple solution that might be viable for your scenario:
{% set selected = false %}
{% for purchasable in product.variants %}
    {% if selected == false and (purchasable.stock > 0 or purchasable.unlimitedStock) %}
        {# output selected radio #}
        {% set selected = true %}
    {% else %}
        {# output unselected radio #}
    {% endif %}
{% endfor %}

Essentially set a variable (selected in this case) that once set cannot be re-set, and is only set under the circumstances you mentioned (stock available or unlimitedStock set to true).

> **Comments:**
> **username_0**: That did the trick perfectly! Can't believe how simple that was, borderline ashamed I didn't think of it.  

---

### Answer by username_2 on 2016-10-08. Score: 1

Check out the [MN Twig Perversion](https://github.com/marionnewlevant/craft-twig_perversion) plugin. It adds a `break` Twig tag, which allows you to escape from a loop once you have the data you need.

> **Comments:**
> **username_0**: Trying to avoid adding too many plugins, but this seems useful nonetheless. Many thanks for the tip!  

---

