Skip to content

Instantly share code, notes, and snippets.

@mcnabsystems
Created July 24, 2019 08:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcnabsystems/83728516bf6e7c3bebc107acdd9e54df to your computer and use it in GitHub Desktop.
Save mcnabsystems/83728516bf6e7c3bebc107acdd9e54df to your computer and use it in GitHub Desktop.
Convert Shopify Transporter import CSV phone numbers to E.164 format
<?php
// Create two directories.
// Place all your Transporter converted .csv files in shopify-customer-csvs
// Create empty directory /new-shopify-files
//
// In clude the following libraries
//
// 1. https://github.com/giggsey/libphonenumber-for-php
// 2. https://github.com/parsecsv/parsecsv-for-php
require __DIR__ . '/vendor/autoload.php';
$dir = "shopify-customer-csvs/*";
foreach(glob($dir) as $file) {
if(!is_dir($file)) {
echo basename($file)."<br />";
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$newCustomerCsv = __DIR__.'/new-shopify-files/'.basename($file);
$shopifycsv = new ParseCsv\Csv($file);
$newArray = array();
foreach($shopifycsv->data as $customerData) {
$phone = $customerData['Phone'];
$countryCode = $customerData['Country Code'];
//var_dump($customerData);
try {
$numberProto = $phoneUtil->parse($phone, $countryCode);
$e164Phone = $phoneUtil->format($numberProto, \libphonenumber\PhoneNumberFormat::E164);
$customerData['Phone'] = $e164Phone;
} catch (\libphonenumber\NumberParseException $e) {
// Do nothing, use the existing phone number
}
// Fix issue with line break in address
$customerData['Address1'] = str_replace ('\r\n', '\r', $customerData['Address1']);
$newArray[] = $customerData;
}
$shopifycsv->data = $newArray;
$shopifycsv->save($newCustomerCsv);
}
}
echo 'done';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment