33

As the part of creating bundle product programmatically in Magento 2. I am getting below error. Even i removed var/cache and generation.

This may be a duplicate question but none of the answer is working for this issue.

Fatal error: Cannot instantiate interface Magento\Catalog\Api\Data\ProductExtensionInterface in \ObjectManager\Factory\Dynamic\Developer.php on line 73

Used below code as the reference of link

$productExtension = $objectManager->create(\Magento\Catalog\Api\Data\ProductExtensionInterface::class);
$option = $objectManager->create(\Magento\Bundle\Api\Data\OptionInterface::class);
$option->setTitle('Size');
$option->setType('radio');
$option->setRequired(true);
$option->setPosition(1);
//$option->setProductLinks($links);
$productExtension->setBundleOptions([$option]);
$_product->setExtensionAttributes($productExtension);

I am using object manager for temporary/quick purpose, will convert the object manager to constructor injunction.

Any one can help me on this issue.

7 Answers 7

40

I think you have not mentioned the preference in your di.xml. I also faced the same issue when I was creating custom config xml file module.

I got the issue when I called this code:

$testConfig = $this->_objectManager->get('Training\Test\Model\Config\ConfigInterface');
$myNodeInfo = $testConfig->getMyNodeInfo();

Below is the sample code I have added in my di.xml, may be it may help you to get reference:

<preference for="Training\Test\Model\Config\ConfigInterface" type="Training\Test\Model\Config" />

Here I have implemented the interface in Training\Test\Model\Config class and my getMyNodeInfo() method is defined in that Model.

So based on the the preference defined in di.xml the code ->get('Training\Test\Model\Config\ConfigInterface'); will create an object of type Training\Test\Model\Config.

1
  • its worked for me, Dont user _objectManager
    – matinict
    Commented Jan 10, 2021 at 9:42
19

I struggled with this and then found a solution that worked for me in the documentation.

Be sure to add your interface as a preference (correctly) in the di.xml, and then run php bin/magento setup:di:compile.

2
  • 2
    My problem was that I placed the di.xml in the etc/frontend folder instead of the etc root by accident, but your answer led me to find that.
    – KyleBunga
    Commented May 2, 2019 at 17:30
  • ah thank you man.
    – Kevinzie
    Commented May 13 at 14:17
10

You should not use the ObjectManager.

Collect your dependencies via _constructor.

In case your interface still won't be loaded, check your di.xml and validate that a preference is available.

2
  • @ David Lambauer Even i used constructor i am getting Cannot instantiate interface Magento\Catalog\Api\Data\ProductExtensionInterface in magento\framework\ObjectManager\Factory\Dynamic\Developer.php on line 73 Commented Oct 13, 2016 at 10:15
  • 1
    Did you add the factory to your use statements? use Magento\Catalog\Api\Data\ProductExtensionInterfaceFactory; Commented Oct 13, 2016 at 11:24
5

I tried this mentioned in another thread but it didn't solve:

redis-cli flushall

Finally, got solved by running setup:upgrade:

bin/magento setup:upgrade

# also run, if in production mode
bin/magento setup:static-content:deploy
bin/magento setup:di:compile
1
  • 1
    I had the same problem locally by going to an old project version, the problem was related to the redis cache, I spent time on it because it's one of the things that we don't necessarily think about; those who use "warden" stack, the command to flush redis locally is: "warden redis flushall".it can help other people
    – PЯINCƎ
    Commented Jan 18 at 15:52
2

Please make sure module is enabled. You can verify using {{MagentoRoot}}/app/etc/config.php file. In my case module was disabled.

0

I have faced the similar issue i.e. cannot instantiate interface for some third party module.
The reason that I found out after debugging that the corresponding module's entry was missing from the app/etc/config.php but the module's code was present in the codebase.
After manually adding the module's entry in the app/etc/config.php file, the issue got resolved.

3
  • There is no such a file app/etc/config.xml in M2
    – medina
    Commented Jun 3, 2020 at 2:41
  • @medina it's config.php, I have updated my answer. Commented Jun 3, 2020 at 4:16
  • should be added using bin/magento set:up command
    – jim smith
    Commented Nov 15, 2023 at 17:46
0

If anyone meets this bug, you should check if you declare an inherence from the interface to the implemented class in di.xml with preference tag. Then run bin/magento setup:upgrade to remove var/generation and cache. For production, you also need to run:

bin/magento setup:static-content:deploy
bin/magento setup:di:compile

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.