Order Printer phone number format

Gordon_Purcey
Shopify Partner
4 0 1

Just in case any of you are working to format phone numbers on the order printer templates, I found some liquid code that works quite well from another user on the forums (Sorry I couldn't find the post to give credit to the original poster).

We wanted to be able to print the phone number as XXX-XXX-XXXX . Problem is some customers include the dashes when they enter their phone numbers, and some don't. So the code I found worked fine if the customer had not entered dashes themselves. But if they had, the phone number print became unreadable.

So here's the code with my modification. It's quite easy actually.  I simply remove all of the "-"s before formatting the number. So it handles either case just fine .

The liquid code is:

Billing Phone Number: {% assign phone = billing_address.phone | remove: "-" | split: '' %}{{ phone[0] }}{{ phone[1] }}{{ phone[2] }}-{{ phone[3] }}{{ phone[4] }}{{ phone[5] }}-{{ phone[6] }}{{ phone[7] }}{{ phone[8] }}{{ phone[9] }}

 This would be easily adaptible for other countries by checking the country information and creating a slightly different version for other countries if you need to do that.

Hope this helps someone else.

Gordon

0 Likes
Bob_Starr1
Shopify Staff (Retired)
Shopify Staff (Retired)
26 0 7

Hi Gordon,

Thanks for posting this for everyone to see!  It's great to have people contribute information like this which is why these forums are so great.  Very much appreciated : )

Thanks!

Bose
New Member
1 0 0

This helped me alot in short time

0 Likes
EimanS
Tourist
3 0 3

It's 2021 and I was looking for this code, Thank you

0 Likes