Skip to content

Magento 2.4.5: Abstract class does not see another class?

I’m working on fixing one of the plugins that stopped sending emails.

It uses:

use MagentoFrameworkMailTemplateTransportBuilder

To send email:

$transport = $this->transportBuilder->setTemplateIdentifier((int)$template)
    ->setTemplateOptions(['area' => MagentoFrameworkAppArea::AREA_FRONTEND, 'store' => $storeId])
    ->setTemplateVars($templateVars)
    ->setFromByScope($sender)
    ->addTo($toSender)
    ->getTransport();   

For some strange reason, this class is not visible to the Abstract class that is calling it.

Other classes that are used in the Abstract class work fine.

The error that shows up:

Error: Call to a member function setTemplateIdentifier() on null in /var/www/php/folder/vendor/vendor-folder/VendorPlugin/PriceMatch/Model/SenderAbstract.php:116

I’ve tried using ObjectManager (Yes I know it’s bad):

$objectManager = MagentoFrameworkAppObjectManager::getInstance();

$transportBuilder2 = $objectManager->create(MagentoFrameworkMailTemplateTransportBuilder::class);

And this works fine.

There are other plugins that are using MagentoFrameworkMailTemplateTransportBuilder and all of them are fine.

I assume this is related to the class being Abstract class unless I misspelled something somewhere.

Any ideas?

Magento 2.4.5 notes do not show any issues with TransportBuilder: https://devdocs.magento.com/guides/v2.4/release-notes/backward-incompatible-changes/reference.html

UPDATE:

Here is the whole Abstract class:

<?php

namespace VendorVendorPluginModel;

abstract class SenderAbstract
{
    protected $transportBuilder;
    protected $helperData;
    protected $priceCurrency;
    protected $storeFactory;
    protected $productRepository;

    public function __construct(
        MagentoFrameworkMailTemplateTransportBuilder $transportBuilder,
        MagentoFrameworkPricingPriceCurrencyInterface $priceCurrency,
        VendorVendorPluginHelperData $helperData,
        MagentoCatalogApiProductRepositoryInterface $productRepository,
        MagentoStoreModelStoreFactory $storeFactory
    )
    {
        $this->transportBuilder = $transportBuilder;
        $this->priceCurrency = $priceCurrency;
        $this->helperData = $helperData;
        $this->storeFactory = $storeFactory;
        $this->productRepository = $productRepository;
    }

    public function send($item, $method)
    {
        if( !($this->helperData->isEnabled()) ){
            return;
        }

        $templateVar = $this->getFommatVar($item, $method);
        if(!$templateVar) return;

        $toSender = $templateVar['email'];
        $storeId = $templateVar['store_id'];
        $sender = $this->getFromSender($item);

        $template = $this->getTemplateSender($storeId);
        $templateVarsItem = $templateVar['send_vars'];

        $this->sendEmail($sender, $toSender, $template,  $templateVarsItem, $storeId);
    }

    abstract protected function getFommatVar($item, $method);

    abstract protected function getFromSender($storeId);

    abstract protected function getTemplateSender($storeId);

    protected function sendEmail($sender, $toSender, $template,  $templateVars, $storeId)
    {
        $transport = $this->transportBuilder
            ->setTemplateIdentifier($template)
            ->setTemplateOptions(
                [
                    'area' => MagentoFrameworkAppArea::AREA_FRONTEND,
                    'store' => $storeId,
                ]
            )
            ->setTemplateVars($templateVars)
            ->setFromByScope($sender)
            ->addTo($toSender, $templateVars['customer_name'])
            ->getTransport();

        $transport->sendMessage();
    }

    protected function formatPrice($price, $storeId)
    {
        return $this->priceCurrency->format(
            $price,
            false,
            MagentoFrameworkPricingPriceCurrencyInterface::DEFAULT_PRECISION,
            $storeId
        );
    }
}