Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get all FoxyCart transactions for a given year
<?php
// @thatericsmith
class FoxyAPI{
public $domain = 'YOURDOMAIN.foxycart.com';
public $api_key = 'YOUR API KEY';
public $year = 2014;
public function api($action,$foxyData = array()){
// foxy api
$foxy_domain = $this->domain;
$foxyData["api_token"] = $this->api_key;
$foxyData["api_action"] = $action;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://" . $foxy_domain . "/api");
curl_setopt($ch, CURLOPT_POSTFIELDS, $foxyData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
// If you get SSL errors, you can uncomment the following, or ask your host to add the appropriate CA bundle
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = trim(curl_exec($ch));
// The following if block will print any CURL errors you might have
if ($response == false):
return false;
endif;
curl_close($ch);
libxml_use_internal_errors(true);
if(!$foxyResponse = simplexml_load_string($response, NULL, LIBXML_NOCDATA)):
libxml_clear_errors();
return false;
endif;
return $foxyResponse;
}
public function transactions(){
$retarray = array();
// foxy api - go through each month and get transactions
// works great as long as you have less than 300 trans per month
// it's to get around the 300 page limit
$action = "transaction_list";
$foxyData = array();
for($m = 1; $m <= 12; $m++):
$month = $m < 10 ? '0'.$m : $m;
$foxyData["transaction_date_filter_begin"] = $this->year.$month."-01";
$foxyData["transaction_date_filter_end"] = $this->year.$month."-31";
$foxyResponse = $this->api($action,$foxyData);
if($foxyResponse && $foxyResponse->result.'' == "SUCCESS"):
// NOW - we can proceed entering any new transactions into db - hurray!
foreach($foxyResponse->transactions[0]->transaction as $transaction):
$retarray[]= array($transaction->id,$transaction->order_total,date('j-n-Y',strtotime($transaction->transaction_date)));
endforeach;
endif;
endfor;
return $retarray;
}
}
$f = new FoxyAPI;
$ts = $f->transactions();
$total = 0;
$num_transactions = count($ts);
$ids = array();
echo "id,total,date\n";
foreach($ts as $t):
if(!in_array($t[0],$ids)):
$total+=$t[1];
echo implode(",",$t)."\n";
endif;
endforeach;
echo "TOTAL,$total,$num_transactions\n";
?>
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.