Shopify themes, liquid, logos, and UX
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.
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.
Share product url
Interesting question, I'd like to know more and check.
Which file type/format are you trying to reference in the metafields file object?
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 }}
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 }}
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"
Did you ever get a resolution to this?
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
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 }}
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.
Instead of
{{ product.metafields.my_fields.product_instructions.url }}
use
{{ product.metafields.my_fields.product_instructions.value.url }}
1 year later, I've encountered the exact issue you described.
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 }}
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:
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.
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!
Hey Community! As we jump into 2025, we want to give a big shout-out to all of you wh...
By JasonH Jan 7, 2025Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024