Body: <p>I'm working on solution that shows product variants with ability to select attributes in the front end. Now I have code like this:</p>
  <pre><code>{% set colour = colour ?? [] %}
  {% set length = length ?? [] %}
  {% set addedToCart = success is defined %}
  {% set colours = craft.categories().group('spalva').relatedTo(productId).all() %}
  {% set lengths = craft.categories().group('ilgis').relatedTo(productId).all() %}
  {% set variantQuery = craft.variants().productId(productId) %}
  {% if colour %}
      {% do variantQuery.andRelatedTo(colour) %}
  {% endif %}
  {% if length %}
      {% do variantQuery.andRelatedTo(length) %}
  {% endif %}
  
  
  {% do variantQuery %}
  {% set variants = variantQuery.all() %}
  
  &lt;form sprig s-method=&quot;post&quot; s-action=&quot;commerce/cart/update-cart&quot;&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;qty&quot; value=&quot;1&quot;&gt;
    &lt;fieldset&gt;
   &lt;p class=&quot;is-size-4 has-text-black mb-3&quot;&gt;Pasirinkite stotelės spalvą&lt;/p&gt;
             &lt;div class=&quot;columns is-multiline&quot;&gt;
        {% for colourOption in colours %}
  &lt;div class=&quot;column is-6&quot;&gt;
      &lt;label class=&quot;labl&quot;&gt;
       &lt;input sprig type=&quot;radio&quot; name=&quot;colour&quot; value=&quot;{{ colourOption.id }}&quot; {{ colour == colourOption.id ? 'checked' }}&gt;
      &lt;div&gt;{{ colourOption.title }}&lt;/div&gt;
  &lt;/label&gt;
  &lt;/div&gt;
         {% endfor %}
    &lt;/div&gt;
    &lt;/fieldset&gt;
  
      &lt;fieldset&gt;
      &lt;p class=&quot;is-size-4 has-text-black&quot;&gt;Parinkome laido ilgį&lt;/p&gt;
                          &lt;p class=&quot;is-size-7 has-text-black mb-3&quot;&gt;Į plana įskaičiuotas 5m integruotas laidas (už papildomą mokestį galima pasirinkti ilgesnį).&lt;/p&gt;
             &lt;div class=&quot;columns is-multiline&quot;&gt;
        {% for lengthOption in lengths %}
        &lt;div class=&quot;column is-6&quot;&gt;
      &lt;label class=&quot;labl&quot;&gt;
      &lt;input sprig type=&quot;radio&quot; name=&quot;length&quot; value=&quot;{{ lengthOption.id }}&quot; {{ length == lengthOption.id ? 'checked' }}&gt;
            &lt;div&gt;{{ lengthOption.title }}&lt;/div&gt;
  &lt;/label&gt;
  &lt;/div&gt;
        {% endfor %}
      &lt;/div&gt;
    &lt;/fieldset&gt;
    &lt;fieldset class=&quot;mb-8&quot;&gt;
      {% if colour and length %}
      {% for variant in variants %}
        &lt;div class=&quot;columns is-multiline has-border pt-3 pb-3 mt-5 has-text-black&quot;&gt;
  &lt;div class=&quot;column is-half&quot;&gt;
     {% set price = variant.price / 24 %}
  &lt;p class=&quot;mb-2&quot;&gt;&lt;span data-variant-price-full class=&quot;is-size-5 has-text-weight-normal&quot;&gt;{{ price|number(decimals=2) }}&lt;/span&gt;&lt;span class=&quot;is-size-7&quot;&gt; €/mėn.&lt;/span&gt;&lt;/p&gt;
  &lt;p class=&quot;is-size-7&quot;&gt;Sutartis 24 mėn&lt;br/&gt;
  Pradinė įmoka 0 €&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class=&quot;column is-half has-text-right&quot;&gt;
      &lt;span class=&quot;is-size-5 has-text-weight-normal&quot;&gt;&lt;span data-variant-price-full&gt;{{ variant.salePrice }}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;is-size-7&quot;&gt; €&lt;/span&gt;
  &lt;/div&gt;
            &lt;/div&gt;
             &lt;input style=&quot;display: none!important&quot; type=&quot;radio&quot; name=&quot;purchasableId&quot; value=&quot;{{ variant.id }}&quot; checked&gt;
      {% endfor %}
  
               &lt;div class=&quot;columns mt-5&quot;&gt;
              &lt;div class=&quot;column is-12&quot;&gt;
    &lt;button type=&quot;submit&quot; class=&quot;button is-fullwidth is-cart&quot;&gt;Pridėti į mano krepšelį&lt;/button&gt;
  &lt;/div&gt;
  &lt;/div&gt;
   &lt;div class=&quot;columns mt-5&quot;&gt;
              &lt;div class=&quot;column is-12&quot;&gt;
   {% if addedToCart and success %}
      was added to your cart!
    {% endif %}
    &lt;/div&gt;
  &lt;/div&gt;
      {% else %}
      {% set products = craft.products()
    .id(productId)
    .all() %}
  {% for product in products %}
  
      {% set price = product.defaultVariant.salePrice / 24 %}
        &lt;div class=&quot;columns is-multiline has-border pt-3 pb-3 mt-5 has-text-black&quot;&gt;
  &lt;div class=&quot;column is-half&quot;&gt;
  &lt;p class=&quot;mb-2&quot;&gt;&lt;span data-variant-price-full class=&quot;is-size-5 has-text-weight-normal&quot;&gt;{{ price|number(decimals=2) }}&lt;/span&gt;&lt;span class=&quot;is-size-7&quot;&gt; €/mėn.&lt;/span&gt;&lt;/p&gt;
  &lt;p class=&quot;is-size-7&quot;&gt;Sutartis 24 mėn&lt;br/&gt;
  Pradinė įmoka 0 €&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class=&quot;column is-half has-text-right&quot;&gt;
      &lt;span class=&quot;is-size-5 has-text-weight-normal&quot;&gt;&lt;span data-variant-price-full&gt;{{ product.defaultVariant.salePrice }}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;is-size-7&quot;&gt; €&lt;/span&gt;
  &lt;/div&gt;
            &lt;/div&gt;
  {% endfor %}
      {% endif %}
    &lt;/fieldset&gt;
  &lt;/form&gt;
  </code></pre>
  <p>Everything is working fine but I'm looking for the solution on stock check. Let's say I have two colour options - Black and White and both of them can be 2 or 5 metres length. I need to check if any option with the Black have stock and if not - disable of choosing Black input. If one of two exist - let user to choose Black but then show that 2 or 5 metres are out of stock.</p>
  <p>Can someone help me out with this?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-04-19T19:37:21.633Z
Comments.Text: Looks like you're using Sprig as well?
Comments.UserDisplayName: RitterKnight
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-04-20T20:46:42.183Z
Comments.Text: Yes, that’s correct
Comments.UserDisplayName: Lukas Snarskis
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-05-02T11:45:09.433Z
Comments.Text: Anyone can help me out in this case?
Comments.UserDisplayName: Lukas Snarskis
Tags: <plugin-craftcommerce><plugin-sprig><variants>
Title: Craft Commerce variants and stock limit
ThreadText: # Craft Commerce variants and stock limit
  
  (Asked by: username_0 on 2022-04-15)
  
  I'm working on solution that shows product variants with ability to select attributes in the front end. Now I have code like this:
  
  ```
  {% set colour = colour ?? [] %}
  {% set length = length ?? [] %}
  {% set addedToCart = success is defined %}
  {% set colours = craft.categories().group('spalva').relatedTo(productId).all() %}
  {% set lengths = craft.categories().group('ilgis').relatedTo(productId).all() %}
  {% set variantQuery = craft.variants().productId(productId) %}
  {% if colour %}
      {% do variantQuery.andRelatedTo(colour) %}
  {% endif %}
  {% if length %}
      {% do variantQuery.andRelatedTo(length) %}
  {% endif %}
  
  {% do variantQuery %}
  {% set variants = variantQuery.all() %}
  
  <form sprig s-method="post" s-action="commerce/cart/update-cart">
    <input type="hidden" name="qty" value="1">
    <fieldset>
   <p class="is-size-4 has-text-black mb-3">Pasirinkite stotelės spalvą</p>
             <div class="columns is-multiline">
        {% for colourOption in colours %}
  <div class="column is-6">
      <label class="labl">
       <input sprig type="radio" name="colour" value="{{ colourOption.id }}" {{ colour == colourOption.id ? 'checked' }}>
      <div>{{ colourOption.title }}</div>
  </label>
  </div>
         {% endfor %}
    </div>
    </fieldset>
  
      <fieldset>
      <p class="is-size-4 has-text-black">Parinkome laido ilgį</p>
                          <p class="is-size-7 has-text-black mb-3">Į plana įskaičiuotas 5m integruotas laidas (už papildomą mokestį galima pasirinkti ilgesnį).</p>
             <div class="columns is-multiline">
        {% for lengthOption in lengths %}
        <div class="column is-6">
      <label class="labl">
      <input sprig type="radio" name="length" value="{{ lengthOption.id }}" {{ length == lengthOption.id ? 'checked' }}>
            <div>{{ lengthOption.title }}</div>
  </label>
  </div>
        {% endfor %}
      </div>
    </fieldset>
    <fieldset class="mb-8">
      {% if colour and length %}
      {% for variant in variants %}
        <div class="columns is-multiline has-border pt-3 pb-3 mt-5 has-text-black">
  <div class="column is-half">
     {% set price = variant.price / 24 %}
  <p class="mb-2"><span data-variant-price-full class="is-size-5 has-text-weight-normal">{{ price|number(decimals=2) }}</span><span class="is-size-7"> €/mėn.</span></p>
  <p class="is-size-7">Sutartis 24 mėn<br/>
  Pradinė įmoka 0 €</p>
  </div>
  <div class="column is-half has-text-right">
      <span class="is-size-5 has-text-weight-normal"><span data-variant-price-full>{{ variant.salePrice }}</span></span><span class="is-size-7"> €</span>
  </div>
            </div>
             <input style="display: none!important" type="radio" name="purchasableId" value="{{ variant.id }}" checked>
      {% endfor %}
  
               <div class="columns mt-5">
              <div class="column is-12">
    <button type="submit" class="button is-fullwidth is-cart">Pridėti į mano krepšelį</button>
  </div>
  </div>
   <div class="columns mt-5">
              <div class="column is-12">
   {% if addedToCart and success %}
      was added to your cart!
    {% endif %}
    </div>
  </div>
      {% else %}
      {% set products = craft.products()
    .id(productId)
    .all() %}
  {% for product in products %}
  
      {% set price = product.defaultVariant.salePrice / 24 %}
        <div class="columns is-multiline has-border pt-3 pb-3 mt-5 has-text-black">
  <div class="column is-half">
  <p class="mb-2"><span data-variant-price-full class="is-size-5 has-text-weight-normal">{{ price|number(decimals=2) }}</span><span class="is-size-7"> €/mėn.</span></p>
  <p class="is-size-7">Sutartis 24 mėn<br/>
  Pradinė įmoka 0 €</p>
  </div>
  <div class="column is-half has-text-right">
      <span class="is-size-5 has-text-weight-normal"><span data-variant-price-full>{{ product.defaultVariant.salePrice }}</span></span><span class="is-size-7"> €</span>
  </div>
            </div>
  {% endfor %}
      {% endif %}
    </fieldset>
  </form>
  
  ```
  
  Everything is working fine but I'm looking for the solution on stock check. Let's say I have two colour options - Black and White and both of them can be 2 or 5 metres length. I need to check if any option with the Black have stock and if not - disable of choosing Black input. If one of two exist - let user to choose Black but then show that 2 or 5 metres are out of stock.
  
  Can someone help me out with this?
  
  **Tags:** plugin-craftcommerce, plugin-sprig, variants
  
  > **Comments:**
  > **username_1**: Looks like you're using Sprig as well?  
  > **username_0**: Yes, that’s correct  
  > **username_0**: Anyone can help me out in this case?  
  
  ---
  
  ## 0 answers
  
  