2013-11-25 83 views
1

我有一個工作函數,我在我的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: [email protected] \r\n" . 
      "Reply-To: [email protected] \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('[email protected]', 'Error sending product', 'The follow order was not sent to the drop shippers:&nbsp;' . $order_id); 
    } 

} 
} 

是有時間限制,多數民衆贊成設置成自從什麼?雖然這仍然無法解釋爲什麼頁面需要刷新才能使函數正常工作。任何幫助表示讚賞!

回答

1

在任何其他邏輯的初始化之前,您需要將該行及其相關的所有邏輯放置在header.php中最頂端。

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

的問題是,這個函數被調用你的代碼這就是爲什麼所有舊值顯示在您的渲染HTML,當您刷新頁面,因爲執行已經發生在你最後的負載非常晚你會看到新的數據。

希望它有幫助! :)

+0

它是有道理的,但問題是這些數組中的一些直到checkout_success.php文件中的標記之前才被定義。我應該將所有數組移動到header.php文件中,然後使用: if(stripos($ _ SERVER ['REQUEST_URI'],'checkout_success.php')) 確保函數不在任何checkout_success.php以外的其他頁面? –

相關問題