Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
few_simple_products.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 use Magento\Catalog\Model\ProductFactory;
11 
13 $objectManager = Bootstrap::getObjectManager();
14 
16 $productFactory = $objectManager->create(ProductFactory::class);
17 
19 $productRepository = $objectManager->create(ProductRepositoryInterface::class);
20 
21 // Create 10 products (with change this variable, don't forget to change the same in rollback)
23 
24 for ($i = 1; $i <= $productsAmount; $i++) {
25  $productArray = [
26  'data' => [
27  'name' => "Product{$i}",
28  'sku' => "Product{$i}",
29  'price' => 100,
30  'attribute_set_id' => 4,
31  'website_ids' => [1]
32  ]
33  ];
34 
35  $productRepository->save($productFactory->create($productArray));
36 }
$productRepository
$i
Definition: gallery.phtml:31