Unmasking a Hidden Performance Bottleneck in Magento 2 Wishlist Caching

As e-commerce migration experts, we often delve into the intricacies of Magento 2's core functionalities to uncover potential performance bottlenecks. A recent discovery on GitHub (Issue #41151) has brought to light a significant, yet subtle, performance issue within the Magento 2 Wishlist module that could be impacting countless stores running on Adobe Commerce and Open Source editions.

The Hidden Recalculation Bug in Magento 2 Wishlist

The issue, reported by Eddcapone, details a critical flaw in the Wishlist\Helper\Data::getItemCount() method. This method, responsible for determining the number of items in a customer's wishlist, is intended to leverage caching to avoid unnecessary database queries. However, a logical error causes it to recalculate the wishlist item count on virtually every request for logged-in customers, regardless of whether any changes have occurred.

Root Cause: A Flawed Caching Condition

The core of the problem lies within a specific conditional check in the getItemCount() method. After the initial call to calculate(), the method setDisplayOutOfStockProducts() is unconditionally invoked. This action permanently sets a session variable, causing the condition $this->_customerSession->hasDisplayOutOfStockProducts() to always evaluate to true for the remainder of the customer's session. Consequently, the caching mechanism is bypassed, forcing a full recalculation every time the wishlist count is requested.

This bug has been consistently reproduced across Magento versions 2.4.4, 2.4.5, 2.4.6, 2.4.7, 2.4.8, and 2.4.9, indicating its long-standing presence in the core codebase.

Significant Performance Impact

The implications of this bug are far-reaching. For stores with active customer wishlists, every AJAX call that includes the wishlist section (e.g., customer/section/load?secti>) triggers an expensive, uncached recalculation. On a production-like environment, this has been measured to add 1.7-2.4 seconds consistently to each such request. This not only degrades the user experience by slowing down page loads and interactions but also increases server load and extends the duration of PHP session locks, which can compound under concurrent requests.

The Proposed Solution: A Simple Yet Effective Code Fix

Fortunately, the issue author has also provided a clear and concise solution. The fix involves modifying the conditional logic within Wishlist\Helper\Data::getItemCount() to compare actual values rather than merely checking if a session variable has been set. This restores the intended caching behavior, allowing the method to reuse cached values when no relevant changes have occurred.

// Original problematic code snippet (simplified for context)
public function getItemCount()
{
    ...
    if (!$this->_customerSession->hasWishlistItemCount() ||
        $currentDisplayType != $storedDisplayType ||
        $this->_customerSession->hasDisplayOutOfStockProducts() ||   // <- always true after the first calculate()
        $currentDisplayOutOfStockProducts != $storedDisplayOutOfStockProducts
    ) {
        $this->calculate();
    }
    ...
}

// Suggested fix:
if (!$this->_customerSession->hasWishlistItemCount() ||
    $currentDisplayType != $storedDisplayType ||
    $currentDisplayOutOfStockProducts != $storedDisplayOutOfStockProducts
) {
    $this->calculate();
}

By implementing this change, the now-redundant hasDisplayOutOfStockProducts() check can be removed, as the subsequent value comparison effectively covers both "changed since last time" and "never set before" scenarios.

What This Means for Magento Merchants and Developers

This GitHub issue serves as a vital alert for Magento 2 users. While classified as S3/S4 severity (affecting non-critical functionality or usability), its direct impact on performance for logged-in users makes it a high-priority item for optimization. Developers can implement the suggested fix as a temporary workaround or a permanent patch until an official Magento update addresses the issue. For merchants, understanding such underlying performance quirks is crucial for maintaining a fast and responsive e-commerce platform, especially when considering migrations or extensive custom development.

Staying informed about such community-driven insights is key to optimizing your Magento 2 store's performance and ensuring a seamless customer experience.

Start with the tools

Explore migration tools

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

Explore migration tools