23

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.

24

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.

| improve this answer | |
7

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.

| improve this answer | |
  • @ 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 – Krishna ijjada Oct 13 '16 at 10:15
  • 1
    Did you add the factory to your use statements? use Magento\Catalog\Api\Data\ProductExtensionInterfaceFactory; – David Lambauer Oct 13 '16 at 11:24
6

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

Be sure to add your interface as a preference (correctly) in the di.xml.

| improve this answer | |
  • 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 May 2 '19 at 17:30
0

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

| improve this answer | |
-2

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.

| improve this answer | |
  • There is no such a file app/etc/config.xml in M2 – medina Jun 3 at 2:41
  • @medina it's config.php, I have updated my answer. – Anshu Mishra Jun 3 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.