Skip to content

Commit 0ab1b18

Browse files
Merge pull request #2350 from magento/avoid-view-query-on-default-stock
Avoid querying inventory default stock view in storefront operations
2 parents 6cfd029 + 6cb8e94 commit 0ab1b18

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

InventoryIndexer/Model/ResourceModel/GetStockItemData.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Magento\InventoryIndexer\Model\StockIndexTableNameResolverInterface;
1313
use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
1414
use Magento\InventoryIndexer\Indexer\IndexStructure;
15+
use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface;
16+
use Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface;
1517

1618
/**
1719
* @inheritdoc
@@ -28,35 +30,62 @@ class GetStockItemData implements GetStockItemDataInterface
2830
*/
2931
private $stockIndexTableNameResolver;
3032

33+
/**
34+
* @var DefaultStockProviderInterface
35+
*/
36+
private $defaultStockProvider;
37+
38+
/**
39+
* @var GetProductIdsBySkusInterface
40+
*/
41+
private $getProductIdsBySkus;
42+
3143
/**
3244
* @param ResourceConnection $resource
3345
* @param StockIndexTableNameResolverInterface $stockIndexTableNameResolver
46+
* @param DefaultStockProviderInterface $defaultStockProvider
47+
* @param GetProductIdsBySkusInterface $getProductIdsBySkus
3448
*/
3549
public function __construct(
3650
ResourceConnection $resource,
37-
StockIndexTableNameResolverInterface $stockIndexTableNameResolver
51+
StockIndexTableNameResolverInterface $stockIndexTableNameResolver,
52+
DefaultStockProviderInterface $defaultStockProvider,
53+
GetProductIdsBySkusInterface $getProductIdsBySkus
3854
) {
3955
$this->resource = $resource;
4056
$this->stockIndexTableNameResolver = $stockIndexTableNameResolver;
57+
$this->defaultStockProvider = $defaultStockProvider;
58+
$this->getProductIdsBySkus = $getProductIdsBySkus;
4159
}
4260

4361
/**
4462
* @inheritdoc
4563
*/
4664
public function execute(string $sku, int $stockId): ?array
4765
{
48-
$stockItemTableName = $this->stockIndexTableNameResolver->execute($stockId);
49-
5066
$connection = $this->resource->getConnection();
51-
$select = $connection->select()
52-
->from(
67+
$select = $connection->select();
68+
69+
if ($this->defaultStockProvider->getId() === $stockId) {
70+
$productId = current($this->getProductIdsBySkus->execute([$sku]));
71+
$stockItemTableName = $this->resource->getTableName('cataloginventory_stock_status');
72+
$select->from(
73+
$stockItemTableName,
74+
[
75+
GetStockItemDataInterface::QUANTITY => 'qty',
76+
GetStockItemDataInterface::IS_SALABLE => 'stock_status',
77+
]
78+
)->where('product_id = ?', $productId);
79+
} else {
80+
$stockItemTableName = $this->stockIndexTableNameResolver->execute($stockId);
81+
$select->from(
5382
$stockItemTableName,
5483
[
5584
GetStockItemDataInterface::QUANTITY => IndexStructure::QUANTITY,
5685
GetStockItemDataInterface::IS_SALABLE => IndexStructure::IS_SALABLE,
5786
]
58-
)
59-
->where(IndexStructure::SKU . ' = ?', $sku);
87+
)->where(IndexStructure::SKU . ' = ?', $sku);
88+
}
6089

6190
try {
6291
if ($connection->isTableExists($stockItemTableName)) {

0 commit comments

Comments
 (0)