Can we retrieve a metafields file name?

LloydSpencer
Shopify Partner
19 0 0

Hi @Niki_K ,

 

Is it possible to return a metafields file name?

 

Screenshot 2022-09-21 at 15.11.29.png

Reply 1 (1)

Niki_K
Shopify Partner
447 45 124

Hey @LloydSpencer,

 

sure, not that easy, but still you can get a way around it. 😄 

 

Limitations: My solution works for files, which are stored in pdf. You might need to edit the code if you use other file types. 

 

I saved my file as Color Guide. (Shopify adds underscore instead of space, so I needed to replace the underscore with a space back). Here's my solution:

 

<ul>
  {% for file in product.metafields.custom.file.value %}
    {% assign file_name_initial = file.url | split: '/' | last %} <!-- you get the file name with the file format and additional information -->
    {% assign file_name_long = file_name_initial | split: '.pdf' | first %} <!-- you get rid of the additional information -->
    {% assign file_name = file_name_long | replace: "_", " " %} <!-- you replace underscore with a space -->
    <li>
      <a href="{{ file.url }}" target="_blank">{{ file_name }}</a>
    </li>
  {%- endfor -%}
</ul>

 

And here's how it will be rendered:  

Snímek obrazovky 2022-09-21 v 16.52.07.png

Is it what you're looking for? 

 

Let me know if you need further help! 🙂

Send me a link to your store to niki@ecommercepot.com and I'll help you out.
For more info what I do, visit my agency website ecommercepot.com or my personal website nikikozak.me.