What i needed to do was send emails depending on where the order was made to the representive in that area. In my script I do a search on a database to check the postcode area, the code would look a lot more simple if search was done on a different criteria. I'm new to Oscommerce & PHP so the code may look very messy, any tips on how to approach writing the code better will be gratefully recieved.
the changes are made on catalog/checkout_process.php from line 266
tep_mail($order->customer['firstname'] . ' ' . $order->customer['lastname'], $order->customer['email_address'], EMAIL_TEXT_SUBJECT, nl2br($email_order), STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, '');
// send emails to other people
if (SEND_EXTRA_ORDER_EMAILS_TO != '') {
//(deej)here comes my code for sending to the nearest installer to you
//******************************************************************
// here comes my postcode search code
//******************************************************************
// postcode search with $order->delivery['postcode']
// Returns true if $str is a valid British postcode.
function valid_postcode ($str) {
return (ereg ('^[A-Z]{1,2}[0-9]{1,2}[A-Z]{0,1} [0-9][A-Z]{2}$', $str));
}
// Parses a valid British postcode into it's pieces, being;
// City/Town Code
// City/Town division number
// Postcode suffix
// Results are returned in an array (See example)
function parse_postcode ($str) {
$regs = array ();
ereg ('(^[A-Z]{1,2})([0-9]{1,2}[A-Z]{0,1}) ([0-9])([A-Z]{1})([A-Z]{1}$)', $str, $regs);
return ($regs);
}
$PostCodeAreasUK = array ("AB" => "Aberdeen", "AL" => "Saint Albans", "B" => "Birmingham", "BA" => "Bath", "BB" => "Blackburn", "BD" => "Bradford", "BH" => "Bournemouth", "BL" => "Bolton", "BN" => "Brighton", "BR" => "Bromley", "BS" => "Bristol", "BT" => "Belfast", "CA" => "Carlisle", "CB" => "Cambridge", "CF" => "Cardiff", "CH" => "Chester", "CM" => "Chelmsford", "CO" => "Colchester", "CR" => "Croydon", "CT" => "Canterbury", "CV" => "Coventry", "CW" => "Crewe", "DA" => "Dartford", "DD" => "Dundee", "DE" => "Derby", "DG" => "Dumfries and Galloway", "DH" => "Durham", "DL" => "Darlington", "DN" => "Doncaster", "DT" => "Dorchester", "DY" => "Dudley", "E" => "London East", "EC" => "London East Central", "EH" => "Edinburgh", "EN" => "Enfield", "EX" => "Exeter", "FK" => "Falkirk", "FY" => "Fylde", "G" => "Glasgow", "GL" => "Gloucester", "GU" => "Guildford", "GY" => "Guernsey", "HA" => "Harrow", "HD" => "Huddersfield", "HG" => "Harrogate", "HP" => "Hemel Hempstead", "HR" => "Hereford", "HS" => "Outer Hebrides", "HU" => "Hull", "HX" => "Halifax", "IG" => "Ilford and Barking", "IM" => "Isle of Man", "IP" => "Ipswich", "IV" => "Inverness", "JE" => "Jersey", "KA" => "Kilmarnock and Ayr", "KT" => "Kingston-upon-Thames", "KW" => "Kirk Wall", "KY" => "Kirkcaldy", "L" => "Liverpool", "LA" => "Lancaster", "LA" => "Llandrindod Wells", "LE" => "Leicester", "LL" => "Llandudno", "LN" => "Lincoln", "LS" => "Leeds", "LU" => "Luton", "M" => "Manchester", "ME" => "Medway", "MK" => "Milton Keynes", "ML" => "Motherwell", "N" => "London North", "NE" => "Newcastle-upon-Tyne", "NG" => "Nottingham", "NN" => "Northampton", "NP" => "Newport Gwent", "NR" => "Norwich", "NW" => "London North-West", "OL" => "Oldham", "OX" => "Oxford", "PA" => "Paisley", "PE" => "Peterborough", "PH" => "Perth", "PL" => "Plymouth", "PO" => "Portsmouth", "PR" => "Preston", "RG" => "Reading", "RH" => "Redhill", "RM" => "Romford", "S" => "Sheffield", "SA" => "Swansea", "SE" => "London Southeast", "SG" => "Stevenage", "SK" => "Stockport", "SL" => "Slough", "SM" => "Sutton and Merton", "SN" => "Swindon", "SO" => "Southampton", "SP" => "Salisbury Plain", "SR" => "Sunderland", "SS" => "Southend-on-Sea", "ST" => "Stoke-on-Trent", "SW" => "London South-West", "SY" => "Shrewsbury", "TA" => "Taunton", "TD" => "Tweeddale", "TF" => "Telford", "TN" => "Tonbridge", "TQ" => "Torquay", "TR" => "Truro", "TS" => "Teeside", "TW" => "Twickenham", "UB" => "Uxbridge", "W" => "London West", "WA" => "Warrington", "WC" => "London West Central", "WD" => "Watford", "WF" => "Wakefield", "WN" => "Wigan", "WR" => "Worcester", "WS" => "Walsall", "WV" => "Wolverhampton", "YO" => "York", "ZE" => "Shetland Islands");
$code = $order->delivery['postcode'];
$code = strtoupper($code);
if (valid_postcode ($code)) {
$parts = parse_postcode ($code);
$postcodeTown = $PostCodeAreasUK["$parts[1]"];
$query = "SELECT * FROM ccMembers WHERE user_from='$postcodeTown' && installer_approved='approved' ORDER BY user_id ASC ";
$searchResults = mysql_query($query) or die('error making query');
$resultRowCount = mysql_num_rows($searchResults);
$resultRow = mysql_fetch_assoc($searchResults);
$installerAddress = $resultRow['user_email'];
$installerFirstName = $resultRow['firstName'];
$installerLastName = $resultRow['lastName'];
$installerCompanyName = $resultRow['company_name'];
$installerTelephone = $resultRow['business_phone'];
$installerUsername = $resultRow['username'];
$installerEmailBody = 'Hi, Please contact '.$order->customer['firstname'].' '.$order->customer['lastname'].' either call them at '.$order->customer['telephone'].' or email them at '.$order->customer['email_address'].' about the order<br><br>'.$email_order. '<br><br>Please email CCTV-City at<a href="mailt:sales@cctv-city.com"> sales@cctv-city.com</a> as soon initial contact with the customer is made. ';
$installerTextSubject = 'CCTV-City.com order made in your area. ';
$installerContactedHeader = 'A CCTV-City.com installer has been contacted. ';
$installerInformation = 'Hi '.$order->customer['firstname'].' '.$order->customer['lastname'].'<br><br> We have contacted a CCTV-City registered installer to deal with your order. <br><br> Your installer is '.$installerFirstName.' '.$installerLastName.' from the company '.$installerCompanyName.' who can be contacted at <a href="'.$installerUsername.'@ccmail.co.uk">'.$installerUsername.'@ccmail.co.uk</a> or by phone at '.$installerTelephone.'. <br> If you would like to find out more about your installer please take a look at their personal web page on our website <a href="personalPage.php?username='.$installerUsername.'"> Installers Web Page</a><br><br>If you encounter any problems or are in any way not happy with your installer then please contact us immediately at <a href="mailto:sales@cctv-city.com">sales@cctv-city.com</a><br><br> Thank You<br><br>D.J.Millward ';
if(!isset($installerAddress)) {
$webMonitorTextSubject2 = 'An order has been made but there is no installer in that area ';
$webMonitorEmailBody2 = 'The following order has been made but there is no installer in the area requested.<br><br>'.$email_order. '';
tep_mail('', SEND_EXTRA_ORDER_EMAILS_TO, $webMonitorTextSubject2, nl2br($webMonitorEmailBody2), STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, '');
}
else {
tep_mail('', $installerAddress, $installerTextSubject, nl2br($installerEmailBody), STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, '');
$webMonitorTextSubject = 'An order has been made and sent to installer ';
$webMonitorEmailBody = 'Installer '.$installerFirstName.' '.$installerLastName.' from the company '.$installerCompanyName.' who can be contacted at '.$installerAddress.' has been informed of the order.<br><br>'.$email_order. '';
tep_mail('', SEND_EXTRA_ORDER_EMAILS_TO, $webMonitorTextSubject, nl2br($webMonitorEmailBody), STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, '');
tep_mail($order->customer['firstname'] . ' ' . $order->customer['lastname'], $order->customer['email_address'], $installerContactedHeader, nl2br($installerInformation), STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, '');
}
}









