Skip to content

[DRAFT] Synchronization between legacy inventory data and MSI

Valeriy Nayda edited this page May 10, 2018 · 3 revisions

Synchronization from MSI to LegacyCatalogInventory

Quantity and stock status

In MSI status field of \Magento\InventoryApi\Api\Data\SourceItemInterface is only one of condition for is_salable concept

But in legacy catalog inventory is_in_stock field of \Magento\CatalogInventory\Api\Data\StockItemInterface is aggregation of few rules related to quantity

In order for the old indexation to work correctly, we need to follow the same rules during synhronization

\Magento\InventoryCatalog\Plugin\InventoryApi\SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin
...
/** @var \Magento\CatalogInventory\Api\Data\StockItemInterface $legacyStockItem */
$legacyStockItem = $this->getLegacyStockItemBySku($sourceItem->getSku());
$legacyStockItem->setIsInStock((int)$sourceItem->getStatus());
$legacyStockItem->setQty($sourceItem->getQuantity());

/** \Magento\CatalogInventory\Model\Spi\StockStateProviderInterface::verifyStock */
$isInStock = $legacyStockItem->getManageStock() && $this->stockStateProvider->verifyStock($legacyStockItem);

$this->setDataToLegacyStockItem->execute(
    $sourceItem->getSku(),
    (float)$sourceItem->getQuantity(),
    (int)$isInStock
);

Indexation

Since the proper date has been set on previous step, we can use legacy reindexation mechanism

\Magento\InventoryCatalog\Plugin\InventoryApi\SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin
...
$indexer = $this->indexerRegistry->get('cataloginventory_stock');
if (false === $indexer->isScheduled()) {
    $indexer->reindexList($productIds);
}

Complex products

Introduce chain of data synchronization objects (composite pattern)

$this->dataSynchronizationProcessor->execute($sourceItems);

Configuration

<type name="Magento\InventoryCatalog\Model\DataSynchronizationProcessorChain">
        <arguments>
            <argument name="processors" xsi:type="array">
		<item name="configurable" xsi:type="array">
                    <item name="sort_order" xsi:type="number">20</item>
                    <item name="object" xsi:type="object">Magento\InventoryConfigurableProduct\Model\ConfigurableDataSynchronizationProcessor</item>
                </item>
            </argument>
        </arguments>
    </type>

Complex products processor should be executed after simple products processor

$parentProductIds = $this->configurable->getParentIdsByChild($productIds);
$indexer = $this->indexerRegistry->get('cataloginventory_stock');
if (false === $indexer->isScheduled()) {
    $indexer->reindexList($productIds);
}
  1. Synchronization from LegacyCatalogInventory to MSI \Magento\InventoryCatalog\Plugin\CatalogInventory\UpdateSourceItemAtLegacyStockItemSavePlugin::aroundSave

Synchronization from LegacyCatalogInventory to MSI

Plugin on Magento\CatalogInventory\Model\ResourceModel\Stock\Item

\Magento\InventoryCatalog\Plugin\CatalogInventory\UpdateSourceItemAtLegacyStockItemSavePlugin::aroundSave
...
// need to save configuration
$proceed($legacyStockItem);

$typeId = $this->getTypeId($legacyStockItem);
if ($this->isSourceItemsAllowedForProductType->execute($typeId)) {
    $this->updateSourceItemBasedOnLegacyStockItem->execute($legacyStockItem);
}
\Magento\InventoryCatalog\Model\UpdateSourceItemBasedOnLegacyStockItem::execute
...
$sourceItem->setQuantity((float)$legacyStockItem->getQty());
$sourceItem->setStatus((int)$legacyStockItem->getIsInStock());
$this->sourceItemsSave->execute([$sourceItem]);

Synchronization at Import

Synchronization should not execute at Import process

Plugins on implementation???

Testcases

Work with new Inventory interfaces

Data: qty=0, stock_status=1

Steps:

  1. $sourceItemsSave->execute($sourceItems);
  2. \Magento\InventoryCatalog\Plugin\InventoryApi\SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin::afterExecute

Result: SourceItems has qty=0, stock_status=1. LegacyStockItem has qty=0, stock_status=0.

The same after full reindexation

Work with legacy Inventory interfaces

Data: qty=0, stock_status=1

Steps:

  1. $stockItemsRepository->save($legacyStockItem);
  2. \Magento\InventoryCatalog\Plugin\CatalogInventory\UpdateSourceItemAtLegacyStockItemSavePlugin::aroundSave
  3. \Magento\InventoryCatalog\Plugin\InventoryApi\SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin::afterExecute (chain is broken due to SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin updates via diret SQL queries)

Results: SourceItems has qty=0, stock_status=0. LegacyStockItem has qty=0, stock_status=0. The same after full reindexation

MSI Documentation:

  1. Technical Vision. Catalog Inventory
  2. Installation Guide
  3. List of Inventory APIs and their legacy analogs
  4. MSI Roadmap
  5. Known Issues in Order Lifecycle
  6. MSI User Guide
  7. DevDocs Documentation
  8. User Stories
  9. User Scenarios:
  10. Technical Designs:
  11. Admin UI
  12. MFTF Extension Tests
  13. Weekly MSI Demos
  14. Tutorials
Clone this wiki locally