dmlab.workStart a project
← /blog

One missing array: how Mage-OS loaded stock for only the first product

A one-line regression in Mage-OS made bulk stock lookups return a single item, crashing carts and flipping bundles out of stock. What caused it, why the obvious fix wasn't enough, and how it got fixed upstream.

Since Mage-OS 1.3.0, asking the stock item repository for several products at once returned stock for only the first one. Nothing threw an error. The other items were just missing, and code that loads stock in bulk carried on with incomplete data.

On real stores that showed up in two ways:

  • Cart crashes. The stock preloader runs when quote items load. It got one stock item out of N, so extension code reading the others hit null, e.g. Call to a member function getQtyIncrements() on null.
  • Bundles going out of stock. Saving any child product in the admin recalculates the parent bundle’s stock. With only one child’s stock loaded, the bundle was saved as out of stock even though every child was in stock.

The bug was reported as mage-os/mageos-magento2#352 by @laurnts, with a detailed analysis that pointed straight at the cause. I picked it up and sent the fix in #354.

Affected versions

  • Mage-OS 1.3.0 up to 3.6.0. 1.2.0 and earlier are fine.
  • Fixed in Mage-OS 4.0.0.
  • Magento Open Source and Adobe Commerce aren’t affected: their setter always wraps the value (see below).

If you can’t upgrade yet, apply the StockItemCriteria and StockItemCriteriaMapper changes from #354 to magento/module-catalog-inventory as a composer patch.

The cause: how criteria mappers call their methods

Stock criteria in Magento go through a mapper. AbstractMapper::map() takes each stored criteria value and spreads it into positional arguments of the matching method:

call_user_func_array([$this, $mapperMethod], array_values($value));

So every setter has to store its value wrapped once: the stored array is the argument list. Adobe’s StockItemCriteria::setProductsFilter() does exactly that:

$this->data['products_filter'] = [$products];

In Mage-OS 1.3.0 it changed to store arrays unwrapped:

if (is_array($products)) {
    $this->data['products_filter'] = $products;
} else {
    $this->data['products_filter'] = [$products];
}

Now setProductsFilter([1, 2, 3]) stores [1, 2, 3], the mapper calls mapProductsFilter(1, 2, 3), and the method only reads its first argument. The query becomes WHERE product_id IN (1).

Why restoring the wrapper wasn’t enough

The one-line revert works for the reported case. But the wrapper had been removed for a reason, in #134, and putting it back would have brought that problem back too.

Some callers pass the result of getChildrenIds() straight into the filter. That method doesn’t return a flat list. For a configurable product it returns [0 => [id => id, ...]]. Wrapped once more, the mapper received a nested array. For a configurable with no children, the inner array was empty, the “no ids” fallback didn’t kick in because the outer array wasn’t empty, and the query ended up as IN (), an SQL error. #134 fixed that by unwrapping, and in doing so broke every caller that passed a flat list.

My first version of the PR restored the wrapper and changed the configurable updater to pass the actual list, getChildrenIds($productId)[0], the way other callers already do.

In review, @rhoerr found a case I had missed: the grouped product updater passes getChildrenIds() output keyed by link type. With the wrapper restored, a grouped product with no eligible children would hit the same IN () error. Rather than patch every caller, he suggested making the mapper itself tolerant of nested input. That is the fix that got merged:

public function mapProductsFilter($products)
{
    $productIds = [];
    if (!is_array($products)) {
        $products = [$products];
    }
    array_walk_recursive($products, static function ($product) use (&$productIds) {
        $productIds[] = $product instanceof \Magento\Catalog\Model\Product ? $product->getId() : $product;
    });
    if (empty($productIds)) {
        $productIds[] = false;
    }
    $this->addFieldToFilter('main_table.product_id', ['in' => $productIds]);
}

Flattening in one place covers configurables, grouped products and any third-party code that passes raw getChildrenIds() output. The false for an empty list is Magento’s original code, not part of the fix: it turns “no ids” into a filter that matches nothing instead of an IN () syntax error.

This is a trade-off. A mapper that flattens any nesting will also quietly accept shapes nobody intended, and quiet acceptance is how this bug went unnoticed. Failing loudly on an unexpected shape would be stricter, but it would break every caller that passes getChildrenIds() output today, including extensions.

Tests, so it doesn’t flip again

This code had already gone back and forth once, so the PR adds integration tests for both directions:

  • getList() for three products returns three stock items (without the fix: one);
  • getList() with a nested list of several products, and with an empty nested list;
  • a grouped product whose only child is filtered out: without the mapper change it fails with the IN () error, with it the parent goes out of stock as expected.

The existing bundle test testShouldUpdateBundleStockStatusIfChildProductsStockStatusChanged (in both Bundle and BundleImportExport) fails on main and passes with the fix. I also checked the bundle scenario by hand on a clean install: on main the bundle flips to out of stock after saving a child, with the fix it stays in stock.

#354 was merged on October 23, 2026. Along the way, CI failed in an unrelated test that passed only when two auto-increment IDs happened to match; that got its own fix in #363.

Takeaways

  • When a value goes through call_user_func_array(), its shape is part of the contract. One missing level of array turns “all ids” into “the first id” without any error.
  • A revert is only safe once you know why the original change was made.
  • Normalizing a known input shape in the shared code fixes every caller at once, including third-party ones. Just keep in mind that tolerant code also hides mistakes.
  • A good bug report and a careful review both made this fix better. Thanks to @laurnts and @rhoerr.