Ad

Only Displaying Available Options On Shopify

- 1 answer

In Snippets/product-form.liquid there's a block of code:

<div class="swatch_options">
  {% for option in product.options %}
    {% include 'product-swatch' with option %}
  {% endfor %}
</div>

This is displaying options for products with X's over the unavailable swatches:

<div data-value="option name" class="swatch-element color optionName-swatch available soldout">
  <div class="tooltip">Option Name</div>
  <label for="optionName">
    <span class="crossed-out"></span>
  </label>
</div>

When those non-compatible variants are clicked, a large "UNAVAILABLE" message is displayed:

<p class="modal_price" itemprop="offers" itemscope="" itemtype="http://schema.org/Offer">
  <meta itemprop="priceCurrency" content="USD">
  <meta itemprop="seller" content="site">
  <link itemprop="availability" target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="http://schema.org/InStock"> 
  <meta itemprop="itemCondition" content="New">
  <span class="sold_out">Unavailable</span>
  <span itemprop="price" content="10.00" class="">
    <span class="current_price "></span>
  </span>
  <span class="was_price"></span>
  <span class="sale savings"></span>
</p>

I've tried checking for variant.available, product.variants.first.available, and variant.inventory_quantity > 0 before {% include 'product-swatch' with option %}, without success.

How do I hide the non-compatible variants?

Edit: Here's what's currently inside of product-swatch.liquid:

{% comment %}
  Set the extension of your color files below. Use 'png', 'jpeg', 'jpg' or 'gif'.
{% endcomment %}

{% assign file_extension = 'png' %}

{% assign swatch = product-swatch %}
{% assign found_option = false %}
{% assign is_color = false %}
{% assign option_index = 0 %}

{% for option in product.options %}
  {% if option == swatch %}
    {% assign found_option = true %}
    {% assign option_index = forloop.index0 %}
    {% assign downcased_option = swatch | downcase %}
    {% if downcased_option contains 'color' or downcased_option contains 'colour' %}
      {% assign is_color = true %}
    {% endif %}
  {% endif %}
{% endfor %}

<div class="swatch clearfix" data-option-index="{{ option_index }}">
  <div class="option_title">{{ swatch }}</div>
  {% assign values = '' %}
  {% for variant in product.variants %}
    {% if variant.available %}
    {% assign value = variant.options[option_index] %}
    {% unless values contains value %}
      {% assign values = values | join: ',' %}
      {% assign values = values | append: ',' | append: value %}
      {% assign values = values | split: ',' %}
      <input id="swatch-{{ option_index }}-{{ value | handle }}-{{ product.id }}" type="radio" name="option-{{ option_index }}" value="{{ value | escape }}"{% if forloop.first %} checked{% endif %} />
      <div data-value="{{ value | escape }}" class="swatch-element {% if is_color %}color {% endif %}{{ value | handle }}-swatch {% if variant.available %}available{% else %}soldout{% endif %}">
        {% if is_color %}
          <div class="tooltip">{{ value }}</div>
        {% endif %}
        {% if is_color %}
          <label for="swatch-{{ option_index }}-{{ value | handle }}-{{ product.id }}" style="background-image: url({{ value | handle | append: '.' | append: file_extension | asset_img_url: '50x' }}); background-color: {{ value | split: ' ' | last | handle }};">
            <span class="crossed-out"></span>
          </label>
        {% else %}
          <label for="swatch-{{ option_index }}-{{ value | handle }}-{{ product.id }}">
            {{ value }}
            <span class="crossed-out"></span>
          </label>
        {% endif %}
      </div>
    {% endunless %}
    {% endif %}
  {% endfor %}
</div>
Ad

Answer

It seems you're looking for the Linked Options functionality.

Have a look at this doc: https://help.shopify.com/themes/customization/navigation/link-product-options-in-menus

The code is hosted on GitHub: https://gist.github.com/carolineschnapp/1083007

It's adjusted to work with swatches as well (I suppose you're using the default implementation).

Ad
source: stackoverflow.com
Ad