Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
products_for_index.php
Go to the documentation of this file.
1 <?php
7 use Magento\Catalog\Api\Data\ProductInterfaceFactory;
14 
16 $productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
18  [
19  'name' => 'index enabled',
20  'sku' => 'index_enabled',
21  'status' => Status::STATUS_ENABLED,
22  'visibility' => Visibility::VISIBILITY_BOTH,
23  ],
24  [
25  'name' => 'index disabled',
26  'sku' => 'index_disabled',
27  'status' => Status::STATUS_DISABLED,
28  'visibility' => Visibility::VISIBILITY_BOTH,
29  ],
30  [
31  'name' => 'index visible search',
32  'sku' => 'index_visible_search',
33  'status' => Status::STATUS_ENABLED,
34  'visibility' => Visibility::VISIBILITY_IN_SEARCH,
35  ],
36  [
37  'name' => 'index visible category',
38  'sku' => 'index_visible_category',
39  'status' => Status::STATUS_ENABLED,
40  'visibility' => Visibility::VISIBILITY_IN_CATALOG,
41  ],
42  [
43  'name' => 'index visible both',
44  'sku' => 'index_visible_both',
45  'status' => Status::STATUS_ENABLED,
46  'visibility' => Visibility::VISIBILITY_BOTH,
47  ],
48  [
49 
50  'name' => 'index not visible',
51  'sku' => 'index_not_visible',
52  'status' => Status::STATUS_ENABLED,
53  'visibility' => Visibility::VISIBILITY_NOT_VISIBLE,
54 
55  ]
56 ];
57 
59 $productFactory = Bootstrap::getObjectManager()->create(ProductInterfaceFactory::class);
60 foreach ($products as $data) {
62  $product = $productFactory->create();
63  $product
64  ->setTypeId(Type::TYPE_SIMPLE)
65  ->setAttributeSetId(4)
66  ->setName($data['name'])
67  ->setSku($data['sku'])
68  ->setPrice(10)
69  ->setVisibility($data['visibility'])
70  ->setStatus($data['status']);
72 
74  $stockItem = $product->getExtensionAttributes()->getStockItem();
75  $stockItem->setUseConfigManageStock(0);
77 }
$productRepository
$productFactory