magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.48k stars 9.29k forks source link

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

Open samcleathero opened 2 years ago

samcleathero commented 2 years ago

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.

m2-assistant[bot] commented 2 years ago

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

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.


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: 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.

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

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

samcleathero commented 2 years ago

@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[bot] commented 2 years ago

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

magento-deployment-service[bot] commented 2 years ago

Hi @samcleathero, here is your Magento Instance: https://b26a589b00d5c9d782886b45dab65007-2-4-develop.instances.magento-community.engineering Admin access: https://b26a589b00d5c9d782886b45dab65007-2-4-develop.instances.magento-community.engineering/admin_06f7 Login: b627fb60 Password: ee43a6abf3bd

m2-assistant[bot] commented 2 years ago

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: :point_down:

pikulsky commented 2 years ago

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.
  2. 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 commented 2 years ago

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

github-jira-sync-bot commented 2 years ago

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

m2-assistant[bot] commented 2 years ago

:white_check_mark: 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.

artskirk commented 2 years ago

@magento I am working on this