在magento系统中,默认是在支付成功后发送确认订单邮件的,但是有时我们会碰到支付不成功的情况或者客户不想支付,那我们怎样才能在支付前就发送订单邮件呢?首先我们打开
app/code/core/Mage/Checkout/Model/Type
文件夹下的Onepage.php文件,找到saveOrder()方法,可以看到有这么几句:
/*
*www.weicot.com
*1050653098@qq.com
*兔子
*/
$order = $service->getOrder();
if ($order) {
Mage::dispatchEvent('checkout_type_onepage_save_order_after', array('order'=>$order, 'quote'=>$this->getQuote()));
$redirectUrl = $this->getQuote()->getPayment()->getOrderPlaceRedirectUrl();
if(!$redirectUrl){
try {
$order->sendNewOrderEmail();
} catch (Exception $e) {
Mage::logException($e);
}
}
// add order information to the session
$this->_checkoutSession->setLastOrderId($order->getId())
->setRedirectUrl($redirectUrl)
->setLastRealOrderId($order->getIncrementId());
// as well a billing agreement can be created
$agreement = $order->getPayment()->getBillingAgreement();
if ($agreement) {
$this->_checkoutSession->setLastBillingAgreementId($agreement->getId());
}
}
可以发现magento系统是在保存订单数据后就立即跳转到支付URL上去了,而如果没有支付跳转,则会发送邮件。那我们现在就可以把
if(!$redirectUrl){
try {
$order->sendNewOrderEmail();
} catch (Exception $e) {
Mage::logException($e);
}
}
中的if条件注释掉,这样就可以保证无论是否存在支付跳转,系统都会发送订单确认邮件。
或者你可以这样 同上
if(!$redirectUrl){
try {
$order->sendNewOrderEmail();
} catch (Exception $e) {
Mage::logException($e);
}
}
然后在你需要的地方加
$order = Mage::getModel('sales/order');
$order->load(Mage::getSingleton('checkout/session')->getLastOrderId());
$order->sendNewOrderEmail();
$order->setEmailSent(true);
$order->save();
调用函数原型
app\code\core\Mage\Sales\Model/Order
public function queueNewOrderEmail($forceMode = false)
{
$storeId = $this->getStore()->getId();
if (!Mage::helper('sales')->canSendNewOrderEmail($storeId)) {
return $this;
}
// Get the destination email addresses to send copies to
$copyTo = $this->_getEmails(self::XML_PATH_EMAIL_COPY_TO);
$copyMethod = Mage::getStoreConfig(self::XML_PATH_EMAIL_COPY_METHOD, $storeId);
// Start store emulation process
/** @var $appEmulation Mage_Core_Model_App_Emulation */
$appEmulation = Mage::getSingleton('core/app_emulation');
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);
try {
// Retrieve specified view block from appropriate design package (depends on emulated store)
$paymentBlock = Mage::helper('payment')->getInfoBlock($this->getPayment())
->setIsSecureMode(true);
$paymentBlock->getMethod()->setStore($storeId);
$paymentBlockHtml = $paymentBlock->toHtml();
} catch (Exception $exception) {
// Stop store emulation process
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
throw $exception;
}
// Stop store emulation process
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
// Retrieve corresponding email template id and customer name
if ($this->getCustomerIsGuest()) {
$templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $storeId);
$customerName = $this->getBillingAddress()->getName();
} else {
$templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $storeId);
$customerName = $this->getCustomerName();
}
/** @var $mailer Mage_Core_Model_Email_Template_Mailer */
$mailer = Mage::getModel('core/email_template_mailer');
/** @var $emailInfo Mage_Core_Model_Email_Info */
$emailInfo = Mage::getModel('core/email_info');
$emailInfo->addTo($this->getCustomerEmail(), $customerName);
if ($copyTo && $copyMethod == 'bcc') {
// Add bcc to customer email
foreach ($copyTo as $email) {
$emailInfo->addBcc($email);
}
}
$mailer->addEmailInfo($emailInfo);
// Email copies are sent as separated emails if their copy method is 'copy'
if ($copyTo && $copyMethod == 'copy') {
foreach ($copyTo as $email) {
$emailInfo = Mage::getModel('core/email_info');
$emailInfo->addTo($email);
$mailer->addEmailInfo($emailInfo);
}
}
// Set all required params and send emails
$mailer->setSender(Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId));
$mailer->setStoreId($storeId);
$mailer->setTemplateId($templateId);
$mailer->setTemplateParams(array(
'order' => $this,
'billing' => $this->getBillingAddress(),
'payment_html' => $paymentBlockHtml
));
/** @var $emailQueue Mage_Core_Model_Email_Queue */
$emailQueue = Mage::getModel('core/email_queue');
$emailQueue->setEntityId($this->getId())
->setEntityType(self::ENTITY)
->setEventType(self::EMAIL_EVENT_NAME_NEW_ORDER)
->setIsForceCheck(!$forceMode);
$mailer->setQueue($emailQueue)->send();
$this->setEmailSent(true);
$this->_getResource()->saveAttribute($this, 'email_sent');
return $this;
}
/**
* Send email with order data
*
* @return Mage_Sales_Model_Order
*/
public function sendNewOrderEmail()
{
$this->queueNewOrderEmail(true);
return $this;
转载请注明:(●--●) Hello.My Weicot » magento 关于跳转到支付界面前和跳转到支付界面后自动发送邮件