Whenever a URL results in a 404, I have a javascript that fires that redirects conditionally to a relevant URL.
This has worked great for some time.
But now I want to be able to redirect specific products that have been Drafted… to specific redirect URLs using metafields.
WHAT I HAVE DONE :
I have successfully allowed administrators to enter a URL in a metafield (redirect_url).
And I have been able to output that to the product page :
{{ product.metafields.custom.redirect_url.value }}
And I have been able to pass that to javascript :
const redirect_url = "{{ product.metafields.custom.redirect_url.value }}"; console.log(redirect_url); // output : the correct redirect URL
BUT… the metafields are NOT available when the product is Drafted and results in a 404…
BUT I have discovered you can access metafields even when not on that product page… like this :
{{ product['example-product-handle'].metafields.custom.redirect_url.value }}
but I need something dynamic…
{{handle}} is available and valid… and this would be golden :
const redirect_url = "{{ product['{{ handle }}'].metafields.custom.redirect_url.value }}";
but that breaks and the result is a mangled string 🙁
so i’m up against the wall with this and seeking help… I would really appreciate any solutions anyone has to offer!
To reiterate the goal :
I need to be able to access the metafields of a product even when it’s a 404. Normally, Shopify provides access to the metafileds easily, but NOT when it’s a 404.
So I want to be able to take the available data {{ handle }} and use it to access the metafields
I also want to avoid using Shopify Redirects because I want to make it easier and convenient for content administrators by being able to change the redirect URL themselves inline on the Edit Product screen.


