magento / magento/inventory

Wrong total_count on GET /V1/inventory/export-stock-salable-qty/{salesChannelType}/{salesChannelCode} API

Open
#3,416 1 comment 0 reactions 0 assignees View on GitHub
Progress: ready for grooming
Dominant language
PHP
Stars
357
Forks
262
PR merge metrics
No merged PRs in 30d

Description

### Preconditions
1. MSI tagged with 1.2.7-beta3, but also previous versions have the same bug;
2. A fixed number of products, in this example 95486.

### Steps to reproduce
Call the `GET /V1/inventory/export-stock-salable-qty/{salesChannelType}/{salesChannelCode}` [API](https://adobe-commerce.redoc.ly/2.4.7-admin/tag/inventoryexport-stock-salable-qtysalesChannelTypesalesChannelCode#operation/GetV1InventoryExportstocksalableqtySalesChannelTypeSalesChannelCode)
```
curl --location --globoff 'https://127.0.0.1:8080/rest/V1/inventory/export-stock-salable-qty/{salesChannelType}/{salesChannelCode}?searchCriteria[currentPage]=5&searchCriteria[pageSize]=1000' \
--header 'Authorization: Bearer TOKEN'
```

### Expected result
`total_count` should return the number of product with stock item in the select sales channel:
```
{
"items": [
{
"sku": "SKU_1",
"qty": 0,
"is_salable": false
},
[...]
{
"sku": "SKU_1000",
"qty": 0,
"is_salable": false
}
],
"search_criteria": {
"filter_groups": [],
"page_size": 1000,
"current_page": 5
},
"total_count": 95486
}
```

### Actual result
`total_count` return instead the number of items in the current response:
```
{
"items": [
{
"sku": "SKU_1",
"qty": 0,
"is_salable": false
},
[...]
{
"sku": "SKU_1000",
"qty": 0,
"is_salable": false
}
],
"search_criteria": {
"filter_groups": [],
"page_size": 1000,
"current_page": 5
},
"total_count": 1000
}
```

You can crearly see the bug [here](https://github.com/magento/inventory/blob/1.2.7-beta3/InventoryExportStock/Model/ExportStockSalableQtyBySalesChannel.php#L88). Proposed solution:
```diff
public function execute(
\Magento\InventorySalesApi\Api\Data\SalesChannelInterface $salesChannel,
\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
): ExportStockSalableQtySearchResultInterface {
$stock = $this->getStockBySalesChannel->execute($salesChannel);
$productSearchResult = $this->getProducts($searchCriteria);
$items = $this->preciseExportStockProcessor->execute($productSearchResult->getItems(), $stock->getStockId());
/** @var ExportStockSalableQtySearchResultInterface $searchResult */
$searchResult = $this->exportStockSalableQtySearchResultFactory->create();
$searchResult->setSearchCriteria($productSearchResult->getSearchCriteria());
$searchResult->setItems($items);
- $searchResult->setTotalCount(count($items));
+ $searchResult->setTotalCount($productSearchResult->getTotalCount());

return $searchResult;
}
```

Contributor guide

Open the contributing guide

Research direction

Start in InventoryExportStock/Model/ExportStockSalableQtyBySalesChannel.php, at execute() and the setTotalCount call identified in the issue. Reproduce the paginated GET request with the provided curl example, then verify that total_count represents all matching products rather than only the current page. Done means the response reports the full matching count while items remain paginated.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.