页面加载时不会触发函数

时间:2013-11-25 16:21:11

标签: php function email oscommerce

我有一个我在osCommerce页面中创建的工作函数,该页面对产品进行分类并相应地将它们发送给托运人。它工作得很好,但不是在第一页加载。实际上,就好像函数永远不会被调用,直到页面在第一次加载后刷新。我真的需要让这个功能在第一页加载时执行,以确保这些产品通过电子邮件发送给正确的公司。我在checkout_success.php中调用该函数。我在文件中的标记上方有以下内容:

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

我的功能:

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);
    }

}
}

是否存在设置为osCommerce的时间限制?虽然这仍然无法解释为什么页面需要刷新才能使功能正常工作。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

在初始化任何其他逻辑之前,您需要将此行以及与header.php相关的所有逻辑放在最顶层。

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

问题是这个函数在代码中很晚才被调用,这就是为什么所有较旧的值都显示在渲染的HTML中,当你刷新页面时,因为你的上一次加载已经发生了执行,你会看到新的数据

希望它有所帮助! :)