Solved

List of variants of product X, shown on productpage of product Z

Pavis
Tourist
6 0 0

Hey everyone,

 

I'm trying to display a list of available variants on my product page via Liquid Code. I've already found a code to do this for the current product. The catch is that I'm trying to do this for a product that I've linked via variant.metafields.

 

Let me explain:

I've linked product X to product Z via a variant.metafield.

On the productpage of product Z, I would like to show a list of available variants for product X.

 

This is the code that I've found to do this for the current product. Does anyone know what needs to be changed in order to do this for the linked-product?

 

{% if product.variants.size > 0 and product.available %} {% unless product.has_only_default_variant %}
<span class="sizes-available">

This tile is available in these sizes:
</br>

{% for variant in product.variants %} {% if variant.available %}

{{ variant.title | append: '</br>'}}

{% endif %}
{% endfor %}

</span>
{% endunless %}
{% endif %}

 

 

Thanks for your help in advance!

 

Accepted Solution (1)

tim
Shopify Expert
3559 300 1322

This is an accepted solution.

Some details are missing in your post, so let's assume that for your products you've defined a "product reference"/single type metafield with namespace "custom" and key "linked".

Then the code may look like:

{% assign metafield = product.metafields.custom.linked %}
{% if metafield != blank %}
  {% assign linked_product = metafield.value %}
  {% unless linked_product.has_only_default_variant %}
    {% assign available_variants = linked_product.variants | where: 'available' %}
    {% if available_variants.size > 0 %}
      <span class="sizes-available">
        {% for linked_variant in available_variants %}
          {{ linked_variant.title }}<br />
        {% endfor %}
      </span>
    {% endif %}
  {% endunless %}
{% endif %}

 

https://shopify.dev/docs/api/liquid/objects/metafield 

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com

View solution in original post

Replies 2 (2)

tim
Shopify Expert
3559 300 1322

This is an accepted solution.

Some details are missing in your post, so let's assume that for your products you've defined a "product reference"/single type metafield with namespace "custom" and key "linked".

Then the code may look like:

{% assign metafield = product.metafields.custom.linked %}
{% if metafield != blank %}
  {% assign linked_product = metafield.value %}
  {% unless linked_product.has_only_default_variant %}
    {% assign available_variants = linked_product.variants | where: 'available' %}
    {% if available_variants.size > 0 %}
      <span class="sizes-available">
        {% for linked_variant in available_variants %}
          {{ linked_variant.title }}<br />
        {% endfor %}
      </span>
    {% endif %}
  {% endunless %}
{% endif %}

 

https://shopify.dev/docs/api/liquid/objects/metafield 

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com
Pavis
Tourist
6 0 0

Thank you, Tim! This is indeed exactly what I was looking for!