Re: Adding variant metafield to product in empire

Adding variant metafield to product in empire

Inscale
New Member
4 0 0

I am trying to add a variant metafield that displays a pdf image with a url link to the relevant cds file, i have got the product ones working but not getting anything for the variant, please be gentle i am not a coder, just trying to work my way through this issue  (code is in a custom liquid file and is referenced in theme product page)

{% for item in product.metafields.custom.technical_datasheet.value -%}
{%- if item != blank -%}
<div class= "pdf-child">
<a href={{ item }} target="_blank" rel="noopener noreferrer"><img src="https://cdn.shopify.com/s/files/1/0606/5150/3668/files/images.png?v=1661969450" alt="PDF Document" width="60"></a>
</br><strong>Technical Datasheet</strong>
</br></div>

{%- endif -%}
{%- endfor %}

 

{% for item in variant.metafields.custom.technical_data.value -%}
{%- if item != blank -%}
<div class= "pdf-child">
<a href={{ item }} target="_blank" rel="noopener noreferrer"><img src="https://cdn.shopify.com/s/files/1/0606/5150/3668/files/images.png?v=1661969450" alt="PDF Document" width="60"></a>
</br><strong>Model Data</strong>
</br></div>

{%- endif -%}
{%- endfor %}

Replies 6 (6)

Huptech-Web
Shopify Partner
1152 229 258

Hi @Inscale 
You have to loop through the variants

Check below code

{% for variant in product.variants %}
{% for item in variant.metafields.custom.technical_data.value -%}
{%- if item != blank -%}
<div class= "pdf-child">
<a href={{ item }} target="_blank" rel="noopener noreferrer"><img src="https://cdn.shopify.com/s/files/1/0606/5150/3668/files/images.png?v=1661969450" alt="PDF Document" width="60"></a>
</br><strong>Model Data</strong>
</br></div>

{%- endif -%}
{%- endfor -%}
{%- endfor -%}
If you found this response helpful, please do like and accept the solution. Thanks!
Need support with Customizing your Shopify store?
Feel free to contact me at info@huptechweb.com or Visit our website Huptech Web.
Instant Shortcode Builder: Integrate customizable UI features anywhere in your store - No coding knowledge required
Inscale
New Member
4 0 0

Thank you Rishihuptech, unfortunately that did not work, so i am probably missing something else, probably somewhere else to.  But thank you for reply, very kind

gr_trading
Shopify Partner
2040 149 206

Hi @Inscale ,

 

Just to remind code you are using is for reading variant metafield of metaobject type.

{% for item in product.metafields.custom.technical_datasheet.value -%}

.value we use for metaobject.

 

for metafield please use below one code

 

{% for variant in product.variants %}
{{ variant.metafields.custom.technical_datasheet | file_url }}
{% endfor %}

 

file_url filter used for file type metafield.

 

Hope this will helps...

 

For any custom development WhatsApp or connect at Email ID: support@grtrading.in for quick consultation. | Shopify Free codes
To support Buy Me a Coffee
Inscale
New Member
4 0 0

Thank you for the detail.  The code used (with .value) was written before metaobjects was available and it works for the 'product' links, what i am trying to add is a url for the variant, sounds more and more complicated as i type it, but file_url just returns 3 text lines ending in ?498 which I'm guessing is an error code.  As i say I'm no coder I'm just trying to logically work my way through the problem, but thank you for the detail does seam to get me a step closer if not the answer, appreciated 

gr_trading
Shopify Partner
2040 149 206

best would be to share a video how you have created metafield at variant and how this info you want on the page. 

For any custom development WhatsApp or connect at Email ID: support@grtrading.in for quick consultation. | Shopify Free codes
To support Buy Me a Coffee
Inscale
New Member
4 0 0

Not really done videos before, i leave that to Marketing dept., but please let me give some more detail which may help
So we have a product called a CP P4 and CP Y4 (just using these to sort issue) 
CP P4 has 3 pdf url's listed on page currently Data sheet, Manual and Technical Datasheet - Data Sheet is the model detail and shows available variants, manual is basically instructions, but technical data sheet is related to the variant, so each variant has its own technical data sheet (variant is what we call model on the pages)
Obviously i can list them all with product.metafield, but would need to rename each one for every product and the number of variants can be much larger than these examples, so ideally a different technical datasheet is added when a guest selects a different model on the product page
Yes i can list each variant as a single product and avoid this as i can list the individual datasheets on each page, but that then means a huge collection page and not that user friendly maybe
The links are listed via a custom liquid file shown in theme edit here 

Inscale_0-1717575173977.png

Metafields

Inscale_1-1717575418805.pngInscale_2-1717575446531.png

and the code so far 

{% comment %} Manuals and data sheets links to sites pulling from product metafeilds only shows if info is there Can show multiple items with the for loop. {% endcomment %}

{% for item in product.metafields.custom.data_sheets.value -%}
{%- if item != blank -%}
<div class= "pdf-child">
<a href={{ item }} target="_blank" rel="noopener noreferrer"><img src="https://cdn.shopify.com/s/files/1/0606/5150/3668/files/images.png?v=1661969450" alt="PDF Document" width="60"></a>
</br><strong>Data Sheet</strong>
</br></div>

{%- endif -%}
{%- endfor %}

{% for item in product.metafields.custom.manuals.value -%}
{%- if item != blank -%}
<div class= "pdf-child">
<a href={{ item }} target="_blank" rel="noopener noreferrer"><img src="https://cdn.shopify.com/s/files/1/0606/5150/3668/files/images.png?v=1661969450" alt="PDF Document" width="60"></a>
</br><strong>Manual</strong>
</br></div>

{%- endif -%}
{%- endfor %}

 

{% for item in product.metafields.custom.technical_datasheet.value -%}
{%- if item != blank -%}
<div class= "pdf-child">
<a href={{ item }} target="_blank" rel="noopener noreferrer"><img src="https://cdn.shopify.com/s/files/1/0606/5150/3668/files/images.png?v=1661969450" alt="PDF Document" width="60"></a>
</br><strong>Technical Datasheet</strong>
</br></div>

{%- endif -%}
{%- endfor %}

 

{% for variant in product.variants %}
{{ variant.metafields.custom.technical_datasheet.value }}
{%- if item != blank -%}
<div class= "pdf-child">
<a href={{ item }} target="_blank" rel="noopener noreferrer"><img src="https://cdn.shopify.com/s/files/1/0606/5150/3668/files/images.png?v=1661969450" alt="PDF Document" width="60"></a>
</br><strong>Model Data</strong>
</br></div>

{%- endif -%}
{%- endfor -%}

 

Sorry its a bit long winded and maybe confusing, but not sure how else to detail it