-
Notifications
You must be signed in to change notification settings - Fork 248
[DRAFT] Synchronization between legacy inventory data and MSI
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
);
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);
}
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);
}
- Synchronization from LegacyCatalogInventory to MSI \Magento\InventoryCatalog\Plugin\CatalogInventory\UpdateSourceItemAtLegacyStockItemSavePlugin::aroundSave
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 should not execute at Import process
Plugins on implementation???
Data:
qty=0, stock_status=1
Steps:
$sourceItemsSave->execute($sourceItems);
\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
Data:
qty=0, stock_status=1
Steps:
- $stockItemsRepository->save($legacyStockItem);
\Magento\InventoryCatalog\Plugin\CatalogInventory\UpdateSourceItemAtLegacyStockItemSavePlugin::aroundSave
-
\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
Multi-Source Inventory developed by Magento 2 Community
- Technical Vision. Catalog Inventory
- Installation Guide
- List of Inventory APIs and their legacy analogs
- MSI Roadmap
- Known Issues in Order Lifecycle
- MSI User Guide
- 2.3 LIVE User Guide
- MSI Release Notes and Installation
- Overview
- Get Started with MSI
- MSI features and processes
- Global and Product Settings
- Configure Source Selection Algorithm
- Create Sources
- Create Stock
- Assign Inventory and Product Notifications
- Configure MSI backorders
- MSI Import and Export Product Data
- Mass Action Tool
- Shipment and Order Management
- CLI reference
- Reports and MSI
- MSI FAQs
- DevDocs Documentation
- Manage Inventory Management Modules (install/upgrade info)
- Inventory Management
- Reservations
- Inventory CLI reference
- Inventory API reference
- Inventory In-Store Pickup API reference
- Order Processing with Inventory Management
- Managing sources
- Managing stocks
- Link and unlink stocks and sources
- Manage source items
- Perform bulk actions
- Manage Low-Quantity Notifications
- Check salable quantities
- Manage source selection algorithms
- User Stories
- Support of Store Pickup for MSI
- Product list assignment per Source
- Source assignment per Product
- Stocks to Sales Channel Mapping
- Adapt Product Import/Export to support multi Sourcing
- Introduce SourceCode attribute for Source and SourceItem entities
- Assign Source Selector for Processing of Returns Credit Memo
- User Scenarios:
- Technical Designs:
- Module Structure in MSI
- When should an interface go into the Model directory and when should it go in the Api directory?
- Source and Stock Item configuration Design and DB structure
- Stock and Source Configuration design
- Open Technical Questions
- Inconsistent saving of Stock Data
- Source API
- Source WebAPI
- Sources to Sales Channels mapping
- Service Contracts MSI
- Salable Quantity Calculation and Mechanism of Reservations
- StockItem indexation
- Web API and How To cover them with Functional Testing
- Source Selection Algorithms
- Validation of Domain Entities
- PHP 7 Syntax usage for Magento contribution
- The first step towards pre generated IDs. And how this will improve your Integration tests
- The Concept of Default Source and Domain Driven Design
- Extension Point of Product Import/Export
- Source Selection Algorithm
- SourceItem Entity Extension
- Design Document for changing SerializerInterface
- Stock Management for Order Cancelation
- Admin UI
- MFTF Extension Tests
- Weekly MSI Demos
- Tutorials