Magento2: Admin shows thousands of cart items for customers

Created on 3 Apr 2020  路  22Comments  路  Source: magento/magento2

Preconditions (*)

  1. Centos OS 7 x64
  2. Magento 2.3.3
  3. PHP 7.2

Steps to reproduce (*)

  1. Create lots of carts / quotes, both as logged in user and guest
  2. Keep one customer without any quotes
  3. Goto backend, edit customer, click on Shopping Cart tab
  4. Grid shows thousand of items

Expected result (*)

  1. Shopping cart tab should show customer's cart items if available, or none if no carts available for customer

Actual result (*)

I am experiencing this issue in the backend admin, in the customer record, Shopping Cart tab. It looks like the getQuote() function returns all cart items (without a parent id), if no active quote is found for customer id [X].

See screenshot:

2020-04-02 21 40 21

This getQuote() function is at fault from what I can tell.

2020-04-02 21 42 05

The quoteRepository->getForCustomer() function throws the exception as shown here:

2020-04-02 21 45 26

The trouble is if no quote is found, then _prepareCollection() will return all quote_items regardless of quote id, regardless of customer_id, as shown by this query:

SELECT main_table.* FROM quote_item AS main_table WHERE (parent_item_id IS NULL)

I fixed in by adding this check:

protected function _prepareCollection()
{
$quote = $this->getQuote();

    if ($quote) {
        $collection = $quote->getItemsCollection(false);
    } else {
        $collection = $this->_dataCollectionFactory->create();
    }

    $collection->addFieldToFilter('parent_item_id', ['null' => true]);

    /**** Added by me as temporary fix ***/
    if(is_null($quote->getId()))
        $collection->addFieldToFilter('quote_id', -1);
    /**** Added by me as temporary fix ***/

    $this->setCollection($collection);

    return parent::_prepareCollection();
}

It probably isn't the best or most elegant fix, but it works, or so it seems.
Does anyone have a better idea?

Thanks,
Michael Mussulis

Global-Contribution-Day ready for confirmation Reported on 2.3.3

Most helpful comment

No, I don't agree. This is has nothing to do with a my custom extension. Why did you close this? The module I used was just to prove the fix. Please reopen.

All 22 comments

Hi @xtremevision. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

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

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

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

For more details, please, review the Magento Contributor Assistant documentation.

@xtremevision do you confirm that you were able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • [ ] yes
  • [ ] no

Hi @krishprakash. 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:

  • [ ] 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.

@magento give me 2.4-develop instance

Hi @krishprakash. Thank you for your request. I'm working on Magento 2.4-develop instance for you

Hi @krishprakash, here is your Magento instance.
Admin access: https://i-27564-2-4-develop.instances.magento-community.engineering/admin_cbb5
Login: 043c35ee Password: f3e2f039d232
Instance will be terminated in up to 3 hours.

Hi @xtremevision. Thank you for your report.
Not able to Reproduce the issue on latest magento 2.3.4 instance
Kindly provide more information
Also please confirm the issue on latest 2.3.4 instance
Thanks !!

Hi @krishprakash,

Did you create lots of quotes? What does your quote table look like?

Thanks,
Michael.

Hi @xtremevision. Yes created many quotes for login and guest user as well
We can not see quote table for provided instance.
So if possible please share the video with steps you mentioned so will review in local instance
Thank You !!

Hi @krishprakash ,

I've got it figured out. The quote table was missing records for customers. This was causing the bad view in the admin, shopping cart tab.

I used a module called Login as Customer, to login as the customer, and this resulted in an empty quote record being created for that customer. Checking in the backend in the Shopping Cart tab now does not show any cart items for the customer.

It seems to me that Magento's logic is that each customer MUST have at least 1 empty cart in order for the Shopping Cart grid to function properly, and to avoid the "No such entity with customerId = XXX" exception.

I personally think this logic is somewhat flawed, and that even if there's no quote for a customer, the exception should be thrown and the cart should not show an items.

@xtremevision. Then this is the compatibility issue with your custom extension not the default magento issue

As this compatibility issue with your custom extension so closing this
Thanks !!

No, I don't agree. This is has nothing to do with a my custom extension. Why did you close this? The module I used was just to prove the fix. Please reopen.

I have same issue on 2.3.3-p1 migrated from 1.9.3.1. Thank you @xtremevision for you quick fix.

Same issue here

@xtremevision could you provide some more information about your debug and what seems to be the issue with "empty carts"?

Are you talking about the result of:

SELECT customer_entity.entity_id FROM customer_entity LEFT JOIN quote ON quote.customer_id = customer_entity.entity_id WHERE quote.entity_id IS NULL;

An confirm, issue is there on 2.3.5-p1

I had to tweak @xtremevision's manual fix a tad to for my particular version (2.3.3).

di.xml

    <!--  /** @see    https://github.com/magento/magento2/issues/27564 */ -->
    <preference for="Magento\Customer\Block\Adminhtml\Edit\Tab\Cart" type="Convert\CustomerContent\Block\Adminhtml\Edit\Tab\Cart" />

app/code/MyVendor/MyModule/Block/Adminhtml/Edit/Tab/Cart.php

/**
 * @author  YOUR NAME
 * @since   2020-08-13 (Magento 2.3.3) <1.0.0>
 * @see     https://github.com/magento/magento2/issues/27564 
 */

namespace MyVendor\MyModule\Block\Adminhtml\Edit\Tab;

use Magento\Customer\Block\Adminhtml\Edit\Tab\Cart as Original;

class Cart extends Original
{
    protected function _prepareCollection()
    {
        parent::_prepareCollection();

        $quote = $this->getQuote();
        if ($quote) {
            $collection = $quote->getItemsCollection(false);
        } else {
            $collection = $this->_dataCollectionFactory->create();
        }

        $collection->addFieldToFilter('parent_item_id', ['null' => true]);

        /** BEGIN: fix */
        if (is_null($quote->getId()))
            $collection->addFieldToFilter('quote_id', -1);
        /** END: fix */

        $this->setCollection($collection);

        return $this;
    }
}

We see this as well, Magento 2.3.5-p2

same here, same issue in 2.3.5-p2

Just confirmed what xtremevision said, if customer doesn't have a valid open cart in the quote table it will list everything that is open. I check two new account opened one did have an open cart in the quote table, worked as it should. No items listed. Looked at another customer, not open quotes and listed everything.
Rich

FYI: This is fixed in Magento 2.4 and up...

Magento backend will alsways return a quote(even a empty one)
Using this items collection will not apply the quoteId as a filter when searching/loading cart items resulting in loading all cart items for any customer.

In 2.4 the ->getId() is applied again and the a empty collection is returned, which is indeed the correct behavior.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PascalBrouwers picture PascalBrouwers  路  3Comments

BenSpace48 picture BenSpace48  路  3Comments

PushEngineering picture PushEngineering  路  3Comments

denis-g picture denis-g  路  3Comments

andreaskoch picture andreaskoch  路  3Comments