Skip to content

Commit 9bda11d

Browse files
author
Valeriy Nayda
committed
MSI: integration prototype (write scenario)
1 parent 6d95bca commit 9bda11d

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

app/code/Magento/CatalogInventory/Model/ResourceModel/Stock/Item.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,35 @@ protected function _afterSave(AbstractModel $object)
125125
if ($this->processIndexEvents) {
126126
$this->stockIndexerProcessor->reindexRow($object->getProductId());
127127
}
128+
129+
// start prototype code
130+
$sourceItemFactory = \Magento\Framework\App\ObjectManager::getInstance()
131+
->get(\Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory::class);
132+
/** @var \Magento\InventoryApi\Api\SourceItemsSaveInterface $sourceItemsSave */
133+
$sourceItemsSave = \Magento\Framework\App\ObjectManager::getInstance()
134+
->get(\Magento\InventoryApi\Api\SourceItemsSaveInterface::class);
135+
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
136+
$productRepository = \Magento\Framework\App\ObjectManager::getInstance()
137+
->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
138+
/** @var \Magento\InventoryCatalog\Api\DefaultSourceProviderInterface $defaultSourceProvider */
139+
$defaultSourceProvider = \Magento\Framework\App\ObjectManager::getInstance()
140+
->get(\Magento\InventoryCatalog\Api\DefaultSourceProviderInterface::class);
141+
142+
$sourceId = $defaultSourceProvider->getId();
143+
$product = $productRepository->getById($object->getProductId());
144+
$sku = $product->getSku();
145+
$qty = (float)$object->getQty();
146+
$status = (bool)$object->getIsInStock();
147+
148+
/** @var \Magento\InventoryApi\Api\Data\SourceItemInterface $sourceItem */
149+
$sourceItem = $sourceItemFactory->create();
150+
$sourceItem->setSourceId($sourceId);
151+
$sourceItem->setSku($sku);
152+
$sourceItem->setQuantity($qty);
153+
$sourceItem->setStatus($status);
154+
$sourceItemsSave->execute([$sourceItem]);
155+
// end prototype code
156+
128157
return $this;
129158
}
130159

app/code/Magento/Inventory/Observer/ProcessSourceItemsObserver.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public function __construct(SourceItemsProcessor $sourceItemsProcessor)
3939
*/
4040
public function execute(EventObserver $observer)
4141
{
42+
// start prototype code
43+
// Work like as in single stock mode
44+
return;
45+
// end prototype code
46+
4247
/** @var ProductInterface $product */
4348
$product = $observer->getEvent()->getProduct();
4449
/** @var Save $controller */

dev/tests/integration/testsuite/Magento/CatalogInventory/Model/StockItemSave/StockItemDataChecker.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,35 @@ public function checkStockItemData($sku, array $expectedData)
7474
$productLoadedByModel = $this->productFactory->create();
7575
$productLoadedByModel->load($product->getId());
7676
$this->doCheckStockItemData($product, $expectedData);
77+
78+
// start prototype code
79+
/** @var \Magento\InventoryApi\Api\SourceItemRepositoryInterface $sourceItemRepository */
80+
$sourceItemRepository = \Magento\Framework\App\ObjectManager::getInstance()
81+
->get(\Magento\InventoryApi\Api\SourceItemRepositoryInterface::class);
82+
/** @var \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder */
83+
$searchCriteriaBuilder = \Magento\Framework\App\ObjectManager::getInstance()
84+
->get(\Magento\Framework\Api\SearchCriteriaBuilder::class);
85+
/** @var \Magento\InventoryCatalog\Api\DefaultSourceProviderInterface $defaultSourceProvider */
86+
$defaultSourceProvider = \Magento\Framework\App\ObjectManager::getInstance()
87+
->get(\Magento\InventoryCatalog\Api\DefaultSourceProviderInterface::class);
88+
89+
$searchCriteria = $searchCriteriaBuilder
90+
->addFilter(\Magento\InventoryApi\Api\Data\SourceItemInterface::SOURCE_ID, $defaultSourceProvider->getId())
91+
->addFilter(\Magento\InventoryApi\Api\Data\SourceItemInterface::SKU, $sku)
92+
->create();
93+
$sourceItems = $sourceItemRepository->getList($searchCriteria)->getItems();
94+
\PHPUnit\Framework\Assert::assertCount(1, $sourceItems);
95+
96+
$sourceItem = reset($sourceItems);
97+
\PHPUnit\Framework\Assert::assertEquals(
98+
$expectedData[StockItemInterface::QTY],
99+
$sourceItem->getQuantity()
100+
);
101+
\PHPUnit\Framework\Assert::assertEquals(
102+
$expectedData[StockItemInterface::IS_IN_STOCK],
103+
(int)$sourceItem->getStatus()
104+
);
105+
// end prototype code
77106
}
78107

79108
/**

0 commit comments

Comments
 (0)