0

I'm getting Error: Cannot instantiate interface Magento\Payment\Gateway\Command\CommandManagerInterface when adding in custom Api/Model class constructor

Error is showing for this class interface only rests are working fine.

Magento version 2.4.5

already did:- rm -rf generated bin/magento s:up bin/magento s:di:c bin/magento s:s:d -f

1 Answer 1

1

The error you're encountering typically occurs when you attempt to instantiate an interface directly. Interfaces cannot be instantiated directly because they define a contract for classes to implement, but they do not provide concrete implementations themselves.

To resolve this issue, you should not try to instantiate the interface Magento\Payment\Gateway\Command\CommandManagerInterface directly. Instead, you need to inject an instance of the class that implements this interface into your custom API/Model class constructor.

Here's an example of how you can inject the CommandManagerInterface into your class constructor:

use Magento\Payment\Gateway\Command\CommandManagerInterface;

class YourCustomClass
{
    protected $commandManager;

    public function __construct(
        CommandManagerInterface $commandManager
    ) {
        $this->commandManager = $commandManager;
    }

    // Your class methods...
}

Update your module's di.xml file to provide the implementation of the interface:

<config>
    <preference for="Magento\Payment\Gateway\Command\CommandManagerInterface" 
                type="Magento\Payment\Gateway\Command\CommandManager" />
</config>

After making these changes, Magento's dependency injection system will automatically provide an instance of the Magento\Payment\Gateway\Command\CommandManager class that implements the Magento\Payment\Gateway\Command\CommandManagerInterface interface.

Remember to recompile, clean generated files, and flush the cache after making changes to ensure that the changes take effect.

1
  • Thanks @Charmi Patel Commented May 31, 2023 at 9:50

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.