【问题标题】:Magento: adding pdf invoice to invoice emailMagento:将 pdf 发票添加到发票电子邮件
【发布时间】:2012-08-06 22:59:48
【问题描述】:

过去几天我一直在寻找示例,并一直在尝试自己做,但我陷入了以下困境。

我正在使用 Magento 1.7.0.2。 我想在生成发票时将发票 PDF 作为附件添加到交易电子邮件中。

我尝试了几件事,包括更改 /Mage/Sales/Model/Order/Invoice.php、/Mage/Core/Model/Email/Template.php 和 /Mage/Core/Model/Email/Template/Mailer.php .

我知道该过程涉及哪些文件,我知道在哪里添加附件,我只是不知道如何实际生成 PDF 并将其附加到电子邮件中。

magento 1.7.0.0 似乎也改变了电子邮件的生成方式,所有解释都是针对 1.6.x 的

我也知道有几个扩展名(即 FOOMAN 的),但我更喜欢自己更改文件(我希望尽可能保持我的安装清洁没有扩展名)。

【问题讨论】:

  • 你能发布你的解决方案吗?
  • 嗨,Guus,最后我没能做到。我切换到 prestashop,magento 对我的目的来说有点困难。祝你好运!

标签: php email magento invoice magento-1.7


【解决方案1】:

我在网上搜索了几个小时,但解决方案是针对旧版本的 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

【讨论】:

  • 感谢您的解决方案。但是,我建议扩展核心文件,而不是修改它们。
【解决方案2】:

你可能想签出

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() 的任何引用,但没有找到!很有趣。

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

【讨论】:

  • 感谢您的回复。可以帮助我的新信息。今晚我会再试一次你的信息。如果我成功了,我会在这里发布!再次感谢,如果有人有其他有用的信息,请告诉我!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多