I have a working function that I created within my osCommerce page, which sorts products and delivers them to the drop shippers accordingly. It works great, but not on the first page load. In fact its as if the function is never called on until the page is refreshed after it loads up the first time. I really need to get this function to execute on first page load to ensure that these products are emailed to the correct companies. I am calling on the function in checkout_success.php. I have the following just above the tag within the file:
send_dropships_mail($dropship_array, $products_array, $order_id, $deliveryaddress_array);
My function:
function send_dropships_mail($dropship_array, $products_array, $order_id, $deliveryaddress_array) {
// Create new dropships array indexed by dsid
$newDropships = array();
foreach ($dropship_array as $dropship) {
$newDropships[$dropship['id']] = $dropship;
}
// Perform grouping of products by dsid
// Array of 'dsid' => array of product indices
$dstToProduct = array();
foreach ($products_array as $i => $product) {
if (!isset($dstToProduct[$product['dsid']])) {
$dstToProduct[$product['dsid']] = array();
}
$dstToProduct[$product['dsid']][] = $i;
}
$orders_products_id_query = tep_db_query("select orders_products_id from orders_products where orders_id = " . $order_id);
while ($ipidq = tep_db_fetch_array($orders_products_id_query)) {
$orders_products_id_array[] = array('orders_products_id' => $ipidq['orders_products_id']);
}
$orders_products_id_attributes_query = tep_db_query("select orders_products_id, products_options, products_options_values from orders_products_attributes where orders_id = " . $order_id);
while ($opidq = tep_db_fetch_array($orders_products_id_attributes_query)) {
$orders_products_id_attributes_array[] = array('orders_products_id' => $opidq['orders_products_id'],
'p_o' => $opidq['products_options'],
'p_o_v' => $opidq['products_options_values']);
}
$p_attribute = "";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: noreply@email.com \r\n" .
"Reply-To: me@email.com \r\n" .
"X-Mailer: PHP/" . phpversion();
// Now we are ready to send emails
foreach ($dstToProduct as $dsid => $productIndices) {
$email = $newDropships[$dsid]['email'];
$subject = "A new order has been placed";
// Build message text
$date = date('m/d/Y');
$text = '<span style="color: #513311; font-size: 14px;"><table cellpadding="3" style="margin-top: 20px;"><tr style="background-color: #6d7d59; color: #ffffff; font-weight: bold; font-size: 12px;"><td style="width: 240px; vertical-align:text-top;">Product Name</td><td style="width: 120px; vertical-align:text-top;">Model Number</td><td style="width: 80px; vertical-align:text-top;">Quantity</td><td style="width: 80px; vertical-align:text-top;">Price</td></tr>';
foreach ($productIndices as $productIndex) {
$p_attribute = "";
if (count($orders_products_id_attributes_array) > 0) {
foreach ($orders_products_id_attributes_array as $opidaa) {
if ($products_array[$productIndex]['orders_products_id'] == $opidaa['orders_products_id']) {
$p_attribute .= "<i> - " . $opidaa['p_o'] . " " . $opidaa['p_o_v'] . "</i><br>";
} else {
$p_attribute = "";
}
}
}
if ($p_attribute == "") {
$text .= '<tr style="background-color: #f0f0f0; color: #513311; font-size: 12px;"><td style="vertical-align:text-top;">' . $products_array[$productIndex]["text"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["model"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["qty"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["price"] . '</td></tr>';
} else {
$text .= '<tr style="background-color: #f0f0f0; color: #513311; font-size: 12px;"><td>' . $products_array[$productIndex]["text"] . '<br>' . $p_attribute . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["model"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["qty"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["price"] . '</td></tr>';
}
}
$text .= '</table>';
$text .= '<table cellpadding="3" style="margin-top: 20px;"><tr style="background-color: #6d7d59; color: #ffffff; font-weight: bold; font-size: 12px;"><td style="width: 200px;">Delivery Address</td></tr><tr style="background-color: #f0f0f0; color: #513311; font-size: 12px;"><td>' . $deliveryaddress_array[0]['name'] . '<br>' . $deliveryaddress_array[0]['address'] . '<br>' . $deliveryaddress_array[0]['city'] . ', ' . $deliveryaddress_array[0]['state'] . ' ' . $deliveryaddress_array[0]['zip'] . '</td></tr></table>';
if (!mail($email, $subject, $text, $headers)) {
mail('me@email.com', 'Error sending product', 'The follow order was not sent to the drop shippers: ' . $order_id);
}
}
}
Is there a time limit thats set into osCommerce or something? Though that still wouldn't explain why the page needs to be refreshed inorder for the function to work. Any help is appreciated!