当客户下订单时,我需要将文件附加到 Magento 发送的电子邮件中。
此附件可以是 PDF、HTML 或简单的 TXT,并且必须包含订单摘要(SKU、数量、单价、总价)。
我怎样才能做到这一点?
提前致谢!
最佳答案
解决方案不是很复杂,尽管您需要一些时间来实现它。我将简要解释所需的所有步骤。
主要步骤是:
1) 你需要重写
Mage_Sales_Model_Order
类(class)。覆盖该类中的 `sendNewOrderEmail()' 方法。您需要在此处编写要发送给客户的附件。复制原文
sendNewOrderEmail()
方法源代码添加到您的覆盖方法中,并将以下几行放在 $mailer->send()
之前(对于我们的示例,我们将采用简单的情况 - 我们将发送一个文本文件,其中仅包含订单的总计,附件将命名为“summary.txt”)$fileContents = "Hello, here is the copy of your invoice:\n";
$fileContents .= sprintf("Grand total: %.2f", $this->getGrandTotal()) . "\n";
$fileContents .= "Thank you for your visit!";
$fileName = 'summary.txt';
$mailer->addAttachment($fileContents, $fileName);
2) 重写
Mage_Core_Model_Email_Template_Mailer
- 添加方法addAttachment($fileContents, $fileName)
,这会将传递的附件添加到 protected 变量中,该变量存储附件数组。覆盖
send()
这个类中的方法。在该方法中,您需要将附件数组传递给每个发送的电子邮件模板。例如。添加行$emailTemplate->setAttachments($this->getAttachments());
就在
$emailTemplate->setDesignConfig...
行之前3) 重写
Mage_Core_Model_Email_Template
.添加方法
setAttachments($attachments)
,必须将传入的附件设置为某个 protected 变量。覆盖
send()
这个类中的方法。在这种方法中,您需要在已发送的信件中添加附件。把线条像foreach ($this->getAttachments() as $atInfo) {
$attachment = $mail->createAttachment($atInfo['fileContents']);
$attachment->filename = $atInfo['fileName'];
}
就在
$mail->send()
之前在那里排队。就这样。对于 Magento 开发人员来说,完成这项任务并不难。它只是需要一些时间来编写内容、重写类和完成接口(interface)。
关于email - 将附件添加到订单电子邮件 + Magento,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6662727/