<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>

CC BY-SA 4.0
2022-04-19T19:37:21.633Z
Looks like you're using Sprig as well?
RitterKnight
CC BY-SA 4.0
2022-04-20T20:46:42.183Z
Yes, that’s correct
Lukas Snarskis
CC BY-SA 4.0
2022-05-02T11:45:09.433Z
Anyone can help me out in this case?
Lukas Snarskis
<plugin-craftcommerce><plugin-sprig><variants>
Craft Commerce variants and stock limit
# 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() %}


  
  
 Pasirinkite stotelės spalvą
           
      {% for colourOption in colours %}

    
     
    {{ colourOption.title }}


       {% endfor %}
  
  

    
    Parinkome laido ilgį
                        Į plana įskaičiuotas 5m integruotas laidas (už papildomą mokestį galima pasirinkti ilgesnį).
           
      {% for lengthOption in lengths %}
      
    
    
          {{ lengthOption.title }}


      {% endfor %}
    
  
  
    {% if colour and length %}
    {% for variant in variants %}
      

   {% set price = variant.price / 24 %}
{{ price|number(decimals=2) }} €/mėn.
Sutartis 24 mėn
Pradinė įmoka 0 €


    {{ variant.salePrice }} €

          
           
    {% endfor %}

             
            
  Pridėti į mano krepšelį


 
            
 {% if addedToCart and success %}
    was added to your cart!
  {% endif %}
  

    {% else %}
    {% set products = craft.products()
  .id(productId)
  .all() %}
{% for product in products %}

    {% set price = product.defaultVariant.salePrice / 24 %}
      

{{ price|number(decimals=2) }} €/mėn.
Sutartis 24 mėn
Pradinė įmoka 0 €


    {{ product.defaultVariant.salePrice }} €

          
{% endfor %}
    {% endif %}
  


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

