Magento 2 API Alert: Fractional Bundle Quantities Silently Truncated in Cart

Magento 2 API Alert: Fractional Bundle Quantities Silently Truncated in Cart

As e-commerce migration experts at Shopping Mover, we constantly monitor the Magento ecosystem for critical issues that could impact our clients' operations and data integrity. A recent GitHub issue (#41870) has brought to light a significant bug in Magento 2's REST API, specifically affecting how bundle product quantities are handled when added to the cart. This issue reveals that if a fractional quantity (e.g., 1.5) is provided for a bundle product via the API, Magento 2 silently truncates it to an integer (e.g., 1) without any error or warning. This behavior can lead to incorrect order totals, inventory discrepancies, and a confusing customer experience.

The Silent Truncation Problem

The core of the problem lies in Magento 2's API allowing a request for a fractional quantity for a bundle product, only to process it incorrectly. Instead of rejecting the request with an appropriate error message like "You cannot use decimal quantity for this product," the system accepts the input, truncates the quantity, and adds the item to the cart as if the user had requested a whole number. For example, adding 1.5 units of a bundle product results in 1 unit being added to the cart. Subsequent additions can also be affected, leading to cumulative inaccuracies.

This issue has been confirmed on Magento Open Source 2.4-develop and is classified with a Severity S3, indicating it affects non-critical data or functionality but does not force users to employ a workaround. However, for businesses relying heavily on API integrations for cart management, this silent truncation can have serious implications for order accuracy and customer trust.

Technical Deep Dive: Why It Happens

The GitHub issue provides a clear technical explanation for this behavior. The root cause is identified within the Magento core code, specifically in the Magento\Bundle\Model\Product\Type::prepareQuoteItemQty() method, which explicitly casts the quantity to an integer using (int) $qty. Subsequently, the Magento\CatalogInventory\Model\Quote\Item\QuantityValidator::validate() method writes this truncated value back to the quote item without triggering an error.

Interestingly, the "Qty Uses Decimals" setting, which can be configured for products, does not prevent this truncation for the bundle product's own quantity when added via the API. The decimal quantity check primarily runs on the child items within the bundle, and its outcome depends on the child's "Qty Uses Decimals" setting and the bundle's selection quantity. This means even if child products allow decimals, the bundle's main quantity will still be truncated.

Here's the API request that reproduces the issue:

POST /rest/default/V1/guest-carts//items
{
  "cartItem": {
    "sku": "test-bundle",
    "qty": 1.5,
    "quote_id": "",
    "product_option": {
      "extension_attributes": {
        "bundle_options": [
          {"option_id": , "option_qty": 2, "option_selections": []}
        ]
      }
    }
  }
}

Community Reaction and Next Steps

The issue was reported by SilinMykola and quickly confirmed by engcom-Bravo from the Magento engineering team, validating its reproducibility on the latest 2.4-develop instance. A Jira issue (AC-19930) has been created, indicating that this bug is now officially tracked within Adobe Commerce's development pipeline for a future fix. While no immediate workarounds or solutions have been provided in the comments, the confirmation and tracking of the bug are crucial first steps.

For Magento merchants and developers, this insight underscores the importance of thorough testing for API integrations, especially when dealing with complex product types like bundles and custom quantity inputs. Until a fix is released, developers should be aware of this behavior and consider implementing client-side validation or server-side pre-processing to ensure that only integer quantities are passed for bundle products via the API, or to handle the truncation gracefully.

Start with the tools

Explore migration tools

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

Explore migration tools