Remove any spaces from telephone number in customer data module

Open to all! Ask other shopowners for help.
Post Reply
14Steve14
Senior Contributor
Posts: 923
Joined: Fri Oct 25, 2019 7:01 pm
Phoenix Version: v1.0.9.1
Has thanked: 17 times
Been thanked: 103 times

Remove any spaces from telephone number in customer data module

Post by 14Steve14 »

One of the things that we do after a customer creates an account is to remove any blank spaces that may be added to the telephone number. Its easier then to find a customer from their phone number rather than having to guess all the options with included spaces.

Is there away in the module, or will something need adding to the sloppy words cleaner addon. If its easier this way I will ask in the addon area.


Join The Code Co-op to get access to your library in the Code Co-op Forum
User avatar
burt
Core Team
Posts: 4559
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 412 times

Re: Remove any spaces from telephone number in customer data module

Post by burt »

I haven't tested, but it feels like this should work;

New file:
/includes/hooks/shop/create_account/formatPhone

Code: Select all

class hook_shop_create_account_formatPhone {
  
  function listen_injectFormVerify() {
    global $customer_details;
    
    if (!empty($customer_details['telephone'])) {
      return $customer_details['telephone'] = str_replace(' ', '', $customer_details['telephone']);
    }
  }

}
This should take any spaces out of the customers telephone at the point of account creation.
heatherbell
Senior Contributor
Posts: 2540
Joined: Mon Oct 07, 2019 4:39 am
Phoenix Version:
Has thanked: 35 times
Been thanked: 243 times

Re: Remove any spaces from telephone number in customer data module

Post by heatherbell »

And those customers who seem to autofill their number prefixed with +44 instead of 0 and those that don't include the 0 at all.
This removes spaces first and then changes +44 (if at beginning of number) to 0 and adds 0 (if it doesn't exist):

Code: Select all

    // remove spaces from number and replace '+44' with '0' if needed
    if (!empty($customer_details['telephone'])) {
        $customer_details['telephone'] = preg_replace('/^\+44/', '0', str_replace(' ', '', $customer_details['telephone']));
    }
    // Check if the number doesn't start with '0' and add '0' if needed
    if ($customer_details['telephone'][0] !== '0') {
        $customer_details['telephone'] = '0' . $customer_details['telephone'];
    }
    return $customer_details['telephone'];
Tested for us in U.K. but not tested to destruction yet but customers might eventually find a way to do that :roll:
14Steve14
Senior Contributor
Posts: 923
Joined: Fri Oct 25, 2019 7:01 pm
Phoenix Version: v1.0.9.1
Has thanked: 17 times
Been thanked: 103 times

Re: Remove any spaces from telephone number in customer data module

Post by 14Steve14 »

Thanks @burt and @heatherbell .

Never thought of changing the +44 as well. I will create the hook and give it a try. I will report back here with my findings and testing.
User avatar
burt
Core Team
Posts: 4559
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 412 times

Re: Remove any spaces from telephone number in customer data module

Post by burt »

Good point on the Intl dialling code. Taking parts of mine and parts of heatherbell's, this should do it (but I haven't tested);

Code: Select all

class hook_shop_create_account_formatPhone {
  
  function listen_injectFormVerify() {
    global $customer_details;
    
    if (!empty($customer_details['telephone'])) {
      $phone = strtr($customer_details['telephone'], ['+44' => '', ' ' => '']);
      if (!str_starts_with('0', $phone)) $phone = "0{$phone}";
      
      return $customer_details['telephone'] = $phone;
    }
  }

}
This piece:

Code: Select all

$phone = strtr($customer_details['telephone'], ['+44' => '', ' ' => '']);
removes +44 and empty spaces. You could add more things in here if you need to remove more, eg to remove dashes as well: ['+44' => '', ' ' => '', '-' => '']

This piece:

Code: Select all

if (!str_starts_with('0', $phone)) $phone = "0{$phone}";
checks if the resultant string [after manipulation of removing spaces etc] starts with 0 and if it does not, it adds a 0.

Potential problem: are there any UK numbers with no zero at the start?
Other than the usual which no customer would give anyway (eg 999, 151 etc)
14Steve14
Senior Contributor
Posts: 923
Joined: Fri Oct 25, 2019 7:01 pm
Phoenix Version: v1.0.9.1
Has thanked: 17 times
Been thanked: 103 times

Re: Remove any spaces from telephone number in customer data module

Post by 14Steve14 »

Just found time to try this and am having a problem. I am getting the following error in the error logs file
Stack trace:
#0 /home/railways/public_html/pclatest/includes/system/versioned/1.0.8.1/hooks.php(150): hook_shop_create_account_formatPhone->listen_injectFormVerify(Array)
#1 /home/railways/public_html/pclatest/create_account.php(25): hooks->cat('injectFormVerif...')
#2 {main}
thrown in /home/railways/public_html/pclatest/includes/hooks/shop/create_account/formatPhone.php on line 20
Line 20 is

if (!str_starts_with('0', $phone)) $phone = "0{$phone}";


When I replace the code with the following it works fine so I may leave it at that.

Code: Select all

class hook_shop_create_account_formatPhone {
  
  function listen_injectFormVerify() {
    global $customer_details;
    
    // remove spaces from number and replace '+44' with '0' if needed
    if (!empty($customer_details['telephone'])) {
        $customer_details['telephone'] = preg_replace('/^\+44/', '0', str_replace(' ', '', $customer_details['telephone']));
    }
    // Check if the number doesn't start with '0' and add '0' if needed
    if ($customer_details['telephone'][0] !== '0') {
        $customer_details['telephone'] = '0' . $customer_details['telephone'];
    }
    return $customer_details['telephone'];
    }
  }
Thanks for your help.
User avatar
burt
Core Team
Posts: 4559
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 412 times

Re: Remove any spaces from telephone number in customer data module

Post by burt »

str_starts_with is PHP8
Assume therefore, you're on older.
I am not here to build for you.
I am here to build with you. Let's help each other.
14Steve14
Senior Contributor
Posts: 923
Joined: Fri Oct 25, 2019 7:01 pm
Phoenix Version: v1.0.9.1
Has thanked: 17 times
Been thanked: 103 times

Re: Remove any spaces from telephone number in customer data module

Post by 14Steve14 »

burt wrote: Sun Dec 31, 2023 5:38 pm str_starts_with is PHP8
Assume therefore, you're on older.
To be honest I thought I have the test site set to a php8 version in the htaccess file as cannot change the whole server as the existing site will not run on it. I will have to go and have another look at this.
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Remove any spaces from telephone number in customer data module

Post by ecartz »

Text::is_prefixed_by does pretty much the same thing as str_starts_with but works in PHP 7 and 8 both. Arguments may be in the reverse order.
User avatar
burt
Core Team
Posts: 4559
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 412 times

Re: Remove any spaces from telephone number in customer data module

Post by burt »

Try;

Code: Select all

class hook_shop_create_account_formatPhone {
  
  function listen_injectFormVerify() {
    global $customer_details;
    
    if (!empty($customer_details['telephone'])) {
      $phone = strtr($customer_details['telephone'], ['+44' => '', ' ' => '']);
      // php 8 (native PHP function)
      // if (!str_starts_with('0', $phone)) $phone = "0{$phone}";
      // php 7/8 (inbuilt Phoenix function)
      if (!Text::is_prefixed_by($phone, '0')) $phone = "0{$phone}";
      
      return $customer_details['telephone'] = $phone;
    }
  }

}
I am not here to build for you.
I am here to build with you. Let's help each other.


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply