How to change telephone number format.

  • Posts: 8
  • Thank you received: 1
8 years 1 month ago #231927

-- HikaShop version -- : 2.6.1
-- Joomla version -- : 3.4.8
-- PHP version -- : 5.6.18

Hi,

I would like to know how to change the format of the telephone numbers. More specifically, I would like to add dashes, or at the very least add space between the numbers on the order invoice.

Right now the numbers on the invoice show up like this. 15555555555

I would like it to show up like this. 1-555-555-5555

or like this. 1 555 555 5555.

Is there anyway this can be done? I've already tried looking through the PHP address files, but I couldn’t find any place where I would be able to edit the format.

Thanks for your time,
Tim

Please Log in or Create an account to join the conversation.

  • Posts: 81540
  • Thank you received: 13069
  • MODERATOR
8 years 1 month ago #231938

Hi,

For that, you can replace the line:

<?php echo JText::sprintf('TELEPHONE_IN_ADDRESS','{address_telephone}');?>
by:
<?php

function localize_us_number($phone) {
  $numbers_only = preg_replace("/[^\d]/", "", $phone);
  return preg_replace("/^1?(\d{3})(\d{3})(\d{4})$/", "$1-$2-$3", $numbers_only);
}

$address = $this->params->get('address');
echo JText::sprintf('TELEPHONE_IN_ADDRESS',localize_us_number($address->address_telephone)); ?>
in the file "address_template" for both your frontend and your backend templates.

The following user(s) said Thank You: tonp

Please Log in or Create an account to join the conversation.

  • Posts: 127
  • Thank you received: 1
7 years 11 months ago #238831

Hello,

I have tried using this function in the address_template file, but it breaks the page. I have included the code I have in the file below. Could you let me know what is going wrong? Thanks.

<?php
/**
 * @package	HikaShop for Joomla!
 * @version	2.6.2
 * @author	hikashop.com
 * @copyright	(C) 2010-2016 HIKARI SOFTWARE. All rights reserved.
 * @license	GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?>
<span>{address_title} {address_firstname} {address_lastname}</span>
<span>{address_street}</span>
<span>{address_city}, {address_state} {address_post_code}</span>
<?php

function localize_us_number($phone) {
  $numbers_only = preg_replace("/[^\d]/", "", $phone);
  return preg_replace("/^1?(\d{3})(\d{3})(\d{4})$/", "$1-$2-$3", $numbers_only);
}

$address = $this->params->get('address'); ?>
<span><?php echo JText::sprintf('TELEPHONE_IN_ADDRESS',localize_us_number($address->address_telephone)); ?></span>

Mike

Please Log in or Create an account to join the conversation.

  • Posts: 81540
  • Thank you received: 13069
  • MODERATOR
7 years 11 months ago #238838

Hi,

What error do you get when you do that with the "error reporting" setting of the Joomla configuration activated ?

Please Log in or Create an account to join the conversation.

  • Posts: 127
  • Thank you received: 1
7 years 11 months ago #238867

Hello,

I have enabled maximum error reporting in Joomla, but no errors are displayed. The HTML is broken and the CSS is not even loaded. I have included a screenshot show the rendered page and included the outputted HTML from the developer tools in Safari below. The function seems to be working, as the dashes are being added to the phone number, but something is breaking the page. I have disabled certain template overrides and think I have narrowed it down to the address_template.php file with the extra function that is the culprit. With it disabled the page works with no issues. It is either the file itself or how the code interacts with other files.

<div id="hikashop_checkout_page" class="hikashop_checkout_page hikashop_checkout_page_step2">
			<form action="/book-store/checkout/task-step/step-3" method="post" name="hikashop_checkout_form" enctype="multipart/form-data" onsubmit="if('function' == typeof(hikashopSubmitForm)) { hikashopSubmitForm('hikashop_checkout_form'); return false; } else { return true; }">
		<div id="hikashop_checkout_address" class="hikashop_checkout_address">
	<div id="hikashop_checkout_address_left_part" class="hikashop_checkout_address_left_part">
		<fieldset class="hika_address_field" id="hikashop_checkout_billing_address">
			<legend>Billing Address</legend>
<div class="hikashop_checkout_address_choice_wrap selected">
			<label for="hikashop_checkout_billing_address_radio_1972" class="hikashop_checkout_address_choice"><input id="hikashop_checkout_billing_address_radio_1972" class="hikashop_checkout_billing_address_radio" type="radio" name="hikashop_address_billing" value="1972" checked="checked" /><span>Checkout with this address</span></label>
			<label for="hikashop_checkout_billing_address_radio_1972" style="cursor:pointer;">
			<div class="hikashop_checkout_billing_address_info">
<span> MT Help</span>
<span>4567 Some Place Street</span>
<span>Kerikeri, Alberta 3246</span>
<span>Telephone: 403-123-4567</span>			</div>
			</label>
			<div class="hikashop_checkout_billing_address_buttons">
				<a id="hikashop_checkout_billing_address_edit_1972" title="Edit" class="hikashop_checkout_billing_address_edit btn-blue btn-small modal" href="/book-store/checkout/address/edit/redirect-checkout/address_id-1972/step-2/type-billing/tmpl-component" onclick="return hikashopEditAddress(this,1,false);">
					Edit				</a>
					<a onclick="if(!confirm('Are you sure you want to delete this address ?')){return false;}else{return true;}" title="Remove" class="hikashop_checkout_billing_address_delete btn-red btn-small" href="/book-store/checkout/checkout/deleteaddress/step-2/redirect-checkout/address_id-1972/c98febf6a5aa9f629c9ce5ab3a428c1b-1">
						Remove</a>
			</div>
			</div>
<div class="hikashop_checkout_address_choice_wrap">
			<label for="hikashop_checkout_billing_address_radio_1974" class="hikashop_checkout_address_choice"><input id="hikashop_checkout_billing_address_radio_1974" class="hikashop_checkout_billing_address_radio" type="radio" name="hikashop_address_billing" value="1974"  onclick="this.form.submit(); return false;" /><span>Checkout with this address</span></label>
			<label for="hikashop_checkout_billing_address_radio_1974" style="cursor:pointer;">
			<div class="hikashop_checkout_billing_address_info">

Mike

Attachments:

Please Log in or Create an account to join the conversation.

  • Posts: 81540
  • Thank you received: 13069
  • MODERATOR
7 years 11 months ago #238921

Hi,

It's probably because there is a function and thus the system tries to load it several times and it crashes.
So just do it without a function.
Instead of:

<?php

function localize_us_number($phone) {
  $numbers_only = preg_replace("/[^\d]/", "", $phone);
  return preg_replace("/^1?(\d{3})(\d{3})(\d{4})$/", "$1-$2-$3", $numbers_only);
}

$address = $this->params->get('address');
echo JText::sprintf('TELEPHONE_IN_ADDRESS',localize_us_number($address->address_telephone)); ?>
do:
<?php
$address = $this->params->get('address');
echo JText::sprintf('TELEPHONE_IN_ADDRESS',preg_replace("/^1?(\d{3})(\d{3})(\d{4})$/", "$1-$2-$3", preg_replace("/[^\d]/", "", $address->address_telephone))); ?>

Last edit: 7 years 11 months ago by nicolas.
The following user(s) said Thank You: leitzdesign

Please Log in or Create an account to join the conversation.

  • Posts: 127
  • Thank you received: 1
7 years 11 months ago #238957

Hello,

Thanks, that worked perfectly.

Mike

Please Log in or Create an account to join the conversation.

Time to create page: 0.078 seconds
Powered by Kunena Forum