0

I want to override the file "Fooman\EmailAttachments\Observer\AbstractSendInvoiceObserver".

Here is the code in the di.xml.

<!-- module introduced preferences -->
<preference for="Fooman\EmailAttachments\Model\Api\AttachmentContainerInterface"
            type="Fooman\EmailAttachments\Model\AttachmentContainer"/>

<type name="\Fooman\EmailAttachments\Observer\AbstractSendOrderObserver">
    <arguments>
        <argument name="pdfRenderer" xsi:type="object">\Fooman\EmailAttachments\Model\NoneRenderer</argument>
    </arguments>
</type>


<virtualType name="fooman_emailattachments_invoice_pdf_renderer" type="\Fooman\EmailAttachments\Model\PdfRenderer">
    <arguments>
        <argument name="pdfRenderer" xsi:type="object">\Magento\Sales\Model\Order\Pdf\Invoice</argument>
    </arguments>
</virtualType>
<type name="\Fooman\EmailAttachments\Observer\AbstractSendInvoiceObserver">
    <arguments>
        <argument name="pdfRenderer" xsi:type="object">fooman_emailattachments_invoice_pdf_renderer</argument>
    </arguments>
</type>

<virtualType name="fooman_emailattachments_shipment_pdf_renderer" type="\Fooman\EmailAttachments\Model\PdfRenderer">
    <arguments>
        <argument name="pdfRenderer" xsi:type="object">\Magento\Sales\Model\Order\Pdf\Shipment</argument>
    </arguments>
</virtualType>
<type name="\Fooman\EmailAttachments\Observer\AbstractSendShipmentObserver">
    <arguments>
        <argument name="pdfRenderer" xsi:type="object">fooman_emailattachments_shipment_pdf_renderer</argument>
    </arguments>
</type>

<type name="\Fooman\EmailAttachments\Observer\AbstractSendInvoiceShipmentObserver">
    <arguments>
        <argument name="pdfRenderer" xsi:type="object">fooman_emailattachments_invoice_pdf_renderer</argument>
    </arguments>
</type>

<virtualType name="fooman_emailattachments_creditmemo_pdf_renderer" type="\Fooman\EmailAttachments\Model\PdfRenderer">
    <arguments>
        <argument name="pdfRenderer" xsi:type="object">\Magento\Sales\Model\Order\Pdf\Creditmemo</argument>
    </arguments>
</virtualType>
<type name="\Fooman\EmailAttachments\Observer\AbstractSendCreditmemoObserver">
    <arguments>
        <argument name="pdfRenderer" xsi:type="object">fooman_emailattachments_creditmemo_pdf_renderer</argument>
    </arguments>
</type>

Php file

<?php
declare(strict_types=1);

namespace Fooman\EmailAttachments\Observer;

    class AbstractSendInvoiceObserver extends AbstractObserver
{
    const XML_PATH_ATTACH_PDF = 'sales_email/invoice/attachpdf';
    const XML_PATH_ATTACH_AGREEMENT = 'sales_email/invoice/attachagreement';

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        /**
         * @var \Magento\Sales\Api\Data\InvoiceInterface $invoice
         */
        $invoice = $observer->getInvoice();
        if ($this->pdfRenderer->canRender()
            && $this->scopeConfig->getValue(
                static::XML_PATH_ATTACH_PDF,
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
                $invoice->getStoreId()
            )
        ) {
            $this->contentAttacher->addPdf(
                $this->pdfRenderer->getPdfAsString([$invoice]),
                $this->pdfRenderer->getFileName(__('Invoice') . $invoice->getIncrementId()),
                $observer->getAttachmentContainer()
            );
        }

        if ($this->scopeConfig->getValue(
            static::XML_PATH_ATTACH_AGREEMENT,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            $invoice->getStoreId()
        )
        ) {
            $this->attachTermsAndConditions($invoice->getStoreId(), $observer->getAttachmentContainer());
        }
    }
}

How can I override this in my module?

9
  • Please share the entire di.xml file so we can understand where the object is coming from.
    – Msquare
    Feb 1 at 7:22
  • @Msquare updated the di.xml Feb 1 at 7:25
  • Can you please inform me which pdfRenderer you need to override?
    – Msquare
    Feb 1 at 7:27
  • you can directly overite using preferences magefan.com/blog/magento-2-preferences
    – Msquare
    Feb 1 at 7:30
  • @Msquare I need to override this file Fooman\EmailAttachments\Observer\AbstractSendInvoiceObserver Feb 1 at 7:30

1 Answer 1

1

You have a few options:

  1. completely replace Observer
  2. add a plugin on the Observer's execute method

As the Observer class only has the single execute method the first option I think is easier to read.

To implement option 1 make sure to create a separate module that has a sequence that sorts it after Fooman_EmailAttachments. Then in your etc/events.xml include

    <!--Invoice Emails-->
    <event name="fooman_emailattachments_before_send_invoice">
        <observer name="fooman_emailattachments_before_send_invoice_observer"
                  instance="Custom\FoomanEmailAttachments\Observer\BeforeSendInvoiceObserver"/>
    </event>
    <event name="fooman_emailattachments_before_send_invoice_comment">
        <observer name="fooman_emailattachments_before_send_invoice_comment_observer"
                  instance="Custom\FoomanEmailAttachments\Observer\BeforeSendInvoiceCommentObserver"/>
    </event>

the above assumes your module is called Custom_FoomanEmailAttachments. You can then provide your own implementation for the observer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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