public function aroundGetStockItem(
        \Magento\CatalogInventory\Model\StockRegistryProvider $subject,
        \Closure $proceed,
        $productId,
        $scopeId
    ) {
        // $result = $proceed($productId, $scopeId); // original method will create empty item for stock registry
        $result = $this->storeStockRegistry->getStockItem($productId, $scopeId);
        if (null === $result) {
            $criteria = $this->factStockItemCrit->create();
            $criteria->setProductsFilter($productId);
            $stockId = $this->manStock->getCurrentStockId();
            $criteria->setStockFilter($stockId);
            $collection = $this->daoStockItem->getList($criteria);
            $result = current($collection->getItems());
            if ($result && $result->getItemId()) {
                $this->storeStockRegistry->setStockItem($productId, $scopeId, $result);
            } else {
                $result = $this->factStockItem->create();
            }
        }
        return $result;
    }
public function aroundGetStockItem(
        \Magento\CatalogInventory\Model\StockRegistryProvider $subject,
        \Closure $proceed,
        $productId,
        $scopeId
    ) {
        // $result = $proceed($productId, $scopeId); // original method will create empty item for stock registry
        $result = $this->storeStockRegistry->getStockItem($productId, $scopeId);
        if (null === $result) {
            $criteria = $this->factStockItemCrit->create();
            $criteria->setProductsFilter($productId);
            $stockId = $this->manStock->getCurrentStockId();
            $criteria->setStockFilter($stockId);
            $collection = $this->daoStockItem->getList($criteria);
            $result = current($collection->getItems());
            if ($result && $result->getItemId()) {
                $this->storeStockRegistry->setStockItem($productId, $scopeId, $result);
            } else {
                $result = $this->factStockItem->create();
            }
        }
        return $result;
    }
public function aroundGetStockItem ( \ Magento \ CatalogInventory \ Model \ StockRegistryProvider $ subject , \ Closure $ proceed , $ productId , $ scopeId ) { // $result = $proceed($productId, $scopeId); // original method will create empty item for stock registry $ result = $ this -> storeStockRegistry -> getStockItem ( $ productId , $ scopeId ) ; if ( null === $ result ) { $ criteria = $ this -> factStockItemCrit -> create ( ) ; $ criteria -> setProductsFilter ( $ productId ) ; $ stockId = $ this -> manStock -> getCurrentStockId ( ) ; $ criteria -> setStockFilter ( $ stockId ) ; $ collection = $ this -> daoStockItem -> getList ( $ criteria ) ; $ result = current ( $ collection -> getItems ( ) ) ; if ( $ result && $ result -> getItemId ( ) ) { $ this -> storeStockRegistry -> setStockItem ( $ productId , $ scopeId , $ result ) ; } else { $ result = $ this -> factStockItem -> create ( ) ; } } return $ result ; }
Detect current stock and get appropriate stock item.

@param \Magento\CatalogInventory\Model\StockRegistryProvider $subject
@param \Closure $proceed
@param int $productId
@param int $scopeId
@return \Magento\CatalogInventory\Api\Data\StockItemInterface
Detect current stock and get appropriate stock item .