Dawn Theme: Custom Section: Blank Spaces are showing up in my collections section

Concept: I have built a custom section on the home page that should only show the first variant of products within a collection. NOTE: Gift with purchase is offered with specific variant sizes only, and size varies from product to product.

Attempted Solution: Use “variant.weight” as a selector and only show items where the variant has inventory available and is the correct weight. NOTE: This store will not ship via Shopify & does not need to use weight for shipping purposes.

Issue/Problem: There are blank spots where items in the collection do not qualify either because the variant does not meet the weight requirement or there is no available inventory.

Ask:

  1. How can I fix this code so that the blank spaces go away? I assume I need to add {% else %} code, but I have no idea what to add to that line. I tried {% continue %} and that did not accomplish what I was looking to accomplish.
  2. Can I show out-of-stock items as “Sold Out” rather than have them removed? (I realize I wrote the code to remove items with no inventory, I iterate through my code as I am still learning)
  3. Is there a more elegant fix/solution that I missed?

Site: https://test-store-colleen.myshopify.com/ Password: thanksforyourhelp

{% comment %}
    Renders a product card

    Accepts:
    - product_card_product: {Object} Product Liquid object (optional)
    - media_size: {String} Size of the product image card. Values are "square" and "portrait". Default is "square" (optional)
    - show_secondary_image: {Boolean} Show the secondary image on hover. Default: false (optional)
    - add_image_padding: {Boolean} Enables padding on the image to space out the grid
    - show_vendor: {Boolean} Show the product vendor. Default: false
    - show_image_outline: {Boolean} Show card outline. Default: true (optional)
    - show_rating: {Boolean} Show the product rating. Default: false

    Usage:
    {% render 'product-card-gwp', show_vendor: section.settings.show_vendor %}
{% endcomment %}

{{ 'component-rating.css' | asset_url | stylesheet_tag }}

    {%- for variant in product_card_product.variants -%}
    {% assign theweight = variant.weight | weight_with_unit: variant.weight_unit %}
    {%- if theweight == '9 g' and variant.inventory_quantity != 0 -%}    

  {% capture the_snippet_fave_icon %}{% render 'ssw-widget-faveicon' with product.id %}{% endcapture %}
  {% unless the_snippet_fave_icon contains 'Liquid error' %}
  {{ the_snippet_fave_icon }}
  {% endunless %}
  
  

    

      {%- if show_vendor -%}
        {{ 'accessibility.vendor' | t }}
        
{{ product_card_product.vendor }}

      {%- endif -%}

      {%- if product_card_product.featured_media -%}
        ### 
          
            {{ product_card_product.title | escape }}
          
        
      {%- endif -%}
      
      {% capture the_snippet_review_avg %}{% render 'ssw-widget-avg-rate-listing', product: product %}{% endcapture %}
      {% unless the_snippet_review_avg contains 'Liquid error' %}
      {{ the_snippet_review_avg }}
      {% endunless %}

      {% comment %} TODO: metafield 
      {{ block.settings.description | escape }}
      {%- if show_rating and product_card_product.metafields.reviews.rating.value != blank -%}
        {% liquid
          assign rating_decimal = 0 
          assign decimal = product_card_product.metafields.reviews.rating.value.rating | modulo: 1 
          if decimal >= 0.3 and decimal <= 0.7
            assign rating_decimal = 0.5
          elsif decimal > 0.7
            assign rating_decimal = 1
          endif 
        %}
        

          
        

        

          {{ product_card_product.metafields.reviews.rating.value }} / {{ product_card_product.metafields.reviews.rating.value.scale_max }}
        

        

          ({{ product_card_product.metafields.reviews.rating_count }})
          {{ product_card_product.metafields.reviews.rating_count }} {{ "accessibility.total_reviews" | t }}
        

      {%- endif -%}
      {% endcomment %}
 
{% comment %}      
     Original code NOTE: Also added line for "Free Shipping" below
     {% render 'price', product: product_card_product, price_class: '' %}
      
     Colleen modified code below to show the first variant price under the product on the collection gwp page     
      
{% endcomment %}
      
      {% render 'price', product: product_card_product, use_variant: product_card_product.first_available_variant.id, price_class: price--large -%} 
            FREE SHIPPING 

         

    

  

    
  
    

      {%- if product_card_product.featured_media -%}
        {%- liquid
          assign featured_media_aspect_ratio = product_card_product.featured_media.aspect_ratio

          if product_card_product.featured_media.aspect_ratio == nil
            assign featured_media_aspect_ratio = 1
          endif
        -%}

        

      {%- else -%}
        
          ## 
            
              {{ product_card_product.title }}
            
          
        

      {%- endif -%}

      
        {%- if product_card_product.available == false -%}
          {{ 'products.product.sold_out' | t }}
        {%- elsif product_card_product.compare_at_price > product_card_product.price and product_card_product.available -%}
          {{ 'products.product.on_sale' | t }}
        {%- endif -%}
        
      

    

  

{% comment %} 
Code added by Colleen to create add to cart button 
{% endcomment %}

    {% endif %}          
    {% endfor %}

@colleenc ,

  1. In your Shopify Admin go to online store > themes > actions > edit code
  2. In your theme.liquid file, find the (press CTRL + F or command + F on Mac)
  3. paste this code right above the tag:

This will work as a quick fix, but it’s not the right way to go about it, it could yield more issues in the future. If this is not a test project, I’d highly recommend contacting a Shopify developer to debut the code and get it properly fixed so you don’t have any headaches in the future in case things break.

Kind regards,
Diego

Thank you! This does work - however, I do understand what you say about it potentially breaking other things in the future. I can’t understand why Shopify code will not allow us to limit a collection to “#” variant only, especially the first variant only as there could be a lot of applications where that makes sense. Workarounds drive me crazy because they can cause unintended issues elsewhere on the site. Thanks again!