While executing below data patch with setup:upgrade
the following error appears in the console.
Unable to apply data patch Vendor\Module\Setup\Patch\Data\ModuleName for module Vendor_Module. Original exception message: Area code is not set
I don't understand why this error is raised, I don't see why it has to be set for executing this data patch.
<?php
declare (strict_types = 1);
namespace Vendor\Module\Setup\Patch\Data;
use Magento\Catalog\Model\ProductFactory;
use Magento\Catalog\Model\ResourceModel\Product;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
class ModuleName implements DataPatchInterface
{
/** @var \Magento\Catalog\Model\ProductFactory */
protected ProductFactory $productFactory;
/** @var \Magento\Catalog\Model\ResourceModel\Product */
protected Product $productResourceModel;
public function __construct(
ProductFactory $productFactory,
Product $productResourceModel
) {
$this->productFactory = $productFactory;
$this->productResourceModel = $productResourceModel;
}
/**
* @param $productId
*
* @return void
* @throws \Exception
*/
private function setVisibility($productId): void
{
$product = $this->productFactory->create();
$this->productResourceModel->load($product, $productId);
$product->setVisibility(1);
$this->productResourceModel->save($product);
}
/**
* @param $productId
* @param $storeId
*
* @return void
* @throws \Exception
*/
private function setStoreViewVisibility($productId, $storeId): void
{
$product = $this->productFactory->create();
$this->productResourceModel->load($product, $productId);
$product->setStoreId($storeId);
$product->setVisibility(4);
$this->productResourceModel->save($product);
}
/**
* @return void
* @throws \Exception
*/
public function apply(): void
{
$this->setVisibility(247);
$this->setStoreViewVisibility(247, 9);
}
/**
* {@inheritdoc}
*
* @return array|string[]
*/
public static function getDependencies(): array
{
return [];
}
/**
* {@inheritdoc}
*
* @return array|string[]
*/
public function getAliases(): array
{
return [];
}
}