|
7 | 7 |
|
8 | 8 | use Magento\Customer\Api\Data\CustomerInterface;
|
9 | 9 | use Magento\Framework\App\ObjectManager;
|
| 10 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 11 | +use Magento\Quote\Api\Data\CartInterface; |
10 | 12 | use Magento\Quote\Model\Quote;
|
11 | 13 | use Magento\Quote\Model\QuoteIdMaskFactory;
|
12 | 14 | use Psr\Log\LoggerInterface;
|
|
21 | 23 | */
|
22 | 24 | class Session extends \Magento\Framework\Session\SessionManager
|
23 | 25 | {
|
24 |
| - /** |
25 |
| - * Checkout state begin |
26 |
| - */ |
27 | 26 | const CHECKOUT_STATE_BEGIN = 'begin';
|
28 | 27 |
|
29 | 28 | /**
|
@@ -228,7 +227,7 @@ public function setLoadInactive($load = true)
|
228 | 227 | *
|
229 | 228 | * @return Quote
|
230 | 229 | * @throws \Magento\Framework\Exception\LocalizedException
|
231 |
| - * @throws \Magento\Framework\Exception\NoSuchEntityException |
| 230 | + * @throws NoSuchEntityException |
232 | 231 | * @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
233 | 232 | * @SuppressWarnings(PHPMD.NPathComplexity)
|
234 | 233 | */
|
@@ -273,21 +272,17 @@ public function getQuote()
|
273 | 272 | */
|
274 | 273 | $quote = $this->quoteRepository->get($this->getQuoteId());
|
275 | 274 | }
|
276 |
| - } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { |
| 275 | + } catch (NoSuchEntityException $e) { |
277 | 276 | $this->setQuoteId(null);
|
278 | 277 | }
|
279 | 278 | }
|
280 | 279 |
|
281 | 280 | if (!$this->getQuoteId()) {
|
282 | 281 | if ($this->_customerSession->isLoggedIn() || $this->_customer) {
|
283 |
| - $customerId = $this->_customer |
284 |
| - ? $this->_customer->getId() |
285 |
| - : $this->_customerSession->getCustomerId(); |
286 |
| - try { |
287 |
| - $quote = $this->quoteRepository->getActiveForCustomer($customerId); |
288 |
| - $this->setQuoteId($quote->getId()); |
289 |
| - } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { |
290 |
| - $this->logger->critical($e); |
| 282 | + $quoteByCustomer = $this->getQuoteByCustomer(); |
| 283 | + if ($quoteByCustomer !== null) { |
| 284 | + $this->setQuoteId($quoteByCustomer->getId()); |
| 285 | + $quote = $quoteByCustomer; |
291 | 286 | }
|
292 | 287 | } else {
|
293 | 288 | $quote->setIsCheckoutCart(true);
|
@@ -375,7 +370,7 @@ public function loadCustomerQuote()
|
375 | 370 |
|
376 | 371 | try {
|
377 | 372 | $customerQuote = $this->quoteRepository->getForCustomer($this->_customerSession->getCustomerId());
|
378 |
| - } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { |
| 373 | + } catch (NoSuchEntityException $e) { |
379 | 374 | $customerQuote = $this->quoteFactory->create();
|
380 | 375 | }
|
381 | 376 | $customerQuote->setStoreId($this->_storeManager->getStore()->getId());
|
@@ -558,7 +553,7 @@ public function restoreQuote()
|
558 | 553 | $this->replaceQuote($quote)->unsLastRealOrderId();
|
559 | 554 | $this->_eventManager->dispatch('restore_quote', ['order' => $order, 'quote' => $quote]);
|
560 | 555 | return true;
|
561 |
| - } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { |
| 556 | + } catch (NoSuchEntityException $e) { |
562 | 557 | $this->logger->critical($e);
|
563 | 558 | }
|
564 | 559 | }
|
@@ -588,4 +583,22 @@ protected function isQuoteMasked()
|
588 | 583 | {
|
589 | 584 | return $this->isQuoteMasked;
|
590 | 585 | }
|
| 586 | + |
| 587 | + /** |
| 588 | + * Returns quote for customer if there is any |
| 589 | + */ |
| 590 | + private function getQuoteByCustomer(): ?CartInterface |
| 591 | + { |
| 592 | + $customerId = $this->_customer |
| 593 | + ? $this->_customer->getId() |
| 594 | + : $this->_customerSession->getCustomerId(); |
| 595 | + |
| 596 | + try { |
| 597 | + $quote = $this->quoteRepository->getActiveForCustomer($customerId); |
| 598 | + } catch (NoSuchEntityException $e) { |
| 599 | + $quote = null; |
| 600 | + } |
| 601 | + |
| 602 | + return $quote; |
| 603 | + } |
591 | 604 | }
|
0 commit comments