Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
QuoteManager.php
Go to the documentation of this file.
1 <?php
7 
12 {
18  protected $persistentSession;
19 
25  protected $checkoutSession;
26 
32  protected $persistentData;
33 
39  protected $_setQuotePersistent = true;
40 
44  protected $quoteRepository;
45 
52  public function __construct(
53  \Magento\Persistent\Helper\Session $persistentSession,
54  \Magento\Persistent\Helper\Data $persistentData,
55  \Magento\Checkout\Model\Session $checkoutSession,
56  \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
57  ) {
58  $this->persistentSession = $persistentSession;
59  $this->persistentData = $persistentData;
60  $this->checkoutSession = $checkoutSession;
61  $this->quoteRepository = $quoteRepository;
62  }
63 
70  public function setGuest($checkQuote = false)
71  {
73  $quote = $this->checkoutSession->getQuote();
74  if ($quote && $quote->getId()) {
75  if ($checkQuote && !$this->persistentData->isShoppingCartPersist() && !$quote->getIsPersistent()) {
76  $this->checkoutSession->clearQuote()->clearStorage();
77  return;
78  }
79 
80  $quote->getPaymentsCollection()->walk('delete');
81  $quote->getAddressesCollection()->walk('delete');
82  $this->_setQuotePersistent = false;
83  $quote->setIsActive(true)
84  ->setCustomerId(null)
85  ->setCustomerEmail(null)
86  ->setCustomerFirstname(null)
87  ->setCustomerLastname(null)
88  ->setCustomerGroupId(\Magento\Customer\Api\Data\GroupInterface::NOT_LOGGED_IN_ID)
89  ->setIsPersistent(false)
90  ->removeAllAddresses();
91  //Create guest addresses
92  $quote->getShippingAddress();
93  $quote->getBillingAddress();
94  $quote->collectTotals();
95  $this->quoteRepository->save($quote);
96  }
97 
98  $this->persistentSession->getSession()->removePersistentCookie();
99  }
100 
109  public function convertCustomerCartToGuest()
110  {
112  $quote = $this->quoteRepository->get($this->checkoutSession->getQuoteId());
113  if ($quote && $quote->getId()) {
114  $this->_setQuotePersistent = false;
115  $quote->setIsActive(true)
116  ->setCustomerId(null)
117  ->setCustomerEmail(null)
118  ->setCustomerFirstname(null)
119  ->setCustomerLastname(null)
120  ->setCustomerGroupId(\Magento\Customer\Api\Data\GroupInterface::NOT_LOGGED_IN_ID)
121  ->setIsPersistent(false);
122  $quote->getAddressesCollection()->walk('setCustomerAddressId', ['customerAddressId' => null]);
123  $quote->getAddressesCollection()->walk('setCustomerId', ['customerId' => null]);
124  $quote->getAddressesCollection()->walk('setEmail', ['email' => null]);
125  $quote->collectTotals();
126  $this->persistentSession->getSession()->removePersistentCookie();
127  $this->quoteRepository->save($quote);
128  }
129  }
130 
136  public function expire()
137  {
138  $quote = $this->checkoutSession->setLoadInactive()->getQuote();
139  if ($quote->getIsActive() && $quote->getCustomerId()) {
140  $this->checkoutSession->setCustomerData(null)->clearQuote()->clearStorage();
141  } else {
142  $quote->setIsActive(true)
143  ->setIsPersistent(false)
144  ->setCustomerId(null)
145  ->setCustomerGroupId(\Magento\Customer\Api\Data\GroupInterface::NOT_LOGGED_IN_ID);
146  }
147  }
148 
154  public function isPersistent()
155  {
157  }
158 }
$quote
__construct(\Magento\Persistent\Helper\Session $persistentSession, \Magento\Persistent\Helper\Data $persistentData, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Quote\Api\CartRepositoryInterface $quoteRepository)