Critical Magento 2.4.9 Bug: Media Gallery API Resets Store-Specific Image Data

Critical Magento 2.4.9 Bug: Media Gallery API Resets Store-Specific Image Data

As e-commerce migration experts at Shopping Mover, we constantly monitor the Magento ecosystem for critical issues that can impact merchants and developers. A significant regression has been identified in Magento Open Source 2.4.9 (and also present in 2.4-develop) concerning the Media Gallery REST API. This bug can lead to unexpected data loss for product images in multi-store view setups, potentially causing considerable frustration and requiring manual workarounds.

The Problem: Unintended Data Reset via Media Gallery REST API

The core issue revolves around how Magento 2.4.9 handles product image updates via its REST API in a store view scope. When a developer or an integration system uses the Media Gallery REST API (specifically PUT, POST, or DELETE requests) to modify, add, or remove a single image for a product on a specific store view, it inadvertently resets the store-specific labels, positions, and disabled flags for all other images associated with that product on the same store view. This means carefully configured store-specific image data can be lost, reverting to default values.

Imagine managing a product with multiple images, each having unique labels or visibility settings tailored for different store views. An automated update to one image via API could wipe out these custom settings for all other images, forcing a time-consuming manual re-configuration or leading to incorrect product displays on your storefronts.

Affected Magento Versions and API Calls

  • Magento Versions: Open Source 2.4.9 and 2.4-develop (regression from 2.4.8).
  • PHP Version: PHP 8.3 (as reported, but likely affects other supported PHP versions too).
  • Affected API Endpoints:
    • PUT /rest/{store_code}/V1/products/{sku}/media/{image_id} (Update image)
    • POST /rest/{store_code}/V1/products/{sku}/media (Create new image)
    • DELETE /rest/{store_code}/V1/products/{sku}/media/{image_id} (Remove image)

Root Cause: A Missing Product ID in the Workflow

The detailed investigation into this bug points to a specific sequence of operations within Magento's core code. The issue stems from the GalleryManagement::update() method (and similar logic in create() and remove()). When processing media entries for a store view, a new, empty product object is created with only the SKU. Crucially, the product's actual ID is not set on this new object before a critical method called processUnmodifiedMediaEntries() is invoked.

This leads to DefaultValueProcessor::getStoreValues() attempting to read store-specific values using a product ID of 0 (because the ID hasn't been set). Consequently, it finds no existing store view rows for other images, incorrectly flagging them as needing to use default values. When the product is saved, these NULL values overwrite any previously configured store-specific settings.

The bug was introduced by commit 60a42c00c7 (ACP2E-4358) and was not present in Magento 2.4.8, indicating a regression in how product media gallery values are handled at the store view level.

The Proposed Solution

Fortunately, a clear and concise fix has been identified and confirmed by community members. The solution involves ensuring that the product ID is correctly set on the newly created product object before processUnmodifiedMediaEntries() is called. This simple addition ensures that the system correctly identifies and preserves existing store-specific image data.

Here's the suggested fix snippet, illustrating the addition of the product ID:

$product = $this->productInterfaceFactory->create();
$product->setSku($sku);
$product->setId($loadedProduct->getId()); // product loaded at the top of the method (or its link field value)

This fix, applied within methods like GalleryManagement::update(), create(), and remove(), ensures that the system correctly retrieves and maintains all store view-specific image attributes for unmodified images, preventing unintended data loss.

Community Confirmation and Next Steps

The issue has been thoroughly reproduced and confirmed by community member SilinMykola, who validated the bug's presence on 2.4-develop and confirmed that the proposed fix resolves the issue for all three affected API calls (update, remove, create). This confirmation is a strong indicator that an official patch or fix will be included in an upcoming Magento release.

For merchants and developers running or planning to upgrade to Magento 2.4.9, it's crucial to be aware of this bug. If your operations rely heavily on the Media Gallery REST API for multi-store product image management, consider implementing a temporary patch based on the suggested fix or carefully monitor official Magento releases for the resolution. Always ensure thorough testing in a staging environment before deploying any changes to production.

At Shopping Mover, we emphasize proactive bug awareness and robust testing as key components of successful Magento operations and migrations. Stay tuned for further updates on this and other critical Magento developments.

Start with the tools

Explore migration tools

See options, compare methods, and pick the path that fits your store.

Explore migration tools