Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
Copy link
Author

stvnrynlds commented Apr 15, 2019

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

@hama
Copy link

hama commented Jan 16, 2020

Great job, thanks!

@stvnrynlds
Copy link
Author

stvnrynlds commented Jul 31, 2020

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

colindjk commented Aug 23, 2020

This is so helpful, thanks!

@andres9722
Copy link

andres9722 commented Aug 24, 2020

Thanks!

@arthurcruzd
Copy link

arthurcruzd commented May 17, 2021

This is awesome!
Thank you very much

@stvnrynlds
Copy link
Author

stvnrynlds commented May 17, 2021

@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
Copy link

phucnvfabbi commented Jun 17, 2021

@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