Skip to content

Instantly share code, notes, and snippets.

@jacobdubail
Created March 27, 2017 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobdubail/6e8bbdb84a8fd3472c080f54b3ac1837 to your computer and use it in GitHub Desktop.
Save jacobdubail/6e8bbdb84a8fd3472c080f54b3ac1837 to your computer and use it in GitHub Desktop.
<?php
$time_start = microtime(true);
require('fc/datafeed-functions.php');
require('fc/Klaviyo.php');
require('fc/foxyapi.php');
$klaviyo = new Klaviyo(KLAVIYO_KEY);
$fc = jtd_bc_setup_foxycart_api();
$store = $fc->get($fc->getLink("fx:store"));
$store_url = $store['_links']['fx:store']['href'];
$store_links = $fc->get( $store_url );
$customers_next = $store_links['_links']['fx:customers']['href'];
$i = 1;
$j = 0;
$trews = '';
$falses = '';
do {
$customers_result = $fc->get($customers_next);
if ( ! $customers_result ) exit;
$customers_next = $customers_result['_links']['next']['href'];
$customers_last = $customers_result['_links']['last']['href'];
foreach ( $customers_result['_embedded']['fx:customers'] as $customer ) {
$status = jtd_bc_has_active_sub($customer['id'], $store_url);
$i++;
if ( $status ) {
$j++;
}
$string_status = ($status) ? 'true' : 'false';
$klaviyo_customer_update = array(
'$id' => $customer['id'],
'$first_name' => $customer['first_name'],
'$last_name' => $customer['last_name'],
'has-active-subscription' => $string_status
);
$identify = $klaviyo->identify(
$klaviyo_customer_update,
false // make async request
);
if ( $status ) {
$trews .= $customer['first_name'] . ',' . $customer['last_name'] . ',' . $string_status . "\r\n";
} else {
$falses .= $customer['first_name'] . ',' . $customer['last_name'] . ',' . $string_status . "\r\n";
}
}
} while ( $customers_next != $customers_last );
$time_end = microtime(true);
$execution_time = ($time_end - $time_start);
$message = "DONE! Processed $i records in $execution_time seconds. $j were active.\r\n\r\n\r\n";
mail(MY_EMAIL, 'Cron Stats', $message . $trews . $falses);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment