2

I have two condition :-

  • First if customer is not approve send custom email template.

  • Second if customer is approve send magento default email template.

How can i achieve this.

THANKS.

2 Answers 2

2

In Magento Magento\Sales\Model\Order\Email\Sender from the email template for orde.

If you want customization then you have to work on prepareTemplate() method.

You need to override this class.

Here Code:

<?php

namespace StackExchange\MagentoModule\Model\Order\Email;

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Address\Renderer;
use Magento\Sales\Model\Order\Email\Container\IdentityInterface;
use Magento\Sales\Model\Order\Email\Container\Template;

class Sender extends \Magento\Sales\Model\Order\Email\Sender
{
    /**
     * @var CustomerRepositoryInterface
     */
    private $customerRepository;

    public function __construct(
        Template $templateContainer,
        IdentityInterface $identityContainer,
        CustomerRepositoryInterface $customerRepository,
        \Magento\Sales\Model\Order\Email\SenderBuilderFactory $senderBuilderFactory,
        \Psr\Log\LoggerInterface $logger,
        Renderer $addressRenderer)
    {
        parent::__construct(
            $templateContainer,
            $identityContainer,
            $senderBuilderFactory,
            $logger,
            $addressRenderer);
        $this->customerRepository = $customerRepository;
    }

    /**
     * Populate order email template with customer information.
     *
     * @param Order $order
     * @return void
     */
    protected function prepareTemplate(Order $order)
    {
        $this->templateContainer->setTemplateOptions($this->getTemplateOptions());

        if ($order->getCustomerIsGuest()) {
            $templateId = $this->identityContainer->getGuestTemplateId();
            $customerName = $order->getBillingAddress()->getName();
        } else {

            //  do customization at here
            $isApproved = $this->isCustomerApproved($order->getCustomerId());
            if($isApproved){
                $templateId = 'Your Template id';
            }else{
                $templateId = $this->identityContainer->getTemplateId();
            }

            $customerName = $order->getCustomerName();
        }

        $this->identityContainer->setCustomerName($customerName);
        $this->identityContainer->setCustomerEmail($order->getCustomerEmail());
        $this->templateContainer->setTemplateId($templateId);
    }
    private function isCustomerApproved($customerId)
    {
        try {
            $customer = $this->customerRepository->getById($customerId);
            if($customer->getData('customer_approved')){
                return true;
            }
        } catch (NoSuchEntityException | LocalizedException $e) {
            return false;
        }
        return false;
    }
}
1
1

@Amit Bera is great answer but i have describe more.

Using preference override Model file.Below steps /app/code/Stack/Exchange/etc/di.xml

<?xml version="1.0"?>
<!--
/**
* Dependency injector xml file
*
* @author Mohit Patel
*/
-->
<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\Sender\OrderSender" type="Stack\Exchange\Model\Order\Email\Sender\OrderSender"/>
</config> 

After set file in di.xml.You can create OrderSender file in your model folder like :-

/app/code/Stack/Exchange/Model/Order/Email/Sender/OrderSender.php

<?php

namespace Stack\Exchange\Model\Order\Email\Sender;

use Magento\Payment\Helper\Data as PaymentHelper;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Email\Container\OrderIdentity;
use Magento\Sales\Model\Order\Email\Container\Template;
use Magento\Sales\Model\Order\Email\Sender;
use Magento\Sales\Model\ResourceModel\Order as OrderResource;
use Magento\Sales\Model\Order\Address\Renderer;
use Magento\Framework\Event\ManagerInterface;

class OrderSender extends \Magento\Sales\Model\Order\Email\Sender\OrderSender
{
    /**
     * @var CustomerRepositoryInterface
     */
    private $customerFactory;

    protected $emailTemplate;

    public function __construct(
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Email\Model\BackendTemplate $emailTemplate,
        Template $templateContainer,
        OrderIdentity $identityContainer,
        \Magento\Sales\Model\Order\Email\SenderBuilderFactory $senderBuilderFactory,
        \Psr\Log\LoggerInterface $logger,
        Renderer $addressRenderer,
        PaymentHelper $paymentHelper,
        OrderResource $orderResource,
        \Magento\Framework\App\Config\ScopeConfigInterface $globalConfig,
        ManagerInterface $eventManager)
    {
        // exit("Mohit Patel");
        parent::__construct($templateContainer, $identityContainer, $senderBuilderFactory, $logger, $addressRenderer, $paymentHelper, $orderResource, $globalConfig, $eventManager);
        $this->customerFactory = $customerFactory;
        $this->emailTemplate = $emailTemplate;
    }

    /**
     * Populate order email template with customer information.
     *
     * @param Order $order
     * @return void
     */
    protected function prepareTemplate(Order $order)
    {
        parent::prepareTemplate($order);

        $this->templateContainer->setTemplateOptions($this->getTemplateOptions());

        if ($order->getCustomerIsGuest()) {
            $templateId = $this->identityContainer->getGuestTemplateId();
            $customerName = $order->getBillingAddress()->getName();
        } else {
            $email_template = $this->emailTemplate->load('Customer Approve Pending New Order', 'template_code');
            $customerDisTemplate  =  $email_template->getId();
            // die("Mohit Patel");
            //  do customization at here
            $isApproved = $this->isCustomerApproved($order->getCustomerId());
            if($isApproved){
                $templateId = $this->identityContainer->getTemplateId();
            }else{
                $templateId = $customerDisTemplate;
            }

            $customerName = $order->getCustomerName();
        }

        $this->identityContainer->setCustomerName($customerName);
        $this->identityContainer->setCustomerEmail($order->getCustomerEmail());
        $this->templateContainer->setTemplateId($templateId);
    }
    private function isCustomerApproved($customerId)
    {
        try {
            $customer = $this->customerFactory->create()->load($customerId);
            $isApprove = $customer->getApproveAccount();
            if($isApprove){
                return true;
            }
        } catch (NoSuchEntityException | LocalizedException $e) {
            return false;
        }
        return false;
    }
}

In above code i have add two condition like if customer is not approve send other custom email template otherwise send default magento email template.Hope this help other.

THANKS.

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.