• tagvibe104@gmail.com
Posted on / Magento2

How to fix the “AREA CODE NOT SET” issue?

Magento 2 Area code not set

As a Magento developer we are facing lot of issue which is common while developing and customizing a module. Today we are going to discuss about How to fix the “Area code not set” issue. This error occurs mainly from four reason CRON, CLI , Setup Scripts and use of session in wrong area and it will pop up 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 IS AREA CODE IN MAGENTO 2?

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

  1. global
  2. doc
  3. crontab
  4. frontend
  5. adminhtml
  6. webapi_rest
  7. webapi_soap

When setting the session for customer or Order we should define the area.

It’s Fixed related to this :

  1. Magento 2.2 area code not set: area code must be set before starting a session.
  2. upgrading data.. area code not set: area code must be set before starting a session.
  3. Magento 2 setup upgrade import failed: area code is not set
  4. import failed: area code not set: area code must be set before starting a session.
  5. Magento 2 area code admin does not exist
  6. processing configurations data from configuration file… import failed: area code is not set
  7. import failed: area code is not set magento2
  8. area code “” does not exist Magento 2

For Example:

We are creating a plugin or overriding a method of core class in custom class and we have injected Magento core session manager and customer session in custom class constructor.

For overriding the existing core class method we are using preference in di.xml and keep in global onside our custom module. when we run complile and upgrade we will get “AREA CODE NOT SET” issue.

Solution:

To resolve this issue we should keep the di.xml in frontend area as we are using customer session in our class. Likewise if you are using Magento Backend session in your custom class you should keep the di.xml in adminhtml area.

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

Leave a Reply

Your email address will not be published. Required fields are marked *