1

I am working on a module where I can add products by import.

This is my code: http://pastebin.com/DVkHVg0t

In my foreach I am creating a new object like this, because if I work with $this->product (ProductInterface) the data from the previous object is saved with my new product.

$product = $this->objectManager->create('\Magento\Catalog\Model\Product');

Is there any better way for doing this ?

1 Answer 1

1

you can use CollectionFactory like below :

Inject object of \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory class in constructor

public function __construct(
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
)
{
    $this->_productCollectionFactory = $productCollectionFactory;

}

public function import($resultSet)
{
    foreach($resultSet as $result) 
    {
      $product = $this->_productCollectionFactory->create();
      // your code..
    }
}
}
3
  • did the solution work for you ? Jun 2, 2016 at 9:57
  • Yes that solution worked for me, thank you for helping me out. Jun 2, 2016 at 19:06
  • Why are you using ProductCollectionFactory to create a product when you should use ProuctFactory instead?
    – Alex Dinca
    May 31, 2019 at 11:01

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.