How to navigate to a URL with Liquid Code?

What is the line of code used to navigate to a URL based on some condition, using Liquid Code (at server side) i.e WITHOUT using Javascript?

(Here, I am not asking about client-side re-direction)

Something example like, if the product is out of stock out navigate to another page …

Thanks

John

1 Like

Hi @Saji

Aibek is here from Speedimize.io

If we are not mistaken, then liquid does not have the ability to redirect.
Use as below:

{% if product.available %}

{% endif %}

Hope that helps you.

2 Likes

Thanks Aibek.

But this was not the solution I am looking for. If you know .Net, I am looking for something similar to Server.Transfer functionality.

Let me reiterate another example:

If the product is out of stock, navigate to the URL, the URL is saved in a shopify metafield.

Now, here I am not sure how we can pass a variable/metafield to a javascript for redirection.

Thanks

John

1 Like

Hey @Saji

Thanks for getting back to me and clarifying with an example, now it’s much better.

If URL is stored in product meta-fields, you can use this piece of code. Just put the correct meta field name.

{% if product.available %}

{% endif %}

Otherwise, if a liquid file stored at your server, to make a redirect from the server you need to make a Shopify API request to get product data, and then you can redirect via .net

Maybe that helps you.

Hey @Anonymous


Does not work.

I have tried like this…

{% if product.available != true %}
{%assign redirecturl = product.collections[0].metafields.breadcrumbs.Current-url%}

{% endif %}

It looks like an infinite loop, loads…reloads…reloads…

Please enlighten me if I am wrong.

Can we call Liquid code {{ .. }} from inside a ?

Thanks

John

Hi @Saji

First of all, yes, liquid code is supported inside the tag.

Current-url-not written this way. Try starting with a small letter and using a lower space. It should work like this:

{% assign redirecturl = product.collections[0].metafields.breadcrumbs.current_url %}

And to check, try printing this variable in any of the tags. For example, h1.

{{ redirecturl }}

See what’s printed out there. If your site is live, it is better to use Then your visitors won’t notice the text.

Try it and let us know.

1 Like