Get the Shopify URL parameters in liquid code

Here’s one that I’ve found is still very much a secret among many developers. As of writing this, Shopify does not give you a clear path to grab URL parameters within the liquid code. You can always do a JS workaround but that means waiting for the DOM to load your script before making any changes to the frontend based on the URL parameters. I’ve found some mention about Shopify’s request object but have not had much luck with that my self.

NOTE: I’ve seen some mention of Shopify limiting access to this method as of March 2022 but have not found that to be the case in my own sites, so use at your own risk!

So the basic idea is that you pull in content_for_header and then split up the return based on the url of the site.

{%- assign page_url = content_for_header | split:'"pageurl":"' | last | split:'"' | first | split: request.host | last | replace:'\/','/' | replace:'%20',' ' | replace:'\u0026','&'  -%}
{% assign param = blank %}

{%- for i in (1..1) -%}

  {%- unless page_url contains "?" -%}{% break %}{%- endunless -%}
  {%- assign query_string = page_url | split:'?' | last -%}
  {%- assign qry_parts= query_string | split:'&' -%}

  {%- for part in qry_parts -%}
    {%- assign key_and_value = part | split:'=' -%}
    {%- if key_and_value.size > 1 -%}
      {% if key_and_value[0] == 'param' %}
        {% assign param = key_and_value[1] %}
      {% endif %}
    {%- endif -%}
  {%- endfor -%}
{%- endfor -%}

NOTE: This is NOT our code however, the original source is unknown.