Skip to content

Commit dce0b52

Browse files
committed
ACP2E-3413: All products are indexed with [is_out_of_stock] = 1 when the SKU is not set as a searchable attribute
1 parent e9d6e33 commit dce0b52

File tree

2 files changed

+17
-41
lines changed

2 files changed

+17
-41
lines changed

InventoryCatalog/Plugin/Catalog/Model/ResourceModel/Product/CollectionPlugin.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2022 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -82,7 +82,7 @@ private function applyOutOfStockSortOrders(Collection $collection): void
8282
$collection->setFlag('is_sorted_by_oos', true);
8383

8484
if ($this->isOutOfStockBottom() && $this->sortableBySaleabilityProvider->isSortableBySaleability()) {
85-
$collection->setOrder(SortableBySaleabilityInterface::IS_OUT_OF_STOCK, Select::SQL_DESC);
85+
$collection->setOrder(SortableBySaleabilityInterface::IS_OUT_OF_STOCK, Select::SQL_ASC);
8686
}
8787
}
8888
$collection->setFlag('is_processing', false);

InventoryElasticsearch/Plugin/Model/Adapter/BatchDataMapper/ProductDataMapperPlugin.php

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,33 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2022 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\InventoryElasticsearch\Plugin\Model\Adapter\BatchDataMapper;
99

1010
use Magento\Elasticsearch\Model\Adapter\BatchDataMapper\ProductDataMapper;
1111
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\InventoryCatalogApi\Model\GetSkusByProductIdsInterface;
13+
use Magento\InventorySalesApi\Model\GetStockItemsDataInterface;
1214
use Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface;
1315
use Magento\Store\Api\StoreRepositoryInterface;
14-
use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
1516

1617
class ProductDataMapperPlugin
1718
{
18-
/**
19-
* @var StockByWebsiteIdResolverInterface
20-
*/
21-
private $stockByWebsiteIdResolver;
22-
23-
/**
24-
* @var StoreRepositoryInterface
25-
*/
26-
private $storeRepository;
27-
28-
/**
29-
* @var GetStockItemDataInterface
30-
*/
31-
private $getStockItemData;
32-
3319
/**
3420
* @param StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver
3521
* @param StoreRepositoryInterface $storeRepository
36-
* @param GetStockItemDataInterface $getStockItemData
22+
* @param GetStockItemsDataInterface $getStockItemsData
23+
* @param GetSkusByProductIdsInterface $getSkusByProductIds
3724
*/
3825
public function __construct(
39-
StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver,
40-
StoreRepositoryInterface $storeRepository,
41-
GetStockItemDataInterface $getStockItemData
26+
private readonly StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver,
27+
private readonly StoreRepositoryInterface $storeRepository,
28+
private readonly GetStockItemsDataInterface $getStockItemsData,
29+
private readonly GetSkusByProductIdsInterface $getSkusByProductIds
4230
) {
43-
$this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver;
44-
$this->storeRepository = $storeRepository;
45-
$this->getStockItemData = $getStockItemData;
4631
}
4732

4833
/**
@@ -64,20 +49,11 @@ public function afterMap(
6449
): array {
6550
$store = $this->storeRepository->getById($storeId);
6651
$stock = $this->stockByWebsiteIdResolver->execute((int)$store->getWebsiteId());
67-
52+
$skus = $this->getSkusByProductIds->execute(array_keys($documents));
53+
$stockItems = $this->getStockItemsData->execute($skus, $stock->getStockId());
6854
foreach ($documents as $productId => $document) {
69-
$sku = $document['sku'] ?? '';
70-
if (!$sku) {
71-
$document['is_out_of_stock'] = 1;
72-
} else {
73-
try {
74-
$stockItemData = $this->getStockItemData->execute($sku, $stock->getStockId());
75-
} catch (NoSuchEntityException $e) {
76-
$stockItemData = null;
77-
}
78-
$document['is_out_of_stock'] = null !== $stockItemData
79-
? (int)$stockItemData[GetStockItemDataInterface::IS_SALABLE] : 1;
80-
}
55+
$sku = $skus[$productId];
56+
$document['is_out_of_stock'] = (int)!($stockItems[$sku][GetStockItemsDataInterface::IS_SALABLE] ?? 0);
8157
$documents[$productId] = $document;
8258
}
8359

0 commit comments

Comments
 (0)