X
    Categories: Knowledgebase

How to Solve error (Uncaught Error: Cannot instantiate interface CopyConstructorInterface)

Creating an upgrade script for cloning a product with MagentoCatalogModelProductCopier has a bug.

public function __construct(
    MagentoCatalogModelProductCopier $productCopier,
    MagentoFrameworkAppState $appState
) {
    $this->productCopier = $productCopier;
    try {
        $appState->setAreaCode('frontend');
    } catch( MagentoFrameworkExceptionLocalizedException $e ) {
        // intentionally left empty
    }
}

Then have error when use product Copier function :

PHP Fatal error: Uncaught Error: Cannot instantiate interface MagentoCatalogModelProductCopyConstructorInterface in /var/www/m210/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:73 
Stack trace: 
#0 /var/www/m210/vendor/magento/framework/ObjectManager/ObjectManager.php(71): MagentoFrameworkObjectManagerFactoryDynamicDeveloper->create('MagentoCatalog...') 
#1 /var/www/m210/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(126): MagentoFrameworkObjectManagerObjectManager->get('MagentoCatalog...') 
#2 /var/www/m210/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(53): MagentoFrameworkObjectManagerFactoryAbstractFactory->resolveArgument(Array, 'MagentoCatalog...', NULL, 'copyConstructor', 'MagentoCatalog...') 
#3 /var/www/m210/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(82): MagentoFrameworkObjectManagerFactoryDynamicDeveloper->_resolveArguments('MagentoCatalog...', Array, Array) 
#4 /var/www/m210/vendor/magento/frame in /var/www/m210/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php on line 73

Simple try the our solution below :

Create di.xml file in frontend folder with the content below :

<?xml version="1.0"?> 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> 
    <type name="MagentoCatalogModelProductCopyConstructorComposite"> 
        <arguments> 
            <argument name="constructors" xsi:type="array"> 
                <item name="crossSell" xsi:type="string">MagentoCatalogModelProductCopyConstructorCrossSell</item> 
                <item name="upSell" xsi:type="string">MagentoCatalogModelProductCopyConstructorUpSell</item> 
                <item name="related" xsi:type="string">MagentoCatalogModelProductCopyConstructorRelated</item> 
                <item name="catalog_inventory" xsi:type="string">MagentoCatalogInventoryModelProductCopyConstructorCatalogInventory</item> 
                <item name="downloadable" xsi:type="string">MagentoDownloadableModelProductCopyConstructorDownloadable</item> 
            </argument>
        </arguments> 
    </type> 
    <type name="MagentoCatalogModelProductCopier"> 
        <arguments> 
            <argument name="copyConstructor" xsi:type="object">MagentoCatalogModelProductCopyConstructorComposite</argument> 
        </arguments> 
    </type> 
</config>

See image :

This solution also applied for magento 2 issue 7056 : https://github.com/magento/magento2/issues/7056

Thank for reading this post, Hope it helps.

Kien Wiliam: Magento Ecommerce Developer