1

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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;" . $opidaa['p_o'] . "&nbsp;" . $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'] . '&nbsp;&nbsp;' . $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:&nbsp;' . $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!

Jack Sniper
  • 389
  • 1
  • 8
  • 20

1 Answers1

1

You need to place this line and all the logic related to it in header.php at the very top before the initialisation of any other logic.

send_dropships_mail($dropship_array, $products_array, $order_id, $deliveryaddress_array);

The problem is that this function is being called very late in your code which is why all the older values are shown in your rendered HTML and when you refresh your page since the execution had already happened on your last load you see the new data.

Hope it helps ! :)

Zeeshan Abbas
  • 821
  • 6
  • 20
  • It makes sense, but the problem is that some of these arrays aren't defined until before the tag in the checkout_success.php file. Should I move all of the arrays to the header.php file, then use: if (stripos($_SERVER['REQUEST_URI'], 'checkout_success.php')) to make sure the function isn't executed on any other page other than checkout_success.php? – Jack Sniper Nov 25 '13 at 16:59