Skip to content

Fix PHP 8.5 ErrorException when creating a new product (null getById) - #70

Merged
mage-os-ci merged 2 commits into
mainfrom
fix/issue-289-null-product-id
Jul 14, 2026
Merged

mage-os-ci merged 2 commits into
mainfrom
fix/issue-289-null-product-id

Conversation

@rhoerr

@rhoerr rhoerr commented Jul 14, 2026

Copy link
Copy Markdown
Member

Summary

Fixes mage-os/mageos-magento2#289 — creating a new product in the admin throws an ErrorException under PHP 8.5:

Using null as an array offset is deprecated, use an empty string instead

Root cause

The stack trace lands in Magento\Catalog\Model\ProductRepository::getById(), but the null originates in this module's product-form modifier:

TranslationStores::modifyMeta → customizeTranslationStoresList → getTranslationStores() called:

$this->productRepository->getById($this->request->getParam("id"));

On the Add Product form there is no id request param, so getParam("id") returns null. getById(null) then uses null as an array offset internally ($this->instancesById[null][...]), which is a deprecation-turned-ErrorException in developer mode on PHP 8.5. The surrounding try/catch only caught NoSuchEntityException, so it escaped.

Fix

Return early when there is no product id (a new product has no assigned stores to translate), and cast the id to int for the load. This mirrors the guard the module already uses for the CMS-block button (!getParam('block_id')), which is why that path was never affected.

$productId = $this->request->getParam("id");
if (empty($productId)) {
    return $translationStores;
}
$currentProduct = $this->productRepository->getById((int)$productId);

Tests

Adds Test/Unit/.../TranslationStoresTest.php:

  • getById() is never called for a new product (no id param)
  • getById() is called with the id cast to int for an existing product

Verified the test fails with the fix reverted and passes with it.

Notes

  • Backward compatible; no API/schema changes.
  • CHANGELOG updated ([2.1.3]).

🤖 Generated with Claude Code

Guard the translation-stores form modifier against a missing product
id so getById() is never called with null.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@rhoerr
rhoerr requested review from a team, SamueleMartini and dadolun95 as code owners July 14, 2026 03:06
@mage-os-ci
mage-os-ci merged commit a695543 into main Jul 14, 2026
2 checks passed
@mage-os-ci
mage-os-ci deleted the fix/issue-289-null-product-id branch July 14, 2026 03:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PHP 8.5 ErrorException

2 participants