Page 1 of 1

Remove any spaces from telephone number in customer data module

Posted: Thu Dec 28, 2023 12:52 pm
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.

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

Posted: Thu Dec 28, 2023 1:09 pm
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.

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

Posted: Thu Dec 28, 2023 2:03 pm
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:

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

Posted: Fri Dec 29, 2023 9:40 am
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.

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

Posted: Fri Dec 29, 2023 11:30 am
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)

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

Posted: Sun Dec 31, 2023 4:52 pm
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.

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

Posted: Sun Dec 31, 2023 5:38 pm
by burt
str_starts_with is PHP8
Assume therefore, you're on older.

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

Posted: Sun Dec 31, 2023 6:04 pm
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.

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

Posted: Sun Dec 31, 2023 11:41 pm
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.

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

Posted: Mon Jan 01, 2024 10:44 am
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;
    }
  }

}