Created
July 24, 2019 08:12
-
-
Save mcnabsystems/83728516bf6e7c3bebc107acdd9e54df to your computer and use it in GitHub Desktop.
Convert Shopify Transporter import CSV phone numbers to E.164 format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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