Upgrading a Magento 2 core is simple. All it takes is writing a new version in the composer.json file and executing a command composer update from the console, which updates the core. Then, you must apply the updates using the bin/magento setup: upgrade command.

Nevertheless, you may encounter some issues after upgrading the core, and you will learn about those issues in this article. Let’s take a look at how to solve them and perform the upgrade Magento 2.1 to 2.2.

How to Upgrade Magento Version Without Problems

Below is a list of common issues which arise during switching from Magento 2.1 to Magento 2.2 Community Edition. Let’s fix them out!

‘Area Code is Not Set’ Error in Magento 2

This error is thrown when running bin/magento setup: upgrade after upgrading the core. After this error, subsequent modules cannot be upgraded, and the site enters maintenance mode. This error has been observed when upgrading the Magento_Theme module.

Reason: When upgrading the Magento_Theme module, Magento re-registers all existing themes. This calls the getAreaCode() method of the Magento\Framework\App\State class, which returns this error. This method returns an error because when executing module upgrades using Magento 2 setup upgrade console command, area codes (‘adminhtml’ or ‘frontend’) are not specified.

Solution: Implement a plugin in Magento\Theme\Setup\UpgradeData in a custom module. The plugin will look as follows:

class Theme {

    public function __construct(
        \Magento\Framework\App\State $state
    ) {
        try{
	$code = $state->getAreaCode();
       }catch(\Exception $e){
	$state->setAreaCode('adminhtml'); 
      }
    }

    public function beforeUpgrade($model,$setup,$context){
        return array($setup,$context);
    }
}

This is a shortcoming of Magento.

‘Code Already Exists’ Error in Magento 2

This error is related to the Magento_Tax module. Magento in and of itself is not at fault here. The issue is that the upgrade script re-saves all tax rates.

Reason: The error occurs when the “code” field in a re-saved record matches a field in another record. In other words, the condition of uniqueness is violated. This is possible if the rates were imported, for example, from Magento 1, during migration.

Solution: Go through the tax_calculation_rate and delete duplicates. Afterward, Magento upgrade script can rerun, and the problem will be solved.

After updating, you might find that some third-party extensions have stopped working. Below, we will examine potential problems that arise in the process of Magento 2 update.

Magento 2 Modules Not Working

1. The getMessage() method was added to the interface Magento\Framework\Mail\TransposrtInterface.

Reason: Consequently, if some class in the module implements this interface and doesn’t have this method, it will return an error. This will happen if, for example, you use the Amasty_Smtp module in a project.

Solution: Upgrade the module to a version compatible with Magento 2.2.

2. You will also have to upgrade all modules in which classes are defined that implement Magento\Framework\Api\Search\SearchResultInterface (collections for UI Grids).

Reason: In version 2.2 mandatory parameters were added that are passed to the constructor of such classes. This outcome was observed in some modules from MageWorx.

Solution: You can correct this issue by updating the modules to the latest version.

These are all the pitfalls we have encountered thus far, and they are all solvable. Now you know how to upgrade Magento Community Edition. If you need any help to perform this or to add some additional functionality, drop us a line and we will help you with Magento 2 Development.