Testing the existence of a file

Highlighted
New Member
5 0 0

Hi, I've got a customer with products that require Safety Data Sheets which are .pdf files uploaded to the usual Shopify Files area.  I've created some code to dynamically determine the correct file name using the part number (e.g., "12-345_SDS.pdf") and then I use that to create a button which will open the file in a new tab.

This works great if the file already exists, however, if it doesn't, it produces a Shopify 404 page with a link that goes back to the Shopify home page (yikes!).

My search on these forums for this topic only finds a post from 2009.  I would hope that this basic functionality has been added since then.

Does anyone know how to check if a file exists?

Or, if that's still not possible, is there a way to use the custom 404 page for the site (which links back to the store) instead of the generic Shopify 404 page that takes customers away from the site?

Thank you!

Craig B.

0 Likes
Highlighted
Excursionist
13 3 0

Hey Craig,

It would be great to find an ideal solution for situations like this. Here is a thread from a few months ago regarding this topic.

https://community.shopify.com/c/Shopify-APIs-SDKs/Best-practice-for-associating-pdf-with-product/m-p...

 

Like you mentioned it's trivial to add links using file_url or asset_url if you have consistent naming associated with a product / asset. But what if the asset does not exist...

 

Another option is using Product Metafields. But then you have to manage / update metafields for every asset which is not necessarily ideal.

 

Using javascript to check if a file exists - again not ideal but it's one example of checking if something exists:

https://community.shopify.com/c/Shopify-Design/Liquid-check-if-a-value-exists/m-p/149816/highlight/t...

 

Ideally some way to check if the file exists or not with liquid or some sort of files api would be nice.

 

Custom 404 templates are possible. Here is a basic example:

https://shopify.dev/docs/themes/files/404-liquid

0 Likes
Highlighted
New Member
5 0 0

Thanks for the reply; I'll check out those links now.

Just so you know, as I mentioned, I already have a custom 404 page for the store, however, this error goes to the 404 page for Shopify not the store's 404 page!  That's a problem since the included link only goes to shopify.com which, obviously, has nothing to do with my client's store.

0 Likes
Highlighted
New Member
5 0 0

Ok...  Until Shopify has an easy way to check for the existence of a file, here's the work-around I came up with.

  {% for tag in product.tags %}
    {% if tag == 'sds' %}
      {% assign sds_pdf = 'https://cdn.shopify.com/s/files/1/2345/6789/0123/files/' | append: product.handle | append: '_SDS.pdf' %}
      <p><a href={{ sds_pdf }} target="_blank" class="btn btn--wide btn--secondary" rel="noopener noreferrer">Safety Data Sheet</a></p>
    {% endif %}
  {% endfor %}

 

The above makes a button for the Minimal theme that looks like this:

ButtonExample1.jpg

When clicked, the .pdf file is opens in a new tab.

The only drawback is that now the users have to remember to add a tag to the product after they upload the file.  Not the best solution (dynamically checking for the file), but it will have to do for now!

 

 

0 Likes