1

This is my first question, so please help! ;) As you can see in commented lince i tried everything what i could found on the web. My code is placed in /app/code/[Vendor]/[Module]/Cron/GroupProducts.php and look like this:

public function __construct(
         \Psr\Log\LoggerInterface $logger
        ,\Magento\Catalog\Model\ResourceModel\ProductFactory $productFactory
        ,\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
        ,\Magento\Catalog\Model\ProductLink\LinkFactory $productLinkFactory
        ,\Magento\Catalog\Model\Product $product
        ,\Magento\Catalog\Model\ProductRepository $productRepository
    //  ,CopyConstructorInterface $copyConstructor
    //  ,\Magento\Catalog\Model\Product\Copier $productCopier
    //  ,\Magento\Catalog\Model\Product\Copier $productCopier
)
{
    $this->logger = $logger;
    $this->_productFactory=$productFactory;
    $this->_productCollectionFactory = $productCollectionFactory;
    $this->_productLinkFactory = $productLinkFactory;
    $this->_prod = $product;
    $this->_productRepo = $productRepository;
    $this->om = \Magento\Framework\App\ObjectManager::getInstance();
//  $this->_productCopier = $this->om->create('\Magento\Catalog\Model\Product\Copier');
//  $this->copyConstructor = $copyConstructor;
//  $this->_productCopier = $productCopier;
}
public function groupProductBySku($group_sku){
    $products=$this->listProductsByGroupSku($group_sku)->load();
    $names = array(); $descriptions = array();
    $newProduct = null;
    foreach($products as $prod){
        if ($newProduct==null) {
            //$this->copyConstructor($prod, $newProduct);;
            $newProduct->setData($prod->getData());
        }
        $names[] = $prod->getName();
        //$descriptions[] = $prod->getDescription();
        $descriptions[] = trim($prod->getData('description'));
    }
    $newProduct->setName($this->getCommonStringPart($names)['result']);
    $newProduct->setSku(str_replace('_group','',$group_sku));
    $newProduct->setUrlKey(str_replace(' ','_',$this->getCommonStringPart($names)['result']));
    $newProduct->setTypeId('grouped');
    $newProduct->save();
}

In most tries i get error:

PHP Fatal error:  Cannot instantiate interface Magento\Catalog\Model\Product\CopyConstructorInterface in vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php on line 73

Please, can you tell me what am I doing wrong?

1

The error just says that you cannot create an instance of an interface, since an interface is just like a blueprint of a class.

In Magento Dependency Injection, one can define which implementation of a Class should be used if an Interface is requested, but this does not seem to be done for the CopyConstructorInterface.

it seems that the preference is not declared anywhere (it should be in any di.xml) https://github.com/magento/magento2/search?q=CopyConstructorInterface&type=Code&utf8=%E2%9C%93

So you would have to define your own preference (http://alanstorm.com/magento_2_object_manager_preferences/) or directly inject the implementation that you need (e.g. Magento\Bundle\Model\Product\CopyConstructor\Bundle)

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.