Magento 2.4+ Critical Error: Colon in Quantity Field Triggers Deprecation Log
As e-commerce platforms evolve, maintaining compatibility with the latest server technologies is crucial. A recent GitHub issue on Magento 2 highlights a critical logging error that can arise when Magento 2.4+ stores run on PHP 8.1 or newer. This issue, triggered by seemingly innocuous user input, underscores the complexities of platform maintenance and the importance of robust error handling.
The Unexpected Critical Error: Colon in Quantity Input
The problem manifests when a user or, more commonly, a bot or scanner attempts to add a product to the cart with a quantity value containing a colon (e.g., qty=1:2). Instead of a simple validation error, Magento 2.4-develop (and likely other 2.4.x versions with PHP 8.1+) responds with a generic message: "We can't add this item to your shopping cart right now." More critically, this action logs a CRITICAL exception in exception.log for every such attempt.
The issue affects core functionalities such as:
- Adding items to the cart (
/checkout/cart/add/) - Updating item options (
/checkout/cart/updateItemOptions/) - Updating mini-cart quantities (
/checkout/sidebar/updateItemQty/)
Diving into the Technical Root Cause
The core of the problem lies within Magento's Magento\Framework\Filter\LocalizedToNormalized::filter() method. This filter is designed to normalize various localized inputs, including dates. The specific problematic branch is:
elseif ($this->_options['date_format'] === null && strpos($value, ':') !== false)When a quantity value like "1:2" is passed, it triggers this branch because it contains a colon, and the date_format option is typically null for quantity inputs. Inside this branch, the code attempts to call IntlDateFormatter::setPattern($this->_options['date_format']). On PHP 8.1+, passing null to IntlDateFormatter::setPattern() (which expects a string pattern) is deprecated. Magento's Magento\Framework\App\ErrorHandler then converts this deprecation notice into a full-blown CRITICAL exception, leading to the log entries.
Here's a snippet of the critical log entry:
main.CRITICAL: Exception: Deprecated Functionality: IntlDateFormatter::setPattern(): Passing null to parameter #1 ($pattern) of type string is deprecated in /lib/internal/Magento/Framework/Filter/LocalizedToNormalized.php on line 90
#1 lib/internal/Magento/Framework/Filter/LocalizedToNormalized.php(90): IntlDateFormatter->setPattern(NULL)
#2 app/code/Magento/Checkout/Controller/Cart/Add.php(136): Magento\Framework\Filter\LocalizedToNormalized->filter('1:2')
Reproducing the Bug: A Developer's Perspective
Initial attempts to reproduce the bug by Magento's engineering team failed due to using a GET request instead of the required POST. The issue author, lbajsarowicz, provided precise steps using both browser DevTools fetch and curl, demonstrating the necessity of a POST request to trigger the controller logic.
For instance, using curl:
BASE=http://magentok.local
JAR=$(mktemp)
FORM_KEY=$(curl -s -c "$JAR" -b "$JAR" "$BASE/" | grep -o 'name="form_key"[^>]*value="[A-Za-z0-9]*"' | head -1 | sed 's/.*value="//; s/"//')
curl -s -b "$JAR" -c "$JAR" -o /dev/null -w '%{http_code} -> %{redirect_url}
' \
--data-urlencode "form_key=$FORM_KEY" --data "product=1" --data-urlencode "qty=1:2" \
"$BASE/checkout/cart/add/"
tail -n 5 var/log/exception.log
This method successfully reproduced the CRITICAL log entry, confirming the bug.
Impact and Resolution
While the issue is classified as S3 (affecting non-critical functionality without forcing a workaround), the continuous logging of CRITICAL exceptions can quickly fill log files, obscure genuine critical errors, and potentially impact server performance. It also presents a poor user experience with a vague error message for an invalid quantity input.
The good news is that the issue has been officially confirmed by the Magento team. The author also mentioned that a fix is available in a separate pull request (#41448), indicating that a resolution is underway for future Magento updates. This highlights the importance of monitoring community contributions and keeping your Magento instance updated to benefit from such compatibility fixes.
For Magento merchants and developers, understanding such nuances is vital for maintaining a healthy and performant e-commerce platform, especially during Magento migrations or upgrades to newer PHP versions.