Magento 2 Frontend Fix: Resolving Related and Up-Sell Product Display Conflicts

As an e-commerce platform, Magento 2 relies on robust frontend rendering to deliver a seamless shopping experience. Features like "Related Products" and "Up-Sell Products" are crucial for increasing average order value and product discovery. However, even well-established features can sometimes harbor subtle bugs that impact storefront presentation and user experience. A recent GitHub issue (and its associated Pull Request) sheds light on one such critical frontend conflict, offering valuable insights for both Magento developers and merchants.

The Hidden Conflict: Related vs. Up-Sell Product Visibility

Imagine assigning the same product as both a "Related Product" and an "Up-Sell Product" to another item. Logically, you'd expect it to appear correctly in both sections on the product page. Yet, a long-standing bug in Magento 2 (reproduced on 2.4-develop) caused these blocks to "fight" over visibility, often resulting in the product appearing in only one of the two sections, or even disappearing entirely from one.

The root of this issue lay in how Magento rendered these product items within the

app/code/Magento/Catalog/view/frontend/templates/product/list/items.phtml
template. Each related and up-sell item was assigned a DOM ID derived solely from the product ID:

id="product-item_getId() ?>"

This approach led to two significant problems:

  • Duplicate DOM IDs: When the same product appeared in both a related and an up-sell block, it resulted in two HTML elements sharing the identical ID (e.g.,
    id="product-item_123"
    ). This is invalid HTML and can lead to unpredictable behavior in browsers and JavaScript.
  • Document-Global Style Rules: Magento's
    $secureRenderer->renderStyleAsTag('display:none;', 'li#product-item_' . $_item->getId())
    emitted a style rule that was global to the entire document. If the logic for one block (e.g., related products) decided to hide its entry for a specific product, this global rule would also inadvertently hide the corresponding entry in the other block (e.g., up-sell products), as both shared the same DOM ID.

The Solution: Namespacing for Clarity and Correctness

The fix, originally proposed by @igorwulff (Partner: Youwe) in PR #37482 and rebased into PR #41171, addresses these issues by introducing namespacing to the product item IDs. Instead of a generic

product-item_
, the IDs are now block-specific:

  • product-item-related_
    for related products
  • product-item-upsell_
    for up-sell products

This simple yet effective change ensures that each product instance within its respective block has a unique DOM ID. Consequently, the style rules emitted by

$secureRenderer
become localized, affecting only the intended element and preventing unintended hiding across different blocks.

Community Contribution and Robust Testing

A key aspect of this resolution was the community's commitment to quality. The original PR lacked automated tests, a common request for significant core changes. The rebased PR #41171 specifically added a new MFTF (Magento Functional Testing Framework) test,

StorefrontRelatedAndUpsellSharedProductVisibilityTest
. This test meticulously verifies:

  • The presence of exactly one
    li#product-item-related_
    and one
    li#product-item-upsell_
    .
  • The absence of the old, conflicting
    li#product-item_
    .
  • That neither element computes to
    display: none
    , ensuring both are visible.

Further validation confirmed that no other core Magento components (JS, LESS, CSS, or other templates) relied on the old

product-item_
, making the ID rename a safe and self-contained change. This thorough approach highlights the importance of comprehensive testing in maintaining Magento's stability and ensuring that fixes don't introduce new regressions.

Impact for Merchants and Developers

For merchants, this fix means a more reliable and consistent display of related and up-sell products, enhancing the customer journey and potentially improving conversion rates. No longer will valuable product recommendations mysteriously disappear. For developers, it serves as a valuable lesson in frontend best practices, emphasizing the critical importance of unique DOM IDs and scoped styling, especially within complex templating environments like Magento 2. It also showcases the power of community contributions in identifying and resolving core platform issues, ultimately leading to a more robust and predictable e-commerce experience.

Start with the tools

Explore migration tools

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

Explore migration tools