Skip to content

$_item->getChildren() returns empty array for parent cart items when cart is paginated #34507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 of 5 tasks
samcleathero opened this issue Nov 2, 2021 · 11 comments · May be fixed by #35739 or #39793
Open
1 of 5 tasks

$_item->getChildren() returns empty array for parent cart items when cart is paginated #34507

samcleathero opened this issue Nov 2, 2021 · 11 comments · May be fixed by #35739 or #39793
Assignees
Labels
Area: Catalog Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: PR in progress Reported on 2.4.3-p1 Indicates original Magento version for the Issue report. Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Comments

@samcleathero
Copy link

Preconditions (*)

  1. PHP 7.4
  2. Magento 2.4.3-p1 (also replicated in 2.3.5-p1 and 2.4.2)

Steps to reproduce (*)

  1. Override cart item renderer template (Magento_Checkout/cart/item/default.phtml).
  2. Add $_item->getChildren() to get child items of configurable products - I am using this to get stock levels for child products when a configurable product is added to the cart. For demo purposes, I have var_dumped $_item->getHasChildren() in the screenshots below.
  3. Add enough individual products to the cart to trigger pagination in the cart (by default more than 20 products).
  4. Observe that without pagination active, $_item->getHasChildren() returns NULL for simple products, and (bool)true for configurable products. When pagination is triggered, $item->getHasChildren() returns NULL for all products, including configurables with children.

Expected result (*)

var_dump($_item->getHasChildren()) should return (bool)true for configurable products. $_item->getChildren() should also return an Magento\Quote\Model\Quote\Item\AbstractItem array. This screenshot shows it working correctly for a cart where pagination has not been triggered.

Actual result (*)

var_dump($_item->getHasChildren()) actually returns NULL for configurable products. $_item->getChildren() also returns an empty array. This screenshot is taken once enough products have been added to the cart to trigger pagination.


Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
@m2-assistant
Copy link

m2-assistant bot commented Nov 2, 2021

Hi @samcleathero. Thank you for your report.
To speed up processing of this issue, make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, review the Magento Contributor Assistant documentation.

Add a comment to assign the issue: @magento I am working on this

To learn more about issue processing workflow, refer to the Code Contributions.


⚠️ According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 You can find the schedule on the Magento Community Calendar page.

📞 The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

🎥 You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@samcleathero
Copy link
Author

samcleathero commented Nov 3, 2021

@magento give me 2.4-develop instance with extensions samcleathero/m2-item-render-test

Edit: this doesn't seem to have worked as I thought it would - hopefully somebody can take a look and figure out what I was trying to achieve!

@magento-deployment-service
Copy link

Hi @samcleathero. Thank you for your request. I'm working on Magento instance for you.

@engcom-Hotel engcom-Hotel self-assigned this Nov 9, 2021
@m2-assistant
Copy link

m2-assistant bot commented Nov 9, 2021

Hi @engcom-Hotel. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 5. Add label Issue: Confirmed once verification is complete.

  • 6. Make sure that automatic system confirms that report has been added to the backlog.

@pikulsky
Copy link

The bug is in vendor/magento/module-checkout/Block/Cart/Grid.php.
See \Magento\Checkout\Block\Cart\Grid::getItems(), let's say the quote includes only configurable products:

  1. if no pagination: parent::getItems() returns $this->getQuote()->getAllVisibleItems(), where
  • first, all quote items are loaded: configurable (parent_item_id is null) and child simples (parent_item_id is not null)
  • note, in \Magento\Quote\Model\ResourceModel\Quote\Item\Collection::_afterLoad() child quote items are assigned to the parent quote items
  • then in getAllVisibleItems() all children are not included in the result array, but child quote items are still linked to the parents - items in the result.
  1. if there is a pagination:
  • getItemsForGrid() is used, and it builds a query originally without children:
    $itemCollection->addFieldToFilter('parent_item_id', ['null' => true]);
  • This condition is important here since the correct page should be retrieved
  • Since the current page is loaded by SQL means (OFFSET, LIMIT), that's why non-visible quote items should be skipped in SQL query (see \Magento\Checkout\Block\Cart\Grid::_prepareLayout(), $pager in setCollection() sets current page number and page size)
  • Then the children are not linked to their parents afterwards, and as result - there is a bug.

As a possible solution I would suggest to add a custom quote items collection for pagination case.
It will be returned in getItemsForGrid().
That collection in its _afterLoad() loads again all quote items (or children only), and attaches children to parents.

@engcom-Hotel
Copy link
Contributor

Hi @samcleathero,

Thank you for reporting the issue.

We are successfully able to reproduce it on Magento 2.4-develop.

Steps followed to reproduce issue:

  1. Added configurable and non-configurable products to the cart.

  2. Print the $_item->getHasChildren() property on the $_items.

  3. Observe the function output with pagination and without pagination for the cart.

  4. $_item->getHasChildren() returns true when the pagination is not there for the cart and returns null when the cart is with pagination. (See the attached screenshots)

Conclusion: Item property should not change before and after pagination applied to the cart items.

Screenshot 2021-11-09 at 5 50 22 PM

Screenshot 2021-11-09 at 5 52 11 PM

Thanks

@engcom-Hotel engcom-Hotel added Area: Catalog Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reported on 2.4.3-p1 Indicates original Magento version for the Issue report. Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch labels Nov 15, 2021
@github-jira-sync-bot
Copy link

✅ Jira issue https://jira.corp.magento.com/browse/AC-1735 is successfully created for this GitHub issue.

@m2-assistant
Copy link

m2-assistant bot commented Nov 15, 2021

✅ Confirmed by @engcom-Hotel. Thank you for verifying the issue.
Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

@engcom-Alfa engcom-Alfa added the Priority: P2 A defect with this priority could have functionality issues which are not to expectations. label Nov 16, 2021
@artskirk
Copy link

artskirk commented Jul 5, 2022

@magento I am working on this

@Gerke-W
Copy link

Gerke-W commented Apr 9, 2025

@magento,

I can confirm this is still an issue in 2.4.6, created a pull request to Magento with a solution.

Until this has been solved, you can use the following patch

Index: vendor/magento/module-checkout/Block/Cart/Grid.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/vendor/magento/module-checkout/Block/Cart/Grid.php b/vendor/magento/module-checkout/Block/Cart/Grid.php
--- a/vendor/magento/module-checkout/Block/Cart/Grid.php
+++ b/vendor/magento/module-checkout/Block/Cart/Grid.php
@@ -117,12 +117,41 @@
             $pager = $this->getLayout()->createBlock(\Magento\Theme\Block\Html\Pager::class);
             $pager->setAvailableLimit([$availableLimit => $availableLimit])->setCollection($itemsCollection);
             $this->setChild('pager', $pager);
+            $this->updateCollectionWithQuoteItems($itemsCollection);
             $itemsCollection->load();
             $this->prepareItemUrls();
         }
         return $this;
     }

+    /**
+     * @param \Magento\Quote\Model\ResourceModel\Quote\Item\Collection $itemsCollection
+     *
+     * @return \Magento\Quote\Model\ResourceModel\Quote\Item\Collection
+     */
+    private function updateCollectionWithQuoteItems(
+        \Magento\Quote\Model\ResourceModel\Quote\Item\Collection $itemsCollection
+    ) {
+        foreach ($itemsCollection->getItems() as $key => $collectionItem) {
+            $quote      = $this->getQuote();
+            $quoteItems = $quote->getItems();
+            foreach ($quoteItems as $quoteItem) {
+                if ($quoteItem->getSku() === $collectionItem->getSku()) {
+                    $itemsCollection->removeItemByKey($key);
+                    try {
+                        $itemsCollection->addItem($quoteItem);
+                    } catch (\Exception $e) {
+                        // No action, item id removed
+                    }
+
+                    break;
+                }
+            }
+        }
+
+        return $itemsCollection;
+    }
+
     /**
      * Prepare quote items collection for pager
      *

@engcom-November engcom-November added the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Catalog Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: PR in progress Reported on 2.4.3-p1 Indicates original Magento version for the Issue report. Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Projects
Status: Pull Request In Progress
8 participants