Magento2: Backend cart in customer detail view

Created on 19 Feb 2020  路  13Comments  路  Source: magento/magento2

Preconditions (*)

  1. Magento 2.3.4
  2. PHP 7.2.x
  3. Multi website environment

Steps to reproduce (*)

  1. Setup a multi website environment
  2. Set customer/account_share/scope to 0 (SHARE_GLOBAL)
  3. create an customer on website > 1 (default frontend website)
  4. Add something to the cart on website > 1
  5. Now in the database should be an active cart for the store in website with id > 1 and no cart for the website with id 1
  6. Go to backend and open the customer account detail view
  7. Go to tab "Shopping cart"

Expected result (*)

  1. See the carts for the different websites

Actual result (*)

  1. A cart with all quote items of all carts because $this->getWebsiteId() in Magento\Customer\Block\Adminhtml\Edit\Tab\Cart returns NULL and $storeIds contains only store_id 1 in getQuote. But there is no quote for store id 1 and so $this->_items->setQuote($this); in function getItemsCollection of Magento\Quote\Model\Quote has no effect because there is no ID in the quote model.

Additional infos

I think this happens because on clicking the "Shopping Cart" button the controller Adminhtml/Index/Cart is called. But Adminhtml/Index/Carts handles the shared account config and create multiple grids with the correct website id.

cart

Customer Clear Description Confirmed Format is valid Ready for Work P3 PR in progress Reported on 2.3.4 Reproduced on 2.4.x S3 Done

Most helpful comment

I can confirm we are having this issue too. On Magento 2.3.3 Multi Website / Multi Storeview

Screenshot from Gyazo

All 13 comments

Hi @bernd-reindl. 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.

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

  • [ ] yes
  • [ ] no

@magento give me 2.4-develop instance

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

Hi @bernd-reindl, here is your Magento instance.
Admin access: https://i-26921-2-4-develop.instances.magento-community.engineering/admin_f5f1
Login: 71b4736a Password: 89854edda769
Instance will be terminated in up to 3 hours.

Additionaly if a customer has products in his quote with store_id > 1 this cart is not shown in backend.

See Customer 2 in instance https://i-26921-2-4-develop.instances.magento-community.engineering/admin_f5f1. This customer has products in quote with store_id 2. But the shopping cart Tab shows only an empty cart.

cart

order

Already argued with the Enterprise support about it. Debugging clearly shows it cannot work for a multiwebsite/store setup with shared customers.

Only one active cart could be handled by the code, but it actually tries to load the quote for store id 0, for which no active quote exists, so in the aftermath getAllItems() is called on a mostly unfiltered quote item collection and returns all quote items given in the database.

I have solved it by three modifications.

  1. Edit Magento\Sales\Block\Adminhtml\ShoppingCartsTab -> getTabUrl()

return $this->getUrl('customer/*/carts', ['_current' => true]);
not
return $this->getUrl('customer/*/cart', ['_current' => true]);

customer/*/cart(s) handle all store_ids

I also think that ShoppingCartsTab should be part of module-quote not module-sales.

  1. Magento\Customer\Block\Adminhtml\Edit\Tab\Cart -> getQuote()

$this->quote = $this->quoteFactory->create()->setId(-1)->setSharedStoreIds($storeIds);
not
$this->quote = $this->quoteFactory->create()->setSharedStoreIds($storeIds);

If getId() return null, $quote->getItemsCollection(false) returns a collection with no quote filter and all QuoteItems are shown. See _prepareCollection()

The if block in _prepareCollection() is not required, because $this->getQuote() allways return a qoute.

  1. Magento\Quote\Model\QuoteRepository -> getForCustomer()

If the cache key is only the customerId, and the function was called with diferent sharedStoreIds, always the quote with the first requested storeIds is returned.
So the cache key has to contain the sharedStoreIds.

E.G.

$cacheKey = $customerId.'-'.implode('-', $sharedStoreIds);
if (!isset($this->quotesByCustomerId[$cacheKey])) {
    $quote = $this->loadQuote('loadByCustomer', 'customerId', $customerId, $sharedStoreIds);
    $this->getLoadHandler()->load($quote);
    $this->quotesById[$quote->getId()] = $quote;
    $this->quotesByCustomerId[$cacheKey] = $quote;
}
return $this->quotesByCustomerId[$cacheKey];

I can confirm we are having this issue too. On Magento 2.3.3 Multi Website / Multi Storeview

Screenshot from Gyazo

Hi @engcom-Bravo. 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.

Hello @bernd-reindl

Thank you for your report

I have rechecked this issue on 2.4-develop, and I'm getting the same result you've mentioned in this screenshot, no cart at all.
This looks like another issue. But what about the initial one? If I understood your comment correctly, you were not able to reproduce the initial issue on 2.4-develop, is that correct?

Thank you in advance

Hello @bernd-reindl

Thank you for your report

I have rechecked this issue on 2.4-develop, and I'm getting the same result you've mentioned in this screenshot, no cart at all.
This looks like another issue. But what about the initial one? If I understood your comment correctly, you were not able to reproduce the initial issue on 2.4-develop, is that correct?

Thank you in advance

Of course i can reproduce it on 2.4-dev.
And i already have solved the problem. See my comment from 20. February, 11:17 MEZ.

:white_check_mark: Confirmed by @engcom-Bravo
Thank you for verifying the issue. Based on the provided information internal tickets MC-32168 were created

Issue Available: @engcom-Bravo, _You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself._

I have also faced same issue. But most easy and quick solution is overriding getQuote() function as below :

protected function getQuote()
{
if (null === $this->quote) {
$customerId = $this->getCustomerId();
$customer = $this->_customerRepository->getById($customerId);
$storeIds = $this->_storeManager->getWebsite($customer->getWebsiteId())->getStoreIds();
try {
$this->quote = $this->quoteFactory->create()->setSharedStoreIds($storeIds)->loadByCustomer($customerId);
} catch (NoSuchEntityException $e) {
$this->quote = $this->quoteFactory->create()->setSharedStoreIds($storeIds);
}
}
return $this->quote;
}

This issue was coming due to customer website is not getting correctly from backend and so the store Id.

Try this out!!!!

Was this page helpful?
0 / 5 - 0 ratings