Skip to content

Magento 2.4.3 p1 command line Area code is not set

I’ve a custom command line module as follows:

<?php
namespace TestMosIntegrationConsole;

use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
use SymfonyComponentConsoleInputInputArgument;

class FixOrders extends Command
{
    protected $_helper;
    protected $_state;

    public function __construct(
        TestMosIntegrationHelperData $helper,
        MagentoFrameworkAppState $state
    )
    {
        $this->_helper = $helper;
        $this->_state = $state;
        //$this->_state->setAreaCode(MagentoFrameworkAppArea::AREA_FRONTEND);
        parent::__construct();
    }


    protected function configure()
    {
        $this->setName('mosaic:fixorders');
        $this->setDescription('Fix missing orders');
        $this->setDefinition([
            new InputArgument(
                'batch_id',
                InputArgument::REQUIRED,
                __("Batch Id")
            ),
        ]);

        parent::configure();
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->_state->setAreaCode(MagentoFrameworkAppArea::AREA_FRONTEND);

    }
}

i’ve already set “$this->_state->setAreaCode(MagentoFrameworkAppArea::AREA_FRONTEND);”, but when i ran setup:upgrade, the command prompt still show

  [MagentoFrameworkExceptionLocalizedException]
  Area code is not set

it is very strange because i’ve 4 others command in another module it works fine, anyone knows what are the problems?