0

i want to attach invoice pdf with shipment email so how can we attach invoice pdf in shipment email as i am using magento 2 mageplaza pdfinvoice module. it attach only one pdf at one time.

0

Path : Magento root directory/app/code/Mageplaza/PdfInvoice/Model/Template/SenderBuilder.php

Replace This file from mageplaza pdfinvoice module

<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_PdfInvoice
 * @copyright   Copyright (c) 2017-2018 Mageplaza (https://www.mageplaza.com/)
 * @license     http://mageplaza.com/LICENSE.txt
 */

namespace Mageplaza\PdfInvoice\Model\Template;

use Magento\Framework\App\ObjectManager;
use Mageplaza\PdfInvoice\Helper\Data;
use Mageplaza\PdfInvoice\Helper\PrintProcess;
use Mageplaza\PdfInvoice\Model\Source\Type;
use Zend\Mail\Message as Message;
use Zend\Mime\Mime;
use Zend\Mime\Part as MimePart;


use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Mail\Template\TransportBuilderByStore;
use Magento\Sales\Model\Order\Email\Container\IdentityInterface;
use Magento\Sales\Model\Order\Email\Container\Template;
use Magento\Framework\Mail\MailMessageInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Mail\Template\SenderResolverInterface;
/**
 * Class SenderBuilder
 * @package Mageplaza\PdfInvoice\Model\Template
 */
class SenderBuilder extends \Magento\Sales\Model\Order\Email\SenderBuilder
{ 
    /**
     * @var SenderResolverInterface
     */
    protected $senderResolver;

    public function __construct(
        Template $templateContainer,
        IdentityInterface $identityContainer,
        ObjectManagerInterface $objectManager,
        SenderResolverInterface $senderResolver,
        array $attachments = []
    ) {
        /** @var MessageInterface $message */
        $message = $objectManager->create(\Ebizmarts\Mandrill\Model\Message::class);
        /** @var TransportBuilder $transportBuilder */
        $transportBuilder = $objectManager->create(
            TransportBuilder::class,
            ["message" => $message]
        );
        /** @var TransportBuilderByStore $transportBuilderByStore */
        $transportBuilderByStore = $objectManager->create(
            TransportBuilderByStore::class,
            ["message" => $message]
        );
        parent::__construct($templateContainer, $identityContainer, $transportBuilder, $transportBuilderByStore);
        $this->senderResolver = $senderResolver;
    }
    /**
     * @inheritdoc
     */
   public function send()
    {

        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info(get_class($this->transportBuilder));
        $this->attachPDF();
        parent::send();
    }

    /**
     * @inheritdoc
     */
    public function sendCopyTo()
    {
        $this->attachPDF();
        parent::sendCopyTo();
    }

    protected function configureEmailTemplate()
    {
        parent::configureEmailTemplate();
        $this->transportBuilder->setFrom(
            $this->senderResolver->resolve(
                $this->identityContainer->getEmailIdentity(),
                $this->identityContainer->getStore()->getId()
            )
        );
    }
    /**
     * Attach pdf
     */
    public function attachPDF()
    {
        $objectManager = ObjectManager::getInstance();
        $configHelper  = $objectManager->get(Data::class);

        $templateVars = $this->templateContainer->getTemplateVars();
        $store        = $templateVars['store'];

        if ($configHelper->isEnabled($store->getId())) {
            try {
                $dataHelper = $objectManager->get(PrintProcess::class);
                $content2 = null;
                $fileName2 = null;
                if (isset($templateVars['invoice'])) {
                    $invoice  = $templateVars['invoice'];
                    $templateVars = array_merge($templateVars,$dataHelper->getDataProcess(Type::INVOICE, $invoice->getId()));

                    $content  = $dataHelper->processPDFTemplate(Type::INVOICE, $templateVars, $store->getId());
                    $fileName = 'Invoice' . $invoice->getIncrementId();
                } else if (isset($templateVars['shipment'])) {
                    /* Main Logic Start from here*/    
                    $shipment = $templateVars['shipment'];
                    $templateVars = array_merge($templateVars,$dataHelper->getDataProcess(Type::SHIPMENT, $shipment->getId()));
                    $content  = $dataHelper->processPDFTemplate(Type::SHIPMENT, $templateVars, $store->getId());
                    $fileName = 'Shipment' . $shipment->getIncrementId();

                    $order = $shipment->getOrder();
                    if ($order->hasInvoices()) {
                        foreach ($order->getInvoiceCollection() as $invoice) {
                            $templateVars = array_merge($templateVars,$dataHelper->getDataProcess(Type::INVOICE, $invoice->getId()));
                            $content2 = $dataHelper->processPDFTemplate(Type::INVOICE, $templateVars, $store->getId());
                            $fileName2 = 'Invoice' . $invoice->getIncrementId();
                            if ($content2) {
                                $this->transportBuilder->addAttachment(
                                    $content2,
                                    'application/pdf',
                                    \Zend_Mime::DISPOSITION_ATTACHMENT,
                                    \Zend_Mime::ENCODING_BASE64,
                                    $fileName2 . '.pdf'
                                );
                            }
                        }   
                    }

                } else if (isset($templateVars['creditmemo'])) {
                    $creditmemo = $templateVars['creditmemo'];
                    $content    = $dataHelper->processPDFTemplate(Type::CREDIT_MEMO, $templateVars, $store->getId());
                    $fileName   = 'Creditmemo' . $creditmemo->getIncrementId();
                } else {
                    $order    = $templateVars['order'];
                    $fileName = 'Order' . $order->getIncrementId();
                    $content  = $dataHelper->processPDFTemplate(Type::ORDER, $templateVars, $store->getId());
                }

                if ($content) {
                    $this->transportBuilder->addAttachment(
                        $content,
                        'application/pdf',
                        \Zend_Mime::DISPOSITION_ATTACHMENT,
                        \Zend_Mime::ENCODING_BASE64,
                        $fileName . '.pdf'
                    );
                }

            } catch (\Exception $e) {
                $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/pdfinvoice.log');
                $logger = new \Zend\Log\Logger();
                $logger->addWriter($writer);
                $logger->info($e->getMessage());
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.