Snippet: make a phone number clickable (create phone number link)

AuralFetish
Excursionist
26 0 3
{%- comment  -%}
  usage:
  {% render 'fn.clickifyPhone', phone: 'string', countrycode: 'integer', anchortxt: 'optionalstring', class: 'optionalstring' %}
{%- endcomment -%}
{%- assign digits = phone | remove: '(' | remove: ')' | remove: '+' | remove: '-' | remove: '.' | remove: ' ' | strip  -%}
{%- assign linky = digits | prepend: countrycode | prepend: 'tel:+' -%}
{%- assign test = digits | times: 1 -%}
{%- if test != 0 -%}
<a href="{{ linky }}" {%- if class != nil and class != '' -%}class="{{ class }}"{%- endif -%}>{%- if anchortxt != nil and anchortxt != ''-%}{{ anchortxt }}{%- else -%}{{ phone }}{%- endif -%}</a>
{%- else -%}
{{ phone }}<!-- entry included invalid characters, so was not converted to a telephone link. -->
{%- endif -%}

 

Save as snippets/fn.clickifyPhone.liquid

New(ish) to shopify, so if anyone sees an improvement, please share.

0 Likes