Skip to content

Is it possible to override type configuration in the di.xml of a module in Magento2?

I want to override the file “FoomanEmailAttachmentsObserverAbstractSendInvoiceObserver”.

Here is the code in the di.xml.

<!-- module introduced preferences -->
<preference for="FoomanEmailAttachmentsModelApiAttachmentContainerInterface"
            type="FoomanEmailAttachmentsModelAttachmentContainer"/>

<type name="FoomanEmailAttachmentsObserverAbstractSendOrderObserver">
    <arguments>
        <argument name="pdfRenderer" xsi:type="object">FoomanEmailAttachmentsModelNoneRenderer</argument>
    </arguments>
</type>


<virtualType name="fooman_emailattachments_invoice_pdf_renderer" type="FoomanEmailAttachmentsModelPdfRenderer">
    <arguments>
        <argument name="pdfRenderer" xsi:type="object">MagentoSalesModelOrderPdfInvoice</argument>
    </arguments>
</virtualType>
<type name="FoomanEmailAttachmentsObserverAbstractSendInvoiceObserver">
    <arguments>
        <argument name="pdfRenderer" xsi:type="object">fooman_emailattachments_invoice_pdf_renderer</argument>
    </arguments>
</type>

<virtualType name="fooman_emailattachments_shipment_pdf_renderer" type="FoomanEmailAttachmentsModelPdfRenderer">
    <arguments>
        <argument name="pdfRenderer" xsi:type="object">MagentoSalesModelOrderPdfShipment</argument>
    </arguments>
</virtualType>
<type name="FoomanEmailAttachmentsObserverAbstractSendShipmentObserver">
    <arguments>
        <argument name="pdfRenderer" xsi:type="object">fooman_emailattachments_shipment_pdf_renderer</argument>
    </arguments>
</type>

<type name="FoomanEmailAttachmentsObserverAbstractSendInvoiceShipmentObserver">
    <arguments>
        <argument name="pdfRenderer" xsi:type="object">fooman_emailattachments_invoice_pdf_renderer</argument>
    </arguments>
</type>

<virtualType name="fooman_emailattachments_creditmemo_pdf_renderer" type="FoomanEmailAttachmentsModelPdfRenderer">
    <arguments>
        <argument name="pdfRenderer" xsi:type="object">MagentoSalesModelOrderPdfCreditmemo</argument>
    </arguments>
</virtualType>
<type name="FoomanEmailAttachmentsObserverAbstractSendCreditmemoObserver">
    <arguments>
        <argument name="pdfRenderer" xsi:type="object">fooman_emailattachments_creditmemo_pdf_renderer</argument>
    </arguments>
</type>

Php file

<?php
declare(strict_types=1);

namespace FoomanEmailAttachmentsObserver;

    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(MagentoFrameworkEventObserver $observer)
    {
        /**
         * @var MagentoSalesApiDataInvoiceInterface $invoice
         */
        $invoice = $observer->getInvoice();
        if ($this->pdfRenderer->canRender()
            && $this->scopeConfig->getValue(
                static::XML_PATH_ATTACH_PDF,
                MagentoStoreModelScopeInterface::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,
            MagentoStoreModelScopeInterface::SCOPE_STORE,
            $invoice->getStoreId()
        )
        ) {
            $this->attachTermsAndConditions($invoice->getStoreId(), $observer->getAttachmentContainer());
        }
    }
}

How can I override this in my module?