2

I have been searching for examples the last couple of days and have been trying to do it myself, but I'm stuck at the following.

I'm using Magento 1.7.0.2. I want to add the invoice PDF as an attachment to the transactional email when generating the invoice.

I have tried several things including changing /Mage/Sales/Model/Order/Invoice.php, /Mage/Core/Model/Email/Template.php and /Mage/Core/Model/Email/Template/Mailer.php.

I know which files are involved in the process, I know where to add the attachment, I just can't figure out how to actually generate the PDF and attach it to the email.

It also seems that the way emails are generated has been changed with magento 1.7.0.0 and all explanations are for 1.6.x

I'm also aware that there are several extentions (i.e. FOOMAN's), but I prefer to change the files myself (I want to keep my installation clean of extentions as much as possible).

4

2 回答 2

3

我在网上搜索了几个小时,但解决方案是针对旧版本的 Magento,我的版本是 1.7.0.2。Magento 处理电子邮件的方式也发生了变化。以下是我的操作步骤,希望对你有所帮助。

1)更改app/code/core/Mage/Core/Model/Email/Template/Mailer.php

Part 1, add protected $emailTemplate; to the top of the class:

class Mage_Core_Model_Email_Template_Mailer extends Varien_Object
{
/**
* List of email infos
* @see Mage_Core_Model_Email_Info
*
* @var array
*/
protected $_emailInfos = array();

// Add the following one line

protected $emailTemplate;

第 2 部分更新函数 send()

public function send()
{

// the original was $emailTemplate = Mage::getModel('core/email_template');
// Change it to the following four lines:

if ($this->emailTemplate)
$emailTemplate = $this->emailTemplate;
else
$emailTemplate = Mage::getModel('core/email_template');

第 3 部分将函数添加到类的末尾:

public function addAttachment(Zend_Pdf $pdf, $filename){
$file = $pdf->render();

$this->emailTemplate = Mage::getModel('core/email_template');



$attachment = $this->emailTemplate->getMail()->createAttachment($file);
$attachment->type = 'application/pdf';
$attachment->filename = $filename;
}

2) 更新 app/code/core/Mage/Sales/Model/Order/Invoice.php

Update sendEmail function

$mailer = Mage::getModel('core/email_template_mailer');

// the next two lines were added

$pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf(array($this));
$mailer->addAttachment($pdf,'invoice.pdf');



if ($notifyCustomer) {
$emailInfo = Mage::getModel('core/email_info');
$emailInfo->addTo($order-

原始网址在这里: http ://www.ericpan.com/2013/02/14/add-pdf-attachment-to-invoice-email/#.USPAKR2t2-0

于 2013-02-19T18:08:18.263 回答
0

您可能想查看

Mage_Adminhtml_Controller_Sales_Invoice

要查看生成的销售发票 pdf 的示例:

$pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf(array($invoice));
$this->_prepareDownloadResponse('invoice'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').
    '.pdf', $pdf->render(), 'application/pdf');

现在这实际上是渲染 PDF 以将其包含在 HTTP 响应中,而不是将其保存为文件,但发票模型或抽象 pdf 模型中应该有一个方法来保存 pdf。

然后,要添加附件,您可能需要查看:

Zend_Mail::addAttachment()

我没有方便的示例,实际上搜索了 Magento 1.7 以获取任何参考addAttachment(),但没有找到任何参考!有趣的。

但希望这能给你一点方向。

于 2012-08-09T21:44:54.607 回答