Skip to content

Admin shows thousands of cart items for customers #27564

@xtremevision

Description

@xtremevision

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions