Skip to content

Instantly share code, notes, and snippets.

@outflux3
Last active December 29, 2015 03:39
Show Gist options
  • Save outflux3/7608974 to your computer and use it in GitHub Desktop.
Save outflux3/7608974 to your computer and use it in GitHub Desktop.
adapted modx foxycart script for processwire..
<?
/*
Title:
FoxyCart_Inventory
Description:
Uses FoxyCart.com's XML Data Feeds to modify Processwire data.
Version:
1.0.0
2013-11-22
*/
// ======================================================================================
// SET THE TEMPLATE VARIABLES and OTHERS
// *** MODIFY THIS SECTION ***
// ======================================================================================
// Finally, set your key to decrypt the XML that you receive from FoxyCart.
// This must match *exactly* what you entered at http://www.foxycart.com/admin
$key = 'api key';
// Optional: If you'd like to keep a log on your server of the results of this snippet, uncomment the
// following line, and put in an appropriate path.
// This is primarily intended for debugging when doing a custom implementation.
//$myFile = "/path/to/foxycart_datafeed.log";
// setup paths on server to the shared files
// get these from http://modx.com/extras/package/foxycartinventory
$rc4cryptPath = "/path/to/class.rc4crypt.php";
$xmlParserPath = "/path/to/class.xmlparser_php5.php";
// Edit the section below for processwire specific stuff
// ===================================================================================================
// CHECK THE XML
// (do not modify past this point)
// ===================================================================================================
$output = '';
if (isset($_POST["FoxyData"])) {
// ===================================================================================================
// DECRYPT YOUR DATA
// (do not modify)
// ===================================================================================================
// Decrypt the data using your $key
include $rc4cryptPath;
// Then decrypt the XML
$FoxyData_encrypted = urldecode($_POST["FoxyData"]);
$FoxyData_decrypted = rc4crypt::decrypt($key,$FoxyData_encrypted);
// ===================================================================================================
// PARSE YOUR XML
// (do not modify)
// ===================================================================================================
// We now have a big fat XML that we can do whatever we want with.
include $xmlParserPath;
//Set up the parser object
$data = new XMLParser($FoxyData_decrypted);
//Work the magic...
$data->Parse();
// ===================================================================================================
// MODIFY THE INVENTORY TV VALUE
// ===================================================================================================
//$output .= print_r($data, 1);
// The XML is now a nice array called $data. Let's have fun.
// First, loop through the transactions
if (is_object($data)) {
// First, loop through the transactions
foreach ($data->document->transactions[0]->transaction as $transaction) {
// Then through the products per transaction
foreach ($transaction->transaction_details[0]->transaction_detail as $detail) {
// Then get the codes
if ($detail->product_code != '') {
// Set the current code and quantity
$code = $detail->product_code[0]->tagData;
$output .= "code = $code \n";
$quantity = $detail->product_quantity[0]->tagData;
$output .= "quantity = $quantity \n";
// First get the Product of the product code field
$product = $pages->get("template=product-bstock, product_sku=$code");
$output .= "Product = {$product->title} \n";
//Decrement the inventory by the quantity purchased
if($product && ($product->stock_qty > 0)) {
$curQty = $product->stock_qty;
$newQty = $curQty - $quantity;
$product->setOutputFormatting(false);
$product->stock_qty = $newQty;
$product->save();
}
}
}
}
// That was fun.
// Are we satisfied? If so, return "foxy" so these transactions can be marked as delivered.
// Dont't forget, you can always export your data directly from the store admin page as well.
//$output = "foxy";
die("foxy");
} else {
// It's not an object, which means something went wrong (like an incorrect key).
//$output = "error: not an object";
die('error: not an object');
}
} else {
// If you're here, you didn't receive the FoxyData post, so error out.
// This ensures that the transactions will still show up on the next datafeed.
//$output = "error: no post data";
die('error: no post data');
}
// ===================================================================================================
// WRITE THE RESULTS TO A FILE ON YOUR SERVER
// ===================================================================================================
if ($myFile) {
$spacer = "===============\n";
$date = date("Y-m-d H:i:s") . "\n";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, $spacer);
fwrite($fh, $output);
fwrite($fh, $date);
fwrite($fh, $spacer);
fwrite($fh, $FoxyData_decrypted);
fclose($fh);
}
return $output;
echo $output;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment