QuoteManager.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Persistent\Model;
  7. /**
  8. * Class QuoteManager
  9. */
  10. class QuoteManager
  11. {
  12. /**
  13. * Persistent session
  14. *
  15. * @var \Magento\Persistent\Helper\Session
  16. */
  17. protected $persistentSession;
  18. /**
  19. * Checkout session
  20. *
  21. * @var \Magento\Checkout\Model\Session
  22. */
  23. protected $checkoutSession;
  24. /**
  25. * Persistent data
  26. *
  27. * @var \Magento\Persistent\Helper\Data
  28. */
  29. protected $persistentData;
  30. /**
  31. * Whether set quote to be persistent in workflow
  32. *
  33. * @var bool
  34. */
  35. protected $_setQuotePersistent = true;
  36. /**
  37. * @var \Magento\Quote\Api\CartRepositoryInterface
  38. */
  39. protected $quoteRepository;
  40. /**
  41. * @param \Magento\Persistent\Helper\Session $persistentSession
  42. * @param \Magento\Persistent\Helper\Data $persistentData
  43. * @param \Magento\Checkout\Model\Session $checkoutSession
  44. * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
  45. */
  46. public function __construct(
  47. \Magento\Persistent\Helper\Session $persistentSession,
  48. \Magento\Persistent\Helper\Data $persistentData,
  49. \Magento\Checkout\Model\Session $checkoutSession,
  50. \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
  51. ) {
  52. $this->persistentSession = $persistentSession;
  53. $this->persistentData = $persistentData;
  54. $this->checkoutSession = $checkoutSession;
  55. $this->quoteRepository = $quoteRepository;
  56. }
  57. /**
  58. * Clear cart of customer data if exists and reset guest information, remove persistent session
  59. *
  60. * @param bool $checkQuote Check quote to be persistent (not stolen)
  61. * @return void
  62. */
  63. public function setGuest($checkQuote = false)
  64. {
  65. /** @var $quote \Magento\Quote\Model\Quote */
  66. $quote = $this->checkoutSession->getQuote();
  67. if ($quote && $quote->getId()) {
  68. if ($checkQuote && !$this->persistentData->isShoppingCartPersist() && !$quote->getIsPersistent()) {
  69. $this->checkoutSession->clearQuote()->clearStorage();
  70. return;
  71. }
  72. $quote->getPaymentsCollection()->walk('delete');
  73. $quote->getAddressesCollection()->walk('delete');
  74. $this->_setQuotePersistent = false;
  75. $quote->setIsActive(true)
  76. ->setCustomerId(null)
  77. ->setCustomerEmail(null)
  78. ->setCustomerFirstname(null)
  79. ->setCustomerLastname(null)
  80. ->setCustomerGroupId(\Magento\Customer\Api\Data\GroupInterface::NOT_LOGGED_IN_ID)
  81. ->setIsPersistent(false)
  82. ->removeAllAddresses();
  83. //Create guest addresses
  84. $quote->getShippingAddress();
  85. $quote->getBillingAddress();
  86. $quote->collectTotals();
  87. $this->quoteRepository->save($quote);
  88. }
  89. $this->persistentSession->getSession()->removePersistentCookie();
  90. }
  91. /**
  92. * Emulate guest cart with persistent cart
  93. *
  94. * Converts persistent cart tied to logged out customer to a guest cart, retaining customer information required for
  95. * checkout
  96. *
  97. * @return void
  98. */
  99. public function convertCustomerCartToGuest()
  100. {
  101. $quoteId = $this->checkoutSession->getQuoteId();
  102. /** @var $quote \Magento\Quote\Model\Quote */
  103. $quote = $this->quoteRepository->get($quoteId);
  104. if ($quote && $quote->getId()) {
  105. $this->_setQuotePersistent = false;
  106. $quote->setIsActive(true)
  107. ->setCustomerId(null)
  108. ->setCustomerEmail(null)
  109. ->setCustomerFirstname(null)
  110. ->setCustomerLastname(null)
  111. ->setCustomerGroupId(\Magento\Customer\Api\Data\GroupInterface::NOT_LOGGED_IN_ID)
  112. ->setIsPersistent(false);
  113. $quote->getAddressesCollection()->walk('setCustomerAddressId', ['customerAddressId' => null]);
  114. $quote->getAddressesCollection()->walk('setCustomerId', ['customerId' => null]);
  115. $quote->getAddressesCollection()->walk('setEmail', ['email' => null]);
  116. $quote->collectTotals();
  117. $this->persistentSession->getSession()->removePersistentCookie();
  118. $this->quoteRepository->save($quote);
  119. }
  120. }
  121. /**
  122. * Expire persistent quote
  123. *
  124. * @return void
  125. */
  126. public function expire()
  127. {
  128. $quote = $this->checkoutSession->setLoadInactive()->getQuote();
  129. if ($quote->getIsActive() && $quote->getCustomerId()) {
  130. $this->checkoutSession->setCustomerData(null)->clearQuote()->clearStorage();
  131. } else {
  132. $quote->setIsActive(true)
  133. ->setIsPersistent(false)
  134. ->setCustomerId(null)
  135. ->setCustomerGroupId(\Magento\Customer\Api\Data\GroupInterface::NOT_LOGGED_IN_ID);
  136. }
  137. }
  138. /**
  139. * Is quote persistent
  140. *
  141. * @return bool
  142. */
  143. public function isPersistent()
  144. {
  145. return $this->_setQuotePersistent;
  146. }
  147. }