3rd Party Integration

Our extension can be integrated with other 3rd party extensions. It has already prepared hooks which lets you add new attachment types for example: Order PDF.

Whole integration is only from two parts:
- We need to create observer and register new attachment type
- Create new class which returns file name and file content.

1. Register New Attachment Type

The first thing to do is to create an observer and register new attachment type:

1. Event file: app/code/Magetrend/HelloWorld/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

    <event name="mt_ea_register">
        <observer name="mt_helloworld_register_email_attachment"
                  instance="Magetrend\HelloWorld\Observer\OrderPdf"/>
    </event>

</config>

Event name on which it has to be hooked: mt_ea_register

2. Observer class file: app/code/Magetrend/HelloWorld/Observer/OrderPdf.php

<?php
namespace Magetrend\HelloWorld\Observer;

use Magento\Framework\Event\Observer;

class OrderPdf implements \Magento\Framework\Event\ObserverInterface
{
    public $dataObjectFactory;

    public function __construct(
        \Magento\Framework\DataObjectFactory $dataObjectFactory
    ) {
        $this->dataObjectFactory = $dataObjectFactory;
    }

    public function execute(Observer $observer)
    {
        $observer->getCollection()
            ->addItem($this->dataObjectFactory->create()->setData(
                [
                    'code' => 'order_pdf',
                    'label' => 'Order PDF',
                    'adapter_class' => \Magetrend\HelloWorld\Model\OrderPdf::class
                ]
            ));
    }
}

3. Adapter class file:

Adapter class must has two methods: getFileName and getFileContent
Adapter class file: app/code/Magetrend/HelloWorld/Model/OrderPdf.php

<?php
namespace Magetrend\HelloWorld\Model;

class OrderPdf extends \Magento\Framework\DataObject
{
    public function getFileName()
    {
        $attachment = $this->getAttachment();
        $fileNameTemplate = $attachment->getFileName();
        return $fileNameTemplate;
    }

    public function getFileContent()
    {
        //Do your logic here to get order PDF as string

        return $orderPDFContent;
    }
}

Use the following methods to get data:

To get attachment information like name, file name use:
$this->getAttachment(); (returns object of instance:Magetrend\EmailAttachment\Model\Attachment class)

To get all email template variables like $order, $invoice, $store use: $this->getTemplateVars(); (returns array with all email variables). For example to get the order:

$templateVars = $this->getTemplateVars();
if (!isset($templateVars['order'])) {
    return '';
}
$order = $templateVars['order'];

After when these things are done, run magento upgrade command and new attachment type will be added.