A space to discuss online store customization, theme development, and Liquid templating.
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 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.
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.
Thank you very much
It'll take some backwards and ugly liquid code, but saying simply "no" is a ridiculous and false statement.
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>