Last active
October 21, 2015 16:56
-
-
Save msigley/02b2502e821cb97e05c6 to your computer and use it in GitHub Desktop.
Shopp Customer Import from CSV CLI
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 | |
/** | |
* WB-4091: Take input from CSV file and create WP/Shopp customers for them | |
* Should be invoked on the CLI to web timeouts | |
* Usage: ango_td_customer_import [filename] | |
*/ | |
require "../../wp-content/plugins/untangle-store/cli_init.php"; | |
/************************** | |
* * | |
* FUNCTIONS * | |
* * | |
**************************/ | |
/** | |
* Standardizes data read in from a csv file so that shopp can understand it | |
* @param array $row row of data read in from CSV | |
* @return stdClass modeled after a ShoppCustomer object | |
*/ | |
function standardizeData($row) | |
{ | |
//check to make sure all required data is present | |
foreach ([$row[0],$row[1],$row[2],$row[3]] as $data) { | |
if (empty($data)) { | |
return false; | |
} | |
} | |
$standardized = new stdClass(); | |
$standardized->fname = $row[0]; | |
$standardized->lname = $row[1]; | |
$standardized->email = $row[2]; | |
$standardized->password = $row[3]; | |
$standardized->state = $row[4] ?: 'CA'; | |
$standardized->postcode = $row[5]; | |
$standardized->country = $row[6] ?: 'US'; | |
return $standardized; | |
} | |
/** | |
* Translates seconds into something more readable by regular folks | |
* @param $secs | |
* @return string | |
*/ | |
function time_elapsed($secs) { | |
$bit = array( | |
'y' => $secs / 31556926 % 12, | |
'w' => $secs / 604800 % 52, | |
'd' => $secs / 86400 % 7, | |
'h' => $secs / 3600 % 24, | |
'm' => $secs / 60 % 60, | |
's' => $secs % 60 | |
); | |
foreach ($bit as $k => $v) | |
if ($v > 0) | |
$ret[] = $v . $k; | |
return join(' ', $ret); | |
} | |
/** | |
* Translates bytes in KB or MB | |
* @param $mem_usage | |
* @return string | |
*/ | |
function echo_memory_usage($mem_usage) { | |
if ($mem_usage < 1024) | |
return $mem_usage . " bytes"; | |
elseif ($mem_usage < 1048576) | |
return round($mem_usage / 1024, 2) . " kilobytes"; | |
else | |
return round($mem_usage / 1048576, 2) . " megabytes"; | |
} | |
/************************** | |
* * | |
* START SCRIPT EXECUTION * | |
* * | |
**************************/ | |
//use make sure a file is passed as an argument | |
if ($argc != 2) { | |
echo 'USAGE: ango_td_customer_import [filename]'; | |
exit; | |
} else { | |
$fileHandler = fopen($argv[1], 'r'); | |
} | |
//initialize failure variable. Used to notify me after this script runs. | |
$failures = array(); | |
$startTime = time(); | |
$totalCustomers = 0; | |
$customer = 0; | |
//get a count of totalcustomers | |
while (fgetcsv($fileHandler, 1000, ',')) { | |
$totalCustomers++; | |
} | |
rewind($fileHandler); | |
//process these suckas | |
while (($row = fgetcsv($fileHandler, 1000, ',')) !== false) { | |
//check to see if customer already exists | |
$shoppCustomer = shopp_customer_exists($row[2], 'email'); | |
if ($shoppCustomer) { | |
//customer already exists | |
$customer++; | |
continue; | |
} | |
//standardize data | |
$accountInfo = standardizeData($row); | |
if (!$accountInfo) { | |
echo 'Missing info on row ' . $customer . PHP_EOL; | |
$failures[] = 'Missing info on row ' . $customer . '\n'; | |
$customer++; | |
continue; | |
} | |
//create wp/shopp accounts | |
$shoppId = createWordpressAndShoppAccounts($accountInfo); | |
if (!$shoppId) { | |
//failed to create the accounts | |
echo 'Could not create accounts for customer on row ' . $customer . PHP_EOL; | |
$failures[] = 'Could not create accounts for customer on row ' . $customer . '\n'; | |
$customer++; | |
continue; | |
} | |
//record remaining execution time | |
$runTime = time() - $startTime; | |
if ($customer % 50 == 0) { | |
$estimateTime = round($runTime / ($customer / $totalCustomers), 0) - $runTime; | |
echo " Processing cust # $customer of $totalCustomers. RunTime: " . | |
time_elapsed($runTime) . " Estimated Remaining: " . | |
time_elapsed($estimateTime) . " Memory Usage Now: " . | |
echo_memory_usage(memory_get_usage(true)) . PHP_EOL; | |
} | |
$customer++; | |
unset($row, $shoppCustomer, $accountInfo, $shoppId); | |
} | |
if (!empty($failures)) { | |
//there were some failures detected - notify me | |
wp_mail('[email protected]', 'Customer Account Failures', implode(', ', $failures)); | |
} |
I can't update the gist, but here is the function.
/**
* Creates Wordpress/Shopp accounts for the customer.
* @param stdClass $accountData The data for the new account.
* @return string The newly created Shopp Account Id.
*/
function createWordpressAndShoppAccounts($accountData)
{
foreach (['email', 'password', 'fname', 'lname'] as $reqField) {
if (empty($accountData->$reqField)) {
return false;
}
}
$wpAccountId = wp_create_user($accountData->email, $accountData->password, $accountData->email);
if (is_wp_error($wpAccountId)) {
utStoreLog("Could not create a Wordpress account for email {$accountData->email}");
return false;
}
//set first name and last name on the newly created account
update_user_meta($wpAccountId, 'first_name', $accountData->fname);
update_user_meta($wpAccountId, 'last_name', $accountData->lname);
//create shopp customer and link it to wpUser
//shopp account
$shoppData['wpuser'] = $wpAccountId;
$shoppData['firstname'] = $accountData->fname;
$shoppData['lastname'] = $accountData->lname;
$shoppData['email'] = $accountData->email;
$shoppData['type'] = 'Direct';
$shoppData['phone'] = $accountData->phone ?: '';
$shoppData['company'] = $accountData->company ?: '';
//billing address
$shoppData['baddress'] = $accountData->address1 ?: '';
$shoppData['xbaddress'] = $accountData->address2 ?: '';
$shoppData['bcity'] = $accountData->city ?: '';
//default to CA/US - Zuora requires State/Country, just keeping it consistent
$shoppData['bstate'] = $accountData->state ?: 'CA';
$shoppData['bcountry'] = $accountData->country ?: 'US';
$shoppData['bpostcode'] = $accountData->postcode ?: '';
$shoppData['bgeocode'] = '';
//shipping address
$shoppData['saddress'] = $accountData->address1 ?: '';
$shoppData['xsaddress'] = $accountData->address2 ?: '';
$shoppData['scity'] = $accountData->city ?: '';
$shoppData['sstate'] = $accountData->state ?: 'CA';
$shoppData['scountry'] = $accountData->country ?: 'US';
$shoppData['spostcode'] = $accountData->postcode ?: '';
$shoppData['sgeocode'] = '';
$shoppCustomerId = shopp_add_customer($shoppData);
if (!$shoppCustomerId) {
utStoreLog("Could not create a Shopp account for email {$accountData->email}");
return false;
}
return $shoppCustomerId;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where is the source for createWordpressAndShoppAccounts()?