Solved: How to fix Magento 2 Plugin class Vendor\ModuleName\Plugin doesn’t exist?

Solved: How to fix Magento 2 Plugin class Vendor\ModuleName\Plugin doesn’t exist?

Magento is a complex system and it is important to pay close attention to what you code.

One of my clients had a special requirement in which I had to customise the customer login controller to meet the business needs.

One option was to create a custom controller and add a preference in di.xml, and the other option was to write a Magento Plugin. I opted in for the later option.

Everything worked fine on my local and the plugin worked as expected but when it was deployed on the test server, it did not work and returned the following error:

Plugin class Vendor\ModuleName\Plugin doesn't exist

I tried Google, StackOverFlow, Magento community forums and every single website but did not find a solution at all. Eventually, I decided to reiterate the local installation steps to the test server and noticed that on my local I hadn’t used Composer to deploy the package whereas on the test server, the package was deployed using composer.

It was good head start and then I knew the issue is with composer file. After reviewing the file, I found out that everything in the file was fine except the missing PSR-4 element.

Before the fix, my composer was:

 "autoload": {
 "files": [
 "registration.php"
 ]
 }

and after the change:

 "autoload": {
 "psr-4": {
 "Vendor\\ModuleName\\": ""
 },
 "files": [
 "registration.php"
 ]
 }

I saved the file, ran composer and the plugin worked as expected. The error was no longer there.

Summary

  1. Although every problem is different but it is always important to start with simple fixes. Review the directory structure, your di.xml and config files.
  2. Ensure that you follow the same process as you did on your local i.e. Make sure that the deployment process is same as your local.
  3. Check your composer file and ensure that there is no missing information