Magento 2: ‘Area code not set’

Magento 2 - Area Code Not Set

Magento 2 Area code not set

Today we are going to talk about why do we get an error “Area code not set”. This error occurs mainly from three sections of your module Cron, Command or Setup Scripts and happens when you are either compiling or running setup upgrade command. In both of the cases, it tries to create an object using the constructor of your classes but couldn’t find the area code and it fails to load.

What does area code do in Magento 2?

Magento uses area codes to define which parts of the application to load given a specific request context. There are seven distinct ‘areas’ of the system, each with their own area code:

  • global
  • doc
  • crontab
  • frontend
  • adminhtml
  • webapi_rest
  • webapi_soap

The area code is set within Magento by the MagentoFrameworkAppState object.

We were getting the similar issue on our Magento 2 Cash Back Extension where we had a requirement to recalculate Cashback discounts for the customers using cron job.

namespace ScommerceCustomConsoleCommand; use MagentoFrameworkConsoleCli;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
use ScommerceCustomCronCustomProcess;
use MagentoFrameworkObjectManagerInterface;
use MagentoFrameworkAppState; /** * Class CustomCommand * @package ScommerceCustomConsoleCommand */
class CustomCommand extends Command
{ /** * @var CustomProcess */ private $customProcess; /** * @var State */ private $state; /** * Object manager * * @var ObjectManagerInterface */ protected $objectManager; /** * ProcessSubscriptionsCommand constructor. * @param State $state * @param ObjectManagerInterface $objectManager */ public function __construct( State $state, ObjectManagerInterface $objectManager ) { $this->objectManager = $objectManager; $this->state = $state; parent::__construct(); } protected function configure() { $this->setName('custom:process')->setDescription('Create custom process'); parent::configure(); } /** * @param InputInterface $input * @param OutputInterface $output * @return int|null * @throws Exception */ protected function execute(InputInterface $input, OutputInterface $output) { try{ $this->state->emulateAreaCode( MagentoFrameworkAppArea::AREA_CRONTAB, [$this, "executeCallBack"], [$input, $output] ); } catch(Exception $e){ } } protected function executeCallBack(InputInterface $input, OutputInterface $output) { $this->customProcess = $this->objectManager->create(CustomProcess::class); $this->customProcess->execute(); return Cli::RETURN_SUCCESS; }
} 

The above code will never give you ‘Area code not set’ error because of the following reasons -:

1) We have a constructor function with only two classes objectManager and state.

2) Also we have emulateAreaCode function in execute function which calls callback function which is same as our main execute function.

3) We are also creating an object of our dependent class using objectManager. If you want to avoid this make sure you do set the area code for your dependent classes as well using emulateAreaCode function of the state class.

Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.