If/elsif code conditional logic isn't working for me creating buy now vs sold out buttons

I have been bashing my head against this code all day. I’ve managed to get it to work almost perfectly, but the “Sold Out” buttons are stacking under one product instead of showing under each of the two products which are sold out.

Use case: Gift with Product promotion. I have used weight as a selector to work with only one variant per product as this gift with purchase offer is only valid with certain product sizes. While I have written the code to completely remove the product once it is sold out, I would prefer to have a “sold out” button show up when a specific variant is out of stock. Note: different sizes of the product may still be in stock, but the one associated with the gift with purchase is sold out and therefore I would like to have a “sold out” button come up.

I am including the code as well as an image of the section as it is currently rendering so you can see what is happening.

The code I have added is at the bottom of the file. I assume my for loop isn’t correctly structured but I can’t figure out what I got wrong. Thanks in advance for taking a look!

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

  
  
  

    

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

      {%- endif -%}

      {%- if product_card_product.featured_media -%}
        ### 
          
            {{ product_card_product.title | escape }}
          
        
      {%- endif -%}

      {%- 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 -%}       
      
      {% render 'price', product: product_card_product, use_variant: product_card_product.first_available_variant.id, price_class: price--large -%} 
            FREE SHIPPING 

              

      {% continue %}
      {% endif %}
      {% endfor %}
    

  
     
    
  
    

      {%- 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 -%}
        
      

    

  

    {%- 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 -%} 

        
     {% elsif theweight == '9 g' and variant.inventory_quantity == 0 %}
      
         
            	            
              
		        SOLD OUT
  		      
             
      	  

    {% endif %}          
    {% endfor %}

Problem solved! There was an issue with the configuration of the products not in the script. So if you were looking to do something like this in your store, the code above works like a charm. Also, this script lets you call a specific variant using weight as the selector - will only be useable if you aren’t shipping from the store, but this can be helpful if you are trying to do something like I was with only one size/variant specified in a collection.