Skip to content

Trait Function override not working magento 2

I am using third party plugin Cart2Quote and I am trying to override prepareTemplate function in my custom module Xyz/Plugin.

Here is the file from where I want to override function :

<?php
/**
 * Copyright (c) 2024. Cart2Quote B.V. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Cart2QuoteFeaturesTraitsModelQuoteEmailSender;
trait Sender
{
    /**
     * Application Event Dispatcher
     *
     * @var MagentoFrameworkEventManagerInterface
     */
    /**
     * Global configuration storage.
     *
     * @var MagentoFrameworkAppConfigScopeConfigInterface
     */
    /**
     * @var string
     */
    /**
     * @var string
     */
    /**
     * @var Cart2QuoteQuotationModelQuoteEmailContainerIdentityInterface
     */
    /**
     * @var Cart2QuoteQuotationModelQuoteFactory
     */
    /**
     * @var MagentoUserModelUserFactory
     */
    /**
     * Sender constructor
     *
     * @param MagentoSalesModelOrderEmailContainerTemplate $templateContainer
     * @param MagentoSalesModelOrderEmailContainerContainer $identityContainer
     * @param Cart2QuoteQuotationModelQuoteEmailSenderBuilderFactory $senderBuilderFactory
     * @param PsrLogLoggerInterface $logger
     * @param Cart2QuoteQuotationModelQuoteAddressRenderer $addressRenderer
     * @param MagentoFrameworkEventManagerInterface $eventManager
     * @param MagentoFrameworkAppConfigScopeConfigInterface $globalConfig
     * @param Cart2QuoteQuotationModelQuotePdfQuote $pdfModel
     * @param $sendEmailIdentifier
     * @param $emailSentIdentifier
     */
    /**
     * Get Sender email identifier
     *
     * @return string
     */
    private function getSendEmailIdentifier()
    {
        if(Cart2QuoteLicenseModelLicense::getInstance()->isValid()) {
            return $this->sendEmailIdentifier;
        }
    }
    /**
     * Get email sender identifier
     *
     * @return string
     */
    private function getEmailSentIdentifier()
    {
        if(Cart2QuoteLicenseModelLicense::getInstance()->isValid()) {
            return $this->emailSentIdentifier;
        }
    }
    /**
     * Sends quote request email to the customer.
     * - Email will be sent immediately in two cases:
     * - - if asynchronous email sending is disabled in global settings
     * - - if $forceSyncMode parameter is set to TRUE
     * - Otherwise, email will be sent later during running of
     * - corresponding cron job.
     *
     * @param Cart2QuoteQuotationModelQuote $quote
     * @param bool $forceSyncMode
     * @return bool
     */
    private function send(Cart2QuoteQuotationModelQuote $quote, $forceSyncMode = false)
    {
        if(Cart2QuoteLicenseModelLicense::getInstance()->isValid()) {
            $internalEmail = false;
        $inProcessStatus = $quote->getStatusLabel()->getText() == "Open, In Process";
        if ($this->globalConfig->getValue('quotation_email/quote_notice/enabled')) {
            if ($inProcessStatus && !$quote->getNoticeEmailSent() == 1) {
                $internalEmail = true;
            }
        }
        $quote->setData($this->sendEmailIdentifier, true);
        $quote->setData($this->emailSentIdentifier, null);
        if (!$this->globalConfig->getValue('sales_email/general/async_sending') || $forceSyncMode) {
            if ($this->checkAndSend($quote, null, $internalEmail)) {
                $quote->setData($this->emailSentIdentifier, true);
                $quote->save();
                return true;
            } else {
                $quote->save();
                //return false when check and send returns false
                return false;
            }
        }
        $quote->save();
        //always return true on async sends
        return true;
        }
    }
    /**
     * Prepare email template with variables
     *
     * @param Cart2QuoteQuotationModelQuote $quote
     * @param mixed $internalEmail
     * @return void
     */
    private function prepareTemplate(Cart2QuoteQuotationModelQuote $quote, $internalEmail = null)
    {
        if(Cart2QuoteLicenseModelLicense::getInstance()->isValid()) {
            $quoteData = $this->quoteFactory->create()->load($quote->getId())->getData();
        $salesRep = $this->userFactory->create()->load($quoteData['user_id']);
        $transport = [
            'quote' => $quote,
            'billing' => $quote->getBillingAddress(),
            'payment_html' => $this->getPaymentHtml($quote),
            'store' => $quote->getStore(),
            'formattedShippingAddress' => $this->getFormattedShippingAddress($quote),
            'formattedBillingAddress' => $this->getFormattedBillingAddress($quote),
            'attach_pdf' => $quote->getAttachPdf(),
            'attach_doc' => $quote->getAttachDoc(),
            'quote_data' => [
                'quote_id' => (string)$quote->getQuoteId(),
                'customer_name' => (string)$quote->getCustomerName(),
                'billing_name' => (string)$quote->getBillingAddress()->getFirstname() . ' ' .
                    $quote->getBillingAddress()->getLastName(),
                'expiry_date_string' => (string)$quote->getExpiryDateString(),
                'shipping_method' => (string)$quote->getShippingMethod(),
                'customer_note' => (string)$quote->getCustomerNote(),
                'email_customer_note' => (string)$quote->getEmailCustomerNote(),
                'shipping_description' => (string)$quote->getShippingDescription(),
                'quotation_created_at' => (string)$quote->getQuotationCreatedAt(),
                'created_at_formatted' => (string)$quote->getCreatedAtFormatted(1),
                'expiry_date_formatted' => (string)$quote->getExpiryDateFormatted(1),
                'receiver_name' => (string)$quote->getEmailReceiverAttributes($quote->getUserId(), true),
                'salesrep_name' => (string)$salesRep->getName(),
            ],
            'receiver_name' => $this->identityContainer->getRecieverName(),
        ];
        //legacy support for older M2 version like 2.2.5
        foreach ($transport['quote_data'] as $key => $value) {
            $transport['quote_data' . '_' . $key] = $value;
        }
        if (!$quote->getCustomerIsGuest()) {
            $transport['customer_has_account'] = true;
        }
        $this->eventManager->dispatch(
            'email_quote_set_template_vars_before',
            ['sender' => $this, 'transport' => $transport]
        );
        $this->templateContainer->setTemplateVars($transport);
        parent::prepareTemplate($quote, $internalEmail);
        }
    }
    /**
     * Get payment info block as html
     *
     * @param Cart2QuoteQuotationModelQuote $quote
     * @return string
     */
    private function getPaymentHtml(Cart2QuoteQuotationModelQuote $quote)
    {
        if(Cart2QuoteLicenseModelLicense::getInstance()->isValid()) {
            return '';
        }
    }
}

This is the main Model file :

<?php
/**
 * Copyright (c) 2024. Cart2Quote B.V. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Cart2QuoteQuotationModelQuoteEmailSender;

class Sender extends Cart2QuoteQuotationModelQuoteEmailAbstractSender implements QuoteSenderInterface
{
    use Cart2QuoteFeaturesTraitsModelQuoteEmailSenderSender {
        getSendEmailIdentifier as private traitGetSendEmailIdentifier;
        getEmailSentIdentifier as private traitGetEmailSentIdentifier;
        send as private traitSend;
        prepareTemplate as private traitPrepareTemplate;
        getPaymentHtml as private traitGetPaymentHtml;
    }

    /**
     * Application Event Dispatcher
     *
     * @var MagentoFrameworkEventManagerInterface
     */
    protected $eventManager;

    /**
     * Global configuration storage.
     *
     * @var MagentoFrameworkAppConfigScopeConfigInterface
     */
    protected $globalConfig;

    /**
     * @var string
     */
    protected $sendEmailIdentifier;

    /**
     * @var string
     */
    protected $emailSentIdentifier;

    /**
     * @var Cart2QuoteQuotationModelQuoteEmailContainerIdentityInterface
     */
    protected $identityContainer;

    /**
     * @var Cart2QuoteQuotationModelQuoteFactory
     */
    protected $quoteFactory;

    /**
     * @var MagentoUserModelUserFactory
     */
    public $userFactory;


    /**
     * Sender constructor
     *
     * @param MagentoSalesModelOrderEmailContainerTemplate $templateContainer
     * @param MagentoSalesModelOrderEmailContainerContainer $identityContainer
     * @param Cart2QuoteQuotationModelQuoteEmailSenderBuilderFactory $senderBuilderFactory
     * @param PsrLogLoggerInterface $logger
     * @param Cart2QuoteQuotationModelQuoteAddressRenderer $addressRenderer
     * @param MagentoFrameworkEventManagerInterface $eventManager
     * @param MagentoFrameworkAppConfigScopeConfigInterface $globalConfig
     * @param Cart2QuoteQuotationModelQuotePdfQuote $pdfModel
     * @param $sendEmailIdentifier
     * @param $emailSentIdentifier
     */
    public function __construct(
        MagentoSalesModelOrderEmailContainerTemplate $templateContainer,
        MagentoSalesModelOrderEmailContainerContainer $identityContainer,
        Cart2QuoteQuotationModelQuoteEmailSenderBuilderFactory $senderBuilderFactory,
        PsrLogLoggerInterface $logger,
        Cart2QuoteQuotationModelQuoteAddressRenderer $addressRenderer,
        MagentoFrameworkEventManagerInterface $eventManager,
        MagentoFrameworkAppConfigScopeConfigInterface $globalConfig,
        Cart2QuoteQuotationModelQuotePdfQuote $pdfModel,
        Cart2QuoteQuotationModelQuoteFactory $quoteFactory,
        MagentoUserModelUserFactory $userFactory,
        $sendEmailIdentifier = '',
        $emailSentIdentifier = ''
    ) {
        parent::__construct(
            $templateContainer,
            $identityContainer,
            $senderBuilderFactory,
            $logger,
            $addressRenderer,
            $pdfModel
        );
        $this->eventManager = $eventManager;
        $this->globalConfig = $globalConfig;
        $this->quoteFactory = $quoteFactory;
        $this->userFactory = $userFactory;
        $this->sendEmailIdentifier = $sendEmailIdentifier;
        $this->emailSentIdentifier = $emailSentIdentifier;
        $this->identityContainer = $identityContainer;
    }

    /**
     * Get Sender email identifier
     *
     * @return string
     */
    public function getSendEmailIdentifier()
    {
        return $this->traitGetSendEmailIdentifier();
    }

    /**
     * Get email sender identifier
     *
     * @return string
     */
    public function getEmailSentIdentifier()
    {
        return $this->traitGetEmailSentIdentifier();
    }

    /**
     * Sends quote request email to the customer.
     * - Email will be sent immediately in two cases:
     * - - if asynchronous email sending is disabled in global settings
     * - - if $forceSyncMode parameter is set to TRUE
     * - Otherwise, email will be sent later during running of
     * - corresponding cron job.
     *
     * @param Cart2QuoteQuotationModelQuote $quote
     * @param bool $forceSyncMode
     * @return bool
     */
    public function send(Cart2QuoteQuotationModelQuote $quote, $forceSyncMode = false)
    {
        return $this->traitSend($quote, $forceSyncMode);
    }

    /**
     * Prepare email template with variables
     *
     * @param Cart2QuoteQuotationModelQuote $quote
     * @param mixed $internalEmail
     * @return void
     */
    protected function prepareTemplate(Cart2QuoteQuotationModelQuote $quote, $internalEmail = null)
    {
        $this->traitPrepareTemplate($quote, $internalEmail);
    }

    /**
     * Get payment info block as html
     *
     * @param Cart2QuoteQuotationModelQuote $quote
     * @return string
     */
    protected function getPaymentHtml(Cart2QuoteQuotationModelQuote $quote)
    {
        return $this->traitGetPaymentHtml($quote);
    }
}

I want to override this in my custom plugin so I have tried to use plugin method as well as I tried to override via preference method in di.xml but it’s not working.

I want to change this line’s logic :

$salesRep = $this->userFactory->create()->load($quoteData['user_id']);

Here I am getting user_id undefined so I want to set condition if user_id is undefined then I want to set one specific number there.

Anyone know how to override this properly?

I am using latest magento version, php 8.2.