The following code updates products in a db by opening a csv file and updating a product already in the db using the newly update prices in the csv file.
Now we've been running the file for some time manually I wish to make it automated via cron so have added a line at the end to mail me when the script is complete. How would I look to add a count to this script so that the email message can tell me how many products were updated and maybe even email me the log that is created as that tells me which products weren't updated?
EDIT: Think the answer is already in the script near the bottom but I cannot work out how to echo that figure on my email...
To add as well, it is currently missing the first row of the file. How can I change this as there is no header rom in my data?
<?php
/* Markup percentage */
$percent = 0.xx;
if(is_file('./log/eos_update.log'))
{
rename('./log/eos_update.log', './log/eos_update-'.time().'.log');
}
$proxy = new SoapClient('http://myofficestationery.com/api/soap/?wsdl=1');
$sessionId = $proxy->login('import', 'import');
$handle = fopen('eos.csv', "r");
$i=0;
echo "Started\n";
$failed_stack = array();
while($data = fgetcsv($handle, 1000, ",","\""))
{
if($i!=0) {
$product_code = trim($data[0]); // not necessary but it cast $data[0] to string (just to ensure we're updating by SKU and not by ID)
$price = $data[4];
$price = $price / $percent;
$price = number_format($price, 2, '.', '');
echo $product_code.' - '.$price;
try {
$proxy->call($sessionId, 'product.update', array($product_code, array('price'=>$price)));
} catch (SoapFault $e) {
array_push($failed_stack, $product_code);
echo 'Unable to find: '.$product_code.' - skipped and not updated';
}
echo "\n";
}
++$i;
}
if(count($failed_stack) > 0) {
$output = "Failed: ".count($failed_stack)."\n";
foreach($failed_stack as $key => $value) {
$output .= " +----- ".$value."\n";
}
file_put_contents('./log/eos_update.log',$output);
}
rename('eos.csv', './log/eos.csv-'.time());
echo "Done\n";
/* Send email to admin when script complete */
mail('paul@email.co.uk', 'Weekly EOS Update', 'The weekly EOS update is complete.');
fclose($handle);
?>