Skip to content

Instantly share code, notes, and snippets.

@stewartknapman
Last active March 3, 2017 03:14
Show Gist options
  • Save stewartknapman/09328b98512d946352e8 to your computer and use it in GitHub Desktop.
Save stewartknapman/09328b98512d946352e8 to your computer and use it in GitHub Desktop.
{% capture function %}
{% comment note:
Shopify, crazily enough, does not have a global object that returns the current url like it does for the page_title.
So lets fake one.
It will take into account filtered collections.
It will not take into account url params and hash anchors, because reasons.
page_url_with_shop_domain gives you the page_url with the shop.url prepended.
%}{% endcomment %}
{% if template contains '404' %}
{% assign page_url = '/404' %}
{% elsif template contains 'article' %}
{% assign page_url = article.url %}
{% elsif template contains 'blog' %}
{% assign page_url = blog.url %}
{% elsif template contains 'cart' %}
{% assign page_url = '/cart' %}
{% elsif template contains 'list-collections' %}
{% assign page_url = '/collections' %}
{% elsif template contains 'collection' %}
{% if collection.url != blank %}
{% assign page_url = collection.url %}
{% else %}
{% assign page_url = '/collections/all' %}
{% endif %}
{% if current_tags.size > 0 %}
{% capture current_tag_params %}{% endcapture %}
{% for current_tag in current_tags %}
{% capture current_tag_params %}{{ current_tag_params }}{% unless forloop.first %}+{% endunless %}{{ current_tag | handle }}{% endcapture %}
{% endfor %}
{% assign page_url = page_url | append: '/' | append: current_tag_params %}
{% endif %}
{% elsif template contains 'index' %}
{% assign page_url = '/' %}
{% elsif template contains 'page' %}
{% assign page_url = page.url %}
{% elsif template contains 'product' %}
{% assign page_url = product.url %}
{% elsif template contains 'search' %}
{% assign page_url = '/search' %}
{% elsif template contains 'customers' %}
{% if template contains 'activate_account' %}
{% assign page_url = '/account/activate' %}
{% elsif template contains 'account' %}
{% assign page_url = '/account' %}
{% elsif template contains 'addresses' %}
{% assign page_url = '/account/addresses' %}
{% elsif template contains 'login' %}
{% assign page_url = '/account/login' %}
{% elsif template contains 'order' %}
{% if order != blank %}
{% assign page_url = order.customer_url | remove: shop.url %}
{% else %}
{% assign page_url = '/account/orders' %}
{% endif %}
{% elsif template contains 'register' %}
{% assign page_url = '/account/register' %}
{% elsif template contains 'reset' %}
{% assign page_url = '/account/reset' %}
{% else %}
{% assign page_url = null %}
{% endif %}
{% else %}
{% assign page_url = null %}
{% endif %}
{% if page_url != null %}
{% assign page_url_with_shop_domain = page_url | prepend: shop.url %}
{% endif %}
{% endcapture %}
@stewartknapman
Copy link
Author

UPDATE: Just use {{ request.path }} or to include the shops domain: {{ request.host }}{{ request.path }}

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