0

I didn't found an event when invoice email is sent, so i tried to override Magento\Sales\Model\Order\Email\SenderBuilder class, the override is working but getting the customer attribute invoice_email is not working, it works on my localhost by in the development website (which is multistore), it didn't work. I don't know if there's a better solution to use an observer if there's a sending invoice email event

so in my module

Vendor/ModuleName/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Sales\Model\Order\Email\SenderBuilder" type="Vendor\ModuleName\Model\Sales\Order\Email\SenderBuilder" />
</config>

In app/code/Vendor/ModuleName/Model/Sales/Order/Email/SenderBuilder.php

namespace Wetag\InvoiceEmail\Model\Sales\Order\Email;

class SenderBuilder extends \Magento\Sales\Model\Order\Email\SenderBuilder    
{



public function send()
{


    $this->configureEmailTemplate();

    $this->transportBuilder->addTo(
        $this->identityContainer->getCustomerEmail(),
        $this->identityContainer->getCustomerName()
    );

    $copyTo = $this->identityContainer->getEmailCopyTo();

    if (!empty($copyTo) && $this->identityContainer->getCopyMethod() == 'bcc') {
        foreach ($copyTo as $email) {
            $this->transportBuilder->addBcc($email);
        }
    }

    if($this->identityContainer  instanceof \Magento\Sales\Model\Order\Email\Container\InvoiceIdentity) {
    //do stuff

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $CustomerModel = $objectManager->create('Magento\Customer\Model\Customer');

        $CustomerModel->setWebsiteId($this->identityContainer->getStore()->getStoreId()); 
        $CustomerModel->loadByEmail($this->identityContainer->getCustomerEmail());
        $email = $CustomerModel->getData()['invoice_email'];


        if(!empty($email)){

            $this->transportBuilder->addBcc($email);
        }

    }
    $transport = $this->transportBuilder->getTransport();
    $transport->sendMessage();
}
}
0

This was an issue with the store id, it shouldn't to be get from the identityContainer, this is my solution

        $url = \Magento\Framework\App\ObjectManager::getInstance();
        $storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface');

        $websiteId = $storeManager->getWebsite()->getWebsiteId();
        // Get Store ID
        //$store = $storeManager->getStore();
        //$storeId = $store->getStoreId();

        $CustomerModel->setWebsiteId($websiteId); 
        $CustomerModel->loadByEmail($this->identityContainer->getCustomerEmail());
        $email = $CustomerModel->getData()['invoice_email'];
  • Why you use object manager, like Magento\Framework\App\ObjectManager::getInstance()? Don't do this. – Sohel Rana Apr 26 at 19:30
  • @SohelRana how to do? i'm Magento beginner – tarek fellah Apr 26 at 22:53

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.