From 350acb904261c1da8d5160902382e2ec8af98370 Mon Sep 17 00:00:00 2001 From: Ievgen Shakhsuvarov Date: Fri, 21 Jun 2019 14:17:19 -0500 Subject: [PATCH 1/3] Avoid querying inventory default stock view in storefront operations --- .../Model/ResourceModel/GetStockItemData.php | 41 +++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/InventoryIndexer/Model/ResourceModel/GetStockItemData.php b/app/code/Magento/InventoryIndexer/Model/ResourceModel/GetStockItemData.php index 079aa4fe499e..db3eccb55291 100644 --- a/app/code/Magento/InventoryIndexer/Model/ResourceModel/GetStockItemData.php +++ b/app/code/Magento/InventoryIndexer/Model/ResourceModel/GetStockItemData.php @@ -12,6 +12,8 @@ use Magento\InventoryIndexer\Model\StockIndexTableNameResolverInterface; use Magento\InventorySalesApi\Model\GetStockItemDataInterface; use Magento\InventoryIndexer\Indexer\IndexStructure; +use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface; +use Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface; /** * @inheritdoc @@ -28,16 +30,30 @@ class GetStockItemData implements GetStockItemDataInterface */ private $stockIndexTableNameResolver; + /** + * @var DefaultStockProviderInterface + */ + private $defaultStockProvider; + + /** + * @var GetProductIdsBySkusInterface + */ + private $getProductIdsBySkus; + /** * @param ResourceConnection $resource * @param StockIndexTableNameResolverInterface $stockIndexTableNameResolver */ public function __construct( ResourceConnection $resource, - StockIndexTableNameResolverInterface $stockIndexTableNameResolver + StockIndexTableNameResolverInterface $stockIndexTableNameResolver, + DefaultStockProviderInterface $defaultStockProvider, + GetProductIdsBySkusInterface $getProductIdsBySkus ) { $this->resource = $resource; $this->stockIndexTableNameResolver = $stockIndexTableNameResolver; + $this->defaultStockProvider = $defaultStockProvider; + $this->getProductIdsBySkus = $getProductIdsBySkus; } /** @@ -45,18 +61,29 @@ public function __construct( */ public function execute(string $sku, int $stockId): ?array { - $stockItemTableName = $this->stockIndexTableNameResolver->execute($stockId); - $connection = $this->resource->getConnection(); - $select = $connection->select() - ->from( + $select = $connection->select(); + + if ($this->defaultStockProvider->getId() === $stockId) { + $productId = current($this->getProductIdsBySkus->execute([$sku])); + $stockItemTableName = $this->resource->getTableName('cataloginventory_stock_status'); + $select->from( + $stockItemTableName, + [ + GetStockItemDataInterface::QUANTITY => 'qty', + GetStockItemDataInterface::IS_SALABLE => 'stock_status', + ] + )->where('product_id' . ' = ?', $productId); + } else { + $stockItemTableName = $this->stockIndexTableNameResolver->execute($stockId); + $select->from( $stockItemTableName, [ GetStockItemDataInterface::QUANTITY => IndexStructure::QUANTITY, GetStockItemDataInterface::IS_SALABLE => IndexStructure::IS_SALABLE, ] - ) - ->where(IndexStructure::SKU . ' = ?', $sku); + )->where(IndexStructure::SKU . ' = ?', $sku); + } try { if ($connection->isTableExists($stockItemTableName)) { From bb106aea9dd50819e803bbf7968a9dde5542f758 Mon Sep 17 00:00:00 2001 From: Eugene Shakhsuvarov Date: Fri, 21 Jun 2019 15:28:39 -0500 Subject: [PATCH 2/3] Minor DocBlock fix --- .../InventoryIndexer/Model/ResourceModel/GetStockItemData.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/code/Magento/InventoryIndexer/Model/ResourceModel/GetStockItemData.php b/app/code/Magento/InventoryIndexer/Model/ResourceModel/GetStockItemData.php index db3eccb55291..da3613e77450 100644 --- a/app/code/Magento/InventoryIndexer/Model/ResourceModel/GetStockItemData.php +++ b/app/code/Magento/InventoryIndexer/Model/ResourceModel/GetStockItemData.php @@ -43,6 +43,8 @@ class GetStockItemData implements GetStockItemDataInterface /** * @param ResourceConnection $resource * @param StockIndexTableNameResolverInterface $stockIndexTableNameResolver + * @param DefaultStockProviderInterface $defaultStockProvider + * @param GetProductIdsBySkusInterface $getProductIdsBySkus */ public function __construct( ResourceConnection $resource, From e84cfbe28415baf233c69fc97acaa9ecbc148146 Mon Sep 17 00:00:00 2001 From: Eugene Shakhsuvarov Date: Mon, 8 Jul 2019 15:38:28 -0500 Subject: [PATCH 3/3] Minor CR fixes --- .../InventoryIndexer/Model/ResourceModel/GetStockItemData.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/InventoryIndexer/Model/ResourceModel/GetStockItemData.php b/app/code/Magento/InventoryIndexer/Model/ResourceModel/GetStockItemData.php index da3613e77450..90072ff48fa3 100644 --- a/app/code/Magento/InventoryIndexer/Model/ResourceModel/GetStockItemData.php +++ b/app/code/Magento/InventoryIndexer/Model/ResourceModel/GetStockItemData.php @@ -75,7 +75,7 @@ public function execute(string $sku, int $stockId): ?array GetStockItemDataInterface::QUANTITY => 'qty', GetStockItemDataInterface::IS_SALABLE => 'stock_status', ] - )->where('product_id' . ' = ?', $productId); + )->where('product_id = ?', $productId); } else { $stockItemTableName = $this->stockIndexTableNameResolver->execute($stockId); $select->from(