Skip to content

Instantly share code, notes, and snippets.

@stvnrynlds
Last active January 19, 2022 14:46
Show Gist options
  • Select an option

  • Save stvnrynlds/21461c1d6a4b8124d53428cf2b5e5157 to your computer and use it in GitHub Desktop.

Select an option

Save stvnrynlds/21461c1d6a4b8124d53428cf2b5e5157 to your computer and use it in GitHub Desktop.
Shopify LinkList API Workaround
{%- layout none -%}
{%- assign listParams = request.path | replace: page.url, "" | remove: "/" | split: '+' -%}
{% if listParams != empty %}
{
{% for param in listParams -%}
{%- assign getList = linklists[param] -%}
{%- if getList != empty %}
"{{ param }}": [{% for level1 in getList.links %}{
"id": "{{ level1.title | handle }}-{{ forloop.index0 }}",
"title": "{{ level1.title }}",
"url": "{% if level1.type != "http_link" %}{{ shop.url }}{% endif %}{{ level1.url }}",
"children": [
{%- for level2 in level1.links -%}
{
"id": "{{ level2.title | handle }}-{{ forloop.index0 }}",
"title": "{{ level2.title }}",
"url": "{% if level2.type != "http_link" %}{{ shop.url }}{% endif %}{{ level2.url }}",
"children": [
{%- for level3 in level2.links -%}
{
"id": "{{ level2.title | handle }}-{{ forloop.index }}",
"title": "{{ level3.title }}",
"url": "{% if level3.type != "http_link" %}{{ shop.url }}{% endif %}{{ level3.url }}"
}
{%- if forloop.last == false %},{% endif -%}
{%- endfor -%}
]
}
{%- if forloop.last == false %},{% endif -%}
{%- endfor -%}
]
}
{%- if forloop.last == false %},{% endif -%}
{% endfor %}
]{%- if forloop.last == false %},{% endif -%}
{% else %}
"{{ param }}": [
{
"status": 404,
"code": "not-found",
"title": "Menu Not Found",
"detail": "Menu `{{ param }}` was not found"
}
]{%- if forloop.last == false %},{% endif -%}
{%- endif -%}
{%- endfor %}
}
{% else %}
{% assign menuHandles = linklists | map: 'handle' %}
["{{ menuHandles | join: '","'}}"]
{% endif %}
@stvnrynlds

stvnrynlds commented Apr 15, 2019

Copy link
Copy Markdown
Author

Added hash for quickly getting index by handle (e.g., menus[menusHash['main-menu']]).

@hama

hama commented Jan 16, 2020

Copy link
Copy Markdown

Great job, thanks!

@stvnrynlds

stvnrynlds commented Jul 31, 2020

Copy link
Copy Markdown
Author

Now this page acts more like a real API. Once you create a page with this layout (e.g., example.com/pages/menus):

  • Hitting it directly will give you an array of available handles: example.com/pages/menus
  • Supplying a handle name will return that linklist: example.com/pages/menus/handle
  • Supplying multiple handles separated by '+' will give you an array of linklists requested: example.com/pages/menus/handle1+handle2

@colindjk

Copy link
Copy Markdown

This is so helpful, thanks!

@andres9722

Copy link
Copy Markdown

Thanks!

@arthurcruzd

Copy link
Copy Markdown

This is awesome!
Thank you very much

@stvnrynlds

stvnrynlds commented May 17, 2021

Copy link
Copy Markdown
Author

@arthurcruzd Glad it’s helpful! I’m not keeping this up to date with any Shopify Liquid changes so you may need to test it out for yourself. Let me know how you use it!

@phucnvfabbi

phucnvfabbi commented Jun 17, 2021

Copy link
Copy Markdown

@arthurcruzd Glad it’s helpful! I’m not keeping this up to date with any Shopify Liquid changes so you may need to test it out for yourself. Let me know how you use it!

@stvnrynlds
This is so helpful, thanks!
But I cant accest url of shop look like restAPI, please tell me how. Thanks!!!
https://{user}:{pass}@{mystore}.myshopify.com/pages/menu-api/category-menu -> this is go to page write password of store

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