Formatting {{ shop.phone }} for use in notifications

Tom77
New Member
1 0 0

New to Shopify and liquid but an experienced programmer otherwise here.

Anyway, I want to put our shop's phone number in the header of emails that go out in the form of notifications but I see that I can only save it in Admin > General > Shop Address as 5556661212.  If I add dashes and hit save, they get taken out.

So when I use the shop.email variable that's how it shows when I want it to show as 555-666-1212.

Is there another variable I need to be looking at?  Does liquid have some kind of string functions that I can use to format it? Or do I just have to hard code the number if I want it formatted?

Thanks in advance.

0 Likes
Kevin76
Shopify Staff
Shopify Staff
54 0 5

Hey Tom,

It's not an elegant solution, but this should do the trick:

{% assign phone = shop.phone | split: '' %}{{ phone[0] }}{{ phone[1] }}{{ phone[2] }}-{{ phone[3] }}{{ phone[4] }}{{ phone[5] }}-{{ phone[6] }}{{ phone[7] }}{{ phone[8] }}{{ phone[9] }}

:)

Douglas_Rogers
New Member
7 0 0

There has to be a better way to do this than what i came up with, based on Kevin's suggestion.

I put this into a snippet included at the top of theme.liquid:

  {% assign ph_ary = shop.phone | split: '' %}
  {% assign phone = ph_ary[0] | append: ph_ary[1] | append: ph_ary[2] | append: '-' | append: ph_ary[3] | append: ph_ary[4] | append: ph_ary[5] | append: '-' | append: ph_ary[6] | append: ph_ary[7] | append: ph_ary[8] | append: ph_ary[9] %}

so i can use {{ phone }} in my templates. However, it's not global, for instance i had to include the snippet on collections.liquid as well.

  • Is there a way to make this more global so i only have to include this once?
  • Is there a better place to insert the include so that it works everywhere?
  • Is my liquid code correct here? Is append: the best way to do this?
0 Likes
IFAWeb
New Member
2 0 0

Much can be accomplished with only a few Liquid filters.

For example, we wanted to make phone numbers clickable within email notifications. Below is how shop.phone outputs the number for our shop.

(555) 555-5555

The filter method outputs directly to the page so there was no reason to assign a variable in our case.

 

{{ shop.phone | remove: "(" | remove: ")" | replace: " ", "-" }}

 

 

Our finished snippet looked like this:

 

<a href="tel:1-{{ shop.phone | remove: "(" | remove: ")" | replace: " ", "-" }}">{{shop.phone}}</a>

 

 

 

0 Likes