magento / magento/inventory

BulkInventoryTransfer fails for new inventory sources when products have no source item

Open
#3,423 3 comments 0 reactions 1 assignee Claimed by @engcom-November View on GitHub
Area: Catalog Component: InventoryCatalog Issue: Confirmed Reported on 2.4.x Reproduced on 2.4.x
Dominant language
PHP
Stars
357
Forks
262
PR merge metrics
No merged PRs in 30d

Description

### Preconditions (*)
1. Adobe Commerce 2.4.7-p4
2. Sample data installed
3. Some products created via rest API

### Steps to reproduce (*)
1. Create two new inventory sources
2. Create a new inventory stock
3. Assign the two new inventory sources to the stock
4. Navigate to the products list in the admin panel
5. Select all items and choose to "Transfer Inventory to Source"
6. Choose to transfer to one of the new inventory sources, and check to unassign from the original source

### Expected result (*)

1. Inventory should be transferred and a success message should display

### Actual result (*)

1. Error message is displayed `Warning: Trying to access array offset on value of type null in {path}/vendor/magento/module-inventory-catalog/Model/ResourceModel/BulkInventoryTransfer.php on line 131`
2. Inventory is not transferred

### Investigation

The code from the following method looks off:

https://github.com/magento/inventory/blob/997162df13e950d8492da6ce3a181430085bfc84/InventoryCatalog/Model/ResourceModel/BulkInventoryTransfer.php#L112

When `$dstSourceItemQty` is `null`, which seems likely to be the case for a brand new inventory source, it is by default set to `0`:

`$dstSourceItemQty = $dstSourceItem === null ? 0.0 : (float) $dstSourceItem[SourceItemInterface::QUANTITY];`

As a result, the first `elseif` in this conditional passes, which means that it will try to read the status array key from `$dstSourceItem`, which is `null`, which causes the error [(line 131). ](https://github.com/magento/inventory/blob/997162df13e950d8492da6ce3a181430085bfc84/InventoryCatalog/Model/ResourceModel/BulkInventoryTransfer.php#L131)

```php
if ($orgSourceItem !== null) {
$status = (int) $orgSourceItem[SourceItemInterface::STATUS];
} elseif ($dstSourceItemQty !== null) {
$status = (int) $dstSourceItem[SourceItemInterface::STATUS];
} else {
$status = (int) SourceItemInterface::STATUS_OUT_OF_STOCK;
}
```

It seems this method is not properly handing the use case of a new inventory source, and if you have products that were created by the API that might not yet have a source item in the database.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.