Skip to content

Instantly share code, notes, and snippets.

@enamhasan
Created February 15, 2019 19:49
Show Gist options
  • Save enamhasan/b8d205856f29e3b6b23ea458d4f86d80 to your computer and use it in GitHub Desktop.
Save enamhasan/b8d205856f29e3b6b23ea458d4f86d80 to your computer and use it in GitHub Desktop.
Get the querystring values with liquid in shopify
{%- capture contentForQuerystring -%}{{ content_for_header }}{%- endcapture -%}
{% comment %} Use string splitting to pull the value from content_for_header and apply some string clean up {% endcomment %}
{%- assign pageUrl = contentForQuerystring | split:'"pageurl":"' | last | split:'"' | first | split:'.myshopify.com' | last |
replace:'\/','/' |
replace:'%20',' ' |
replace:'\u0026','&'
-%}
{%- assign pageQuerystring = pageUrl | split:'?' | last -%}
{%- if pageQuerystring contains "18460587851873" -%}
{% include 'cart-add-on' %}
{%- endif -%}
@Christopher-Hayes
Copy link

⚠️ Beware - contentForQuerystring gets cached by Shopify. If you were to revisit the same page with a slightly different URL param value, contentForQuerystring will give you the wrong (cached) param values.

@Christopher-Hayes
Copy link

Christopher-Hayes commented Jun 3, 2022

https://community.shopify.com/c/technical-q-a/variables-in-url-being-cached/td-p/1361289

I had to use a hack (described in that second forum post) when I wanted to send URL param options to the search results page. Basically it uses random numbers in the URL params to force a cache bust. However, it uses random numbers so it's not the most robust. This was just for a UI filter option, having it break once in a while wasn't a deal-breaker.

{% comment %}
  HACK -
  A caching issue with using "contentForQuerystring" was discovered to only occur on prod.
  Caching busting - use a random number to force a cache bust.
  https://community.shopify.com/c/technical-q-a/variables-in-url-being-cached/m-p/1361289/highlight/true#M69193
{% endcomment %}
{% assign random_number = "now" | date: "%N" | modulo: 10000 | plus: min %}
<a href="/search?q={{ search.terms | url_encode }}&showType=article&view={{ random_number }}">Go to search results with URL option</a>

In my case, showType=article was the URL option that was breaking due to Shopify caching. view={{ random_number }} was the cache-busting URL option.

@eliasolj
Copy link

This is an old conversation, but i have a question @Christopher-Hayes !
The view parameter ofcourse breaks the layout, because it is looking for a non-existing view on a template? How did you deal with this problem?

@eliasolj
Copy link

This is an old conversation, but i have a question @Christopher-Hayes ! The view parameter ofcourse breaks the layout, because it is looking for a non-existing view on a template? How did you deal with this problem?

Actually, I fixed this now. I just used a parameter "type" instead of "view"!

@Christopher-Hayes
Copy link

👍 Good to know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment