I recently worked on a Magento site that included AJAX basket functionality.  When the site went live, the majority of the products were “simple” and the functionality worked well.  Recently, the client went live with a series of “bundle” products, and we soon noticed a bug.  On the basket page, Magento would just show the bundled product, however, as well as the bundled product showing in the AJAX cart preview, it would also list all the simple products that made up that bundled product.

The code we had was as follows:

As stated previously, the problem here is that “simple” products who are the components of “bundle” products are displaying also. So how do we filter them out? The issue is that $checkoutSession->getQuote()->getAllItems() returns a “flat” version of the cart. We need to change our call entirely.

The way getItems works is that it gets all the items and keeps their relationship, so we can simply make a check against $item->getParentItemId() to check if the “simple” product we have is actually a child of a bundle.

Image Credit: *MarS