1

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 [];
    }
}

3 Answers 3

1

I have face same issue into the installData, so i have use this solution and now it's working fine please try this.

public function __construct(
    ...
    \Magento\Framework\App\State $state
) {
    ...
    $state->setAreaCode('adminhtml'); // else use 'frontend' or 'globle'
}
0

I might be wrong with this, but my guess is that, from the moment you specify a store $product->setStoreId($storeId);, then you are under that store area; which means you need to setup the area code.

2
  • But what area needs to be set then? If you look in \Magento\Framework\App\Area the available options seem a bit odd.. Apr 21, 2022 at 7:59
  • i would say frontend.
    – Claims
    Apr 21, 2022 at 8:31
0

I had same issue on upgradedata. I was solved it with apply admin area code.

protected $_appState;
public function __construct(
        \Magento\Framework\App\State $appState,
        ...
        ...
    ) {
        
        $this->_appState = $appState;
        ...
        ...
    }
     

And I was apply area code to "$this" object

              try {
                    $this->_appState->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);

                    or
                    
                    $this->_appState->setAreaCode('admin');


                }
            } catch (\Exception $e) {
                $this->_appState->getAreaCode();
            }            
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.