Magento 2 Required parameter ‘theme_dir’ was not passed
Today we are going to talk about the following error Required parameter ‘theme_dir’ was not passed which we were getting when we were trying to execute our code from cron job or manually running through CLI command using the following command -:
php bin/magento scommerce:custom:sendcustomemails
Please have a look at our custom command class below which we had in place and was throwing the following exception -:
Required parameter 'theme_dir' was not passed error
/** * @var State */ private $_state; /** * @var CustomClass */ private $_customClass; /** * __construct * * @param CustomClass $customClass * @param State $state */ public function __construct( CustomClass $customClass, State $state ) { parent::__construct(); $this->_customClass = $customClass; $this->_state = $state; } /** * Configure to send custom emails */ protected function configure() { $this->setName("scommerce:custom:sendcustomemails")->setDescription('Send Custom Emails'); parent::configure(); } /** * @param InputInterface $input * @param OutputInterface $output * @return int|null */ protected function execute(InputInterface $input, OutputInterface $output) { $this->_state->emulateAreaCode(Area::AREA_ADMINHTML, [$this->_customClass, 'sendCustomEmailUsingCollection']); return Cli::RETURN_SUCCESS; }
After detail investigation we found that our email template in email_templates.xml file had the following entry
<template id="scommerce_custom_email_to_customers" label="Scommerce Custom Email To Customers" file="scommerce_custom_email_to_customers.html" type="html" module="Scommerce_Custom" area="frontend"/>
Please note the defined area of the email template, it is defined as “frontend” whereas in our above command PHP class, under emulateAreaCode function it was passed as AREA_ADMINHTML so we changed the area code from ADMINHTML to FRONTEND in our command class as shown below
/** * @param InputInterface $input * @param OutputInterface $output * @return int|null */ protected function execute(InputInterface $input, OutputInterface $output) { $this->_state->emulateAreaCode(Area::AREA_FRONTEND, [$this->_customClass, 'sendCustomEmailUsingCollection']); return Cli::RETURN_SUCCESS; }
And Voila! Required parameter ‘theme_dir’ was not passed error just disappeared. The key thing to note here is that we should make sure our Cron Jobs and commands are set with the appropriate area code to run them smoothly. This setting area code article will help you set area code in your command classes correctly.
Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.