Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

How to get url query params via liquid code

How to get url query params via liquid code

deepen_lau
Shopify Partner
4 0 2

How to get url query params via liquid code when developing theme

for example, i have a url : https://my-store.myshopify.com/collections/all?foo=bar

 

How to get the foo query param using liquid

 

thanks a lot.

Replies 4 (4)

ZenoPageBuilder
Shopify Partner
1052 203 229

Hello @deepen_lau 

It is not possible to get query params with Liquid code.

You will need to process it on client side with Js script code.

Zeno Page Builder - Build responsive & SEO-optimized Landing pages, Blog posts, Product pages and more...
Learn more at zenobuilder.com
deepen_lau
Shopify Partner
4 0 2

Thank you very much

joshcorbett
Shopify Partner
29 1 10

It'll take some backwards and ugly liquid code, but saying simply "no" is a ridiculous and false statement.

you can't just say perchance

ZephyrSoft
Shopify Partner
9 0 4

Primero, debe crear el siguiente snippet:
snippets/get-url-query.liquid

 

{%- capture content_for_query_string -%}{{ content_for_header }}{%- endcapture -%}
{%- liquid
  assign query_key = query_key | default: blank
  assign query_value = blank
  assign page_url = content_for_query_string | split: '"pageurl":"' | last | split: '"' | first | split: request.host | last | replace: '\/', '/' | replace: '%20', ' ' | replace: '\u0026', '&'

  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] == query_key
          assign query_value = key_and_value[1]
        endif
      endif
    endfor
  endfor

  if query_value != blank
    echo query_value | strip | strip_newlines
  endif
-%}

 

Luego, desde su sección o snippet puede consultar parámetros de la siguiente forma: 

 

{%- assign query_key= 'foo' -%}

{%- capture query_value -%}
  {%- render 'get-url-query', query_key: query_key' -%}
{%- endcapture -%}

{%- assign query_value = query_value | strip_newlines | strip -%}

<h1>foo = {{ query_value }}</h1>