メタフェイズではshopifyを利用したストア構築を行っています。
その工程の中のオリジナルデザインのテーマ開発にはshopifyのテンプレート言語「Liquid」を使用します。
今回はLiquidでGETクエリの値を取得する処理、
具体的には「https://xxxx.myshopify.com/search?type=product」へアクセスした際、typeの値を取得する処理を紹介いたします。
下記コードをファイル名:snippets/function_get_query_value_by_key.liquidで保存します。
{%- capture contentForQuerystring -%}{{ content_for_header }}{%- endcapture -%}
{%- assign pageUrl = contentForQuerystring | split:'"pageurl":"' | last | split:'"' | first | replace:'\/','/' | replace:'%20',' ' | replace:'\u0026','&' -%}
{%- assign get_query_value_by_key = '' -%}
{%- if pageUrl contains "?" -%}
{%- assign pageQuerystring = pageUrl | split:'?' | last | replace:'=','&' -%}
{%- assign tmp = pageQuerystring | split:'&' -%}
{%- for kv in tmp -%}
{%- if kv == key -%}
{%- assign get_query_value_by_key = tmp[forloop.index] -%}
{%- break -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
Liquid上のGETクエリの値を取得したい所でsnippets/function_get_query_value_by_key.liquidをインクルードします。
{%- include "function_get_query_value_by_key" key:"type" -%}
上記コード以降は変数「get_query_value_by_key」に文字列「product」が格納されています。
shopifyを利用したストアの構築・運用のご相談はお問い合わせフォームより受け付けております。