Product Metafield with file type not returning url

50451
Tourist
4 0 5

I'm attempting to access a product metafield with a file content type. However I'm running into issues.

The metafield is set up as below.

50451_0-1626619238385.png

 

As far as I am aware the metafield with this setup should return a generic_file object that should allow access to id, media_type, preview_image, and url.

When trying to access on a product page using the liquid code  {{ product.metafields.my_fields.product_instructions.url }} I get nothing returned. 

However when using {{ product.metafields.my_fields.product_instructions }} on a product page where the product has a file metafeild set the follwing is returned gid://shopify/GenericFile/22363445657783

When no metafield is set as expected nothing is returned.

 

 

Replies 13 (13)

Anthony_David_
Shopify Partner
417 46 105

Share product url

Theme customization | Design | Shopify App Consultant
Have Any Questions? Whatsapp +1 (341) 241-4263
Based in United States

drakedev
Shopify Partner
685 148 230

Interesting question, I'd like to know more and check.

Which file type/format are you trying to reference in the metafields file object?

If my answer was helpful click Like to say thanks
If the problem is solved remember to click Accept Solution
Shopify/Shopify Plus custom development: You can hire me for simple and/or complex tasks.
50451
Tourist
4 0 5

It's a pdf that's being referenced.

I was expecting to be returned a generic file object as per the metafield object docs. From which I could access id/media_type/preview_image/url as per  the generic_file object docs.

Interestingly it instead appears to return a type of Metafields::MetafieldDrop which I can't find any reference to anywhere. This can be seen if you try to use something like asset_url where you get the error "Liquid error (sections/test.liquid line 1): Expected input to be a String but got Metafields::MetafieldDrop"

 I've used the below code instead, utilising the file_reference metafield filter which works for my uses, but it is not what I had expected from the docs.

{{ product.metafields.my_fields.product_instructions | file_reference }}

 

drakedev
Shopify Partner
685 148 230

It is indeed something strange and the documentation need to be improved. I submitted a report on that documentation page.

In the meanwhile the only way that I was able to get the file URL path was using this

File URL: {{ product.metafields.specs.pdf_file | file_url }}

 

If my answer was helpful click Like to say thanks
If the problem is solved remember to click Accept Solution
Shopify/Shopify Plus custom development: You can hire me for simple and/or complex tasks.
tekhaus
Shopify Partner
8 0 3

And none of these methods appear to work within email notifications. @Shopify, what is the correct way to generate a PDF file_reference url within an order notification template?

 

{{ line.product.metafields.my_fields.manual }} => "gid://shopify/GenericFile/22826941284522"

{{ line.product.metafields.my_fields.manual | file_url }} => "https://cdn.shopify.com/s/files/1/0534/2560/2730/files/gid://shopify/GenericFile/22826941284522?113"

{{ line.product.metafields.my_fields.manual.url }} => null

{{ line.product.metafields.my_fields.manual.url | file_url }} => "https://cdn.shopify.com/s/files/1/0534/2560/2730/files/?113"

 

Verdant-Spark
Shopify Partner
99 8 32

Did  you ever get a resolution to this?

mkyigitoglu
Shopify Partner
2 0 0

I know that is not a best solution but it works for me.

{% assign getFile = line.product.metafields.my_fields.manual %}
{{  getFile | file_url | remove: getFile }}

=>//cdn.shopify.com/s/files/1/xxx/xxxx/xxxx/files/xxx.pdf?v=xxxxxxxxxxxx

@tekhaus 





brendamarienyc
Shopify Partner
6 1 5

I am having this problem as well, and have tried everything suggested in this thread. Sometimes the below liquid works as expected and it creates a url link to the file (a pdf in my case), but most of the time I get a liquid error that reads "Liquid error (sections/main-product.liquid line 310): internal".

{{ product.metafields.my_fields.tear_sheet | file_url }}

 

Danh11
Shopify Partner
82 2 32

Also getting this problem. Appending '.url' on the end of the metafield path doesn't work as expected. Putting it through the 'file_url' filter does work.

silus
Shopify Partner
2 0 4

 Instead of
{{ product.metafields.my_fields.product_instructions.url }}

use

 {{ product.metafields.my_fields.product_instructions.value.url }}

easyt
Visitor
1 0 1

1 year later, I've encountered the exact issue you described.

ChunkySteveo
Shopify Partner
17 0 4

For anyone else having this issue still - to get the file URL, you need to add .url to the .value:

 

 

{{ product.metafields.my_meta_namespace.field_key_name.value.url }}

 

made4Uo
Shopify Partner
3805 713 1127

Hi future readers, 

 

If you console.log the code below, you will have the result (see image below)

<script>
console.log({{ product.metafields.my_fields.product_instructions | json }})
</script>

 

Result of console.log with a PDF file: 

made4Uo_0-1665598372697.png

Calling the metafield differs on what type of files are included in the metafield. 

Notice on the image below, the first file, which is 0, is a jpg file. Second is a pdf, and third is an mp3. Both pdf and mp3 have a dropdowns. 

made4Uo_2-1665599022212.png

For the pdf and audio file, the code below should work to get the url

{{ product.metafields.my_fields.product_instructions.value.url }}

 

For the image, you can use the code below to get the url. 

{{ product.metafields.my_fields.product_instructions.value }}

 

NOTE: This differ greatly when you have a list of file metafield, like the sample above. The console log result is an array and we need to loop the values, to call each file. 

{% assign metaFiles = product.metafields.my_fields.product_instructions.value %}
{% for file in metaFiles %}
{{ file.url }}  
{{ file }}
{% endfor %}

 

I recommend, to use console.log to have an idea what you are working for. Happy coding!

 

Volunteering to assist you!  Likes and Accept as Solution  is highly appreciated.✌
Coffee fuels my dedication. If helpful, a small Coffee Tip would be greatly appreciated.
Need EXPERIENCED Shopify developer without breaking the bank?
Hire us at Made4Uo.com for quick replies.
Stay in control and maintain your security by avoiding unnecessary store access!