Skip to main content

Load entities

Use an EntityCollectionQuery with the Magento 2 product repository to load products. Load only the attributes you need — loading every attribute is expensive.

Load products with all attributes

Loads every attribute of every product. Convenient, but not optimal for large catalogs:

$query = new EntityCollectionQuery();
$existingMagentoProducts = $this->magentoProductRepository->getProducts($query);

foreach ($existingMagentoProducts as $existingMagentoProduct) {
if ($existingMagentoProduct->getProductType() === Magento2ProductType::Configurable()) {
// Handle configurable products
}
}

Load only specific attributes

Use addAttribute() to restrict the query to the attributes you actually need. The following loads all products with only their price values:

$query = new EntityCollectionQuery();
$query->addAttribute('price');
$existingMagentoProducts = $this->magentoProductRepository->getProducts($query);

Save entities

$this->m2ProductRepository->saveProducts($products, false, true);