DE: Hier ein paar hilfreiche Code-Snippets für custom plugin development. Soll zeigen, wie man eine Rechnung via E-Mail-Anhang aus seinen Plugins versendet.
EN: Here’s additional documentation on how to create and send Shopware emails + attachments.
// How to send an already generated pdf invoice
try {
$emailTemplete = 'sORDERDISPATCH';
$mail = Shopware()->TemplateMail()->createMail($emailTemplete, $dataEmail);
$mail->addTo($dataEmail['email']);
if ($needsInvoice) {
$path = $this->getInvoicePath($order['id']);
$content = file_get_contents($path);
$attachment = new \Zend_Mime_Part($content);
$attachment->type = 'application/pdf';
$attachment->disposition = \Zend_Mime::DISPOSITION_ATTACHMENT;
$attachment->encoding = \Zend_Mime::ENCODING_BASE64;
$attachment->filename = 'Rechnung_' . $order['ordernumber'] . '.pdf';
$mail->addAttachment($attachment);
}
return $mail->send();
} catch (\Exception $e) {
return false;
}
// how to get the file path of an invoice via orderId
// orderId = s_order_documents.orderID
public function getInvoicePath($orderId)
{
$sql = 'SELECT hash FROM `s_order_documents` WHERE `orderID` = ?';
$stmt = Shopware()->Db()->executeQuery($sql, [$orderId])->fetchAll();
if ($stmt[0]['hash']) {
// only use this when using Shopware version < 5.4.
// check the comments for additional informations
return Shopware()->DocPath() . 'files/documents' . '/' . $stmt[0]['hash'] . '.pdf';
}
throw new \Exception('Could not find hash for invoice pdf file.');
}
// How to generate a pdf invoice for a specific order
/**
* Generate the invoice document for an order
*
* @param int $orderId
* @return void
*/
public function generateInvoice($orderId)
{
// Document Generator Settings
$renderer = "pdf";
$documentType = 1;
$displayDate = date("d.m.Y");
// PDF Document setup
$document = \Shopware_Components_Document::initDocument(
$orderId,
$documentType,
[
'netto' => false,
'bid' => null,
'voucher' => null,
'date' => $displayDate,
'delivery_date' => null,
'shippingCostsAsPosition' => true,
'_renderer' => $renderer,
'_preview' => false,
'_previewForcePagebreak' => null,
'_previewSample' => null,
'docComment' => null,
'forceTaxCheck' => false,
]
);
// Create the document
$document->render();
return $document;
}
DE: Wünsche und Anregungen/Änderungen etc. gerne in die Kommentare.
EN: Feel free to comment when you have important notes/changes.
PS: @shopware PLEASE update your docs, its so bad and nobody wants to work with shopware because you need to figure out everything yourself. Furthermore, make your forum more userfriendly. I cant even get proper Syntax-Highlighting when in preview mode!