Profihorst1Profihorst1 MemberComments: 3 Received thanks: 1 Member since: November 2018 edited November 2018

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!

 

Thanked by 1Shopwareianer

Comments

  • ShyimShyim MemberComments: 831 Received thanks: 335 edited November 2018 Member since: May 2015

    "return Shopware()->DocPath() . 'files/documents' . '/' . $stmt[0]['hash'] . '.pdf';" sollte schon ab Shopware 5.4 nicht mehr verwendet werden.

    von 5.4.0 - 5.4.6 shopware.app.documentsdir aus dem DI nehmen, ab 5.5 dann private filesystem

    Thanked by 1Profihorst1
  • Profihorst1Profihorst1 MemberComments: 3 Received thanks: 1 Member since: November 2018

    Hallo Shyim, 

    vielen Dank, arbeite noch auf der Shopware Version 5.2.2.

    Ich werd mal oben die Versionen kennzeichnen!

  • xxonxxxonx MemberComments: 6 Received thanks: 2 Member since: June 25

    Danke dir Profihorst, genau was ich gebraucht habe!

    Und ich bin ganz bei dir, die Shopware Dokumentation ist wirklich schlecht. Es ist extrem mühsam irgendwas herauszufinden und der Support hier ist auch dürftig! Hält sicher einige Entwickler davon ab mit Shopware zu arbeiten - bin selbst auch am Überlegen ob ichs nicht bald bleiben lasse.

    Thanked by 1wienerpresswerk
Sign In or Register to comment.