Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.