Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
stocks.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
10 use Magento\InventoryApi\Api\Data\StockInterfaceFactory;
13 
15 $stockFactory = Bootstrap::getObjectManager()->get(StockInterfaceFactory::class);
17 $dataObjectHelper = Bootstrap::getObjectManager()->get(DataObjectHelper::class);
19 $stockRepository = Bootstrap::getObjectManager()->get(StockRepositoryInterface::class);
20 
22  [
23  // define only required and needed for tests fields
24  StockInterface::STOCK_ID => 10,
25  StockInterface::NAME => 'EU-stock',
26  ],
27  [
28  StockInterface::STOCK_ID => 20,
29  StockInterface::NAME => 'US-stock',
30  ],
31  [
32  StockInterface::STOCK_ID => 30,
33  StockInterface::NAME => 'Global-stock',
34  ],
35 ];
36 foreach ($stocksData as $stockData) {
38  $stock = $stockFactory->create();
39  $dataObjectHelper->populateWithArray($stock, $stockData, StockInterface::class);
40  $stockRepository->save($stock);
41 }
$stockFactory
Definition: stocks.php:15
$stockRepository
Definition: stocks.php:19
$dataObjectHelper
Definition: stocks.php:17
$stock
$stocksData
Definition: stocks.php:21
$stockData
Definition: products.php:27