Magento 2 Multi-Shipping Bug: Virtual Bundles Double-Counted

Unpacking a Magento 2 Multi-Shipping Glitch: The Case of the Double-Counted Virtual Bundle

The Magento 2 ecosystem thrives on robust features, but even the most intricate systems can harbor subtle bugs. A recent GitHub issue (#41864) has brought to light a critical problem affecting multi-shipping functionality when virtual bundle products are involved. This insight delves into the specifics of this bug, its impact on Magento 2.4.x stores, and the community-driven solution.

The Core Issue: Virtual Bundles and Multi-Shipping Mismatch

The problem manifests when a Magento 2 store, with multi-shipping enabled, includes a specific combination of products in the cart: a bundle product configured to "Ship Bundle Items: Separately" where all its selections are virtual products, alongside at least one simple (physical) product. Under normal circumstances, the "Check Out with Multiple Addresses" link should be available because a physical item is present.

However, a bug in the Magento\Quote\Model\Quote\QuantityCollector::collectItemsQtys() method leads to an incorrect calculation of virtual_items_qty. Specifically, when the quote's totals are re-collected (e.g., after an admin changes a product price, setting quote.trigger_recollect = 1), the bundle product's quantity is double-counted. This happens because the bundle itself is considered virtual (as all its children are virtual), and its individual virtual children are also counted. This results in an inflated virtual_items_qty.

Impact on Checkout Flow

The inflated virtual_items_qty causes the Magento\Multishipping\Helper\Data::isMultishippingCheckoutAvailable() method to incorrectly determine that multi-shipping is not possible. Merchants and customers then encounter one of two frustrating scenarios:

  • The "Check Out with Multiple Addresses" link disappears from the cart page after a totals re-collection.
  • If a customer attempts to click the multi-address checkout link, they are redirected back to the cart with an error message: "The current cart does not match multi shipping criteria, please review or contact the store administrator."

This directly impacts the customer experience and can lead to abandoned carts, especially for B2B or complex orders that rely heavily on multi-address shipping.

Deep Dive into the Technical Root Cause

The issue stems from how QuantityCollector::collectItemsQtys() processes items. The logic contains two separate if conditions that can both apply to a virtual bundle with ship-separately children:

  1. An if condition that adds the quantity of an item if it's virtual and not a child item (this catches the bundle parent itself if it's virtual).
  2. A subsequent if condition that iterates through children of a ship-separately bundle and adds quantities of virtual children.

When a bundle meets both criteria (is itself virtual AND ships children separately, with all children being virtual), its quantity is added by the first condition, and then its children's quantities are added by the second, effectively double-counting the virtual components. This contrasts with Quote::getItemsSummaryQty() which uses an else structure, preventing this double-counting.

The Proposed Solution

The community has identified a clear path to resolution: modifying the conditional logic within QuantityCollector::collectItemsQtys(). The proposed fix involves changing the second if statement to an elseif, ensuring that an item is processed only once for its virtual quantity contribution. This aligns the logic with getItemsSummaryQty(), preventing the erroneous double-counting.

// app/code/Magento/Quote/Model/Quote/QuantityCollector.php
// ... inside collectItemsQtys() loop ...

if ($item->getIsVirtual() && !$item->getParentItem()) {
    $virtualItemsQty += $item->getQty();
} elseif ($item->getProduct()->getTypeId() === 'bundle' && $item->getProduct()->getShipBundleItems() === 0) {
    foreach ($item->getChildren() as $child) {
        if ($child->getIsVirtual()) {
            $virtualItemsQty += $child->getQty() * $item->getQty();
        }
    }
}
// ...

Community Engagement and Confirmation

The issue was initially reported with detailed steps, but required clarification on specific preconditions (all bundle selections being virtual, and the trigger_recollect flag). Through collaborative effort and updated reproduction steps provided by the original author, the Magento engineering team was able to confirm the bug on Magento 2.4-develop, affecting versions 2.4.x up to 2.4.9 and 2.4.7-p10. This confirmation underscores the value of precise bug reporting and community interaction in maintaining Magento's stability.

For Magento merchants and developers, this insight highlights a crucial area to monitor, especially for stores leveraging complex product configurations and multi-shipping. Implementing the proposed fix can prevent significant checkout disruptions and ensure a smoother customer experience.

Start with the tools

Explore migration tools

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

Explore migration tools