On Magento 2.0 I had a console command to delete products under some circumnstances.
After upgrading to 2.1 it doesn't work anymore. It throws the infamous Area code not set
error.
The strangeness however is that I set the area code in my script.
If I put a breakpoint in State::getAreaCode I see my areacode set until the FlushCacheByTags::aroundDelete plugin is called. There suddenly my State::_areaCode property is null and the exception is thrown.
Does someone encountered a similar problem?
Here there are relevant parts of code involved:
Console command constructor:
public function __construct(
State $state, //This is Magento\Framework\App\State
ArrayInputFactory $arrayInputFactory,
PruneFactory $catalogPrune,
Registry $registry
) { ... }
at the beginning of command execute
I set the area code in this way:
$this->state->setAreaCode(Area::AREA_ADMINHTML);
then I delete products in this way:
$product->getResource()->delete($product);
I tried also
$this->productRepository->deleteById($product->getSku());
but it is the same since the repository ultimately calls the ResourceModel delete as above. I need my command to be fast, so I avoid to use repositories and go for the low level approach.