Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Quote.php
Go to the documentation of this file.
1 <?php
7 
10 
30 {
36  protected $_quote;
37 
43  protected $_store;
44 
50  protected $_order;
51 
55  protected $_orderFactory;
56 
61 
67  protected $quoteRepository;
68 
72  protected $_storeManager;
73 
77  protected $groupManagement;
78 
82  protected $quoteFactory;
83 
102  public function __construct(
103  \Magento\Framework\App\Request\Http $request,
104  \Magento\Framework\Session\SidResolverInterface $sidResolver,
106  \Magento\Framework\Session\SaveHandlerInterface $saveHandler,
108  \Magento\Framework\Session\StorageInterface $storage,
109  \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
110  \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
111  \Magento\Framework\App\State $appState,
113  \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
114  \Magento\Sales\Model\OrderFactory $orderFactory,
115  \Magento\Store\Model\StoreManagerInterface $storeManager,
117  \Magento\Quote\Model\QuoteFactory $quoteFactory
118  ) {
119  $this->customerRepository = $customerRepository;
120  $this->quoteRepository = $quoteRepository;
121  $this->_orderFactory = $orderFactory;
122  $this->_storeManager = $storeManager;
123  $this->groupManagement = $groupManagement;
124  $this->quoteFactory = $quoteFactory;
125  parent::__construct(
126  $request,
127  $sidResolver,
129  $saveHandler,
130  $validator,
131  $storage,
134  $appState
135  );
136  if ($this->_storeManager->hasSingleStore()) {
137  $this->setStoreId($this->_storeManager->getStore(true)->getId());
138  }
139  }
140 
146  public function getQuote()
147  {
148  if ($this->_quote === null) {
149  $this->_quote = $this->quoteFactory->create();
150  if ($this->getStoreId()) {
151  if (!$this->getQuoteId()) {
152  $this->_quote->setCustomerGroupId($this->groupManagement->getDefaultGroup()->getId());
153  $this->_quote->setIsActive(false);
154  $this->_quote->setStoreId($this->getStoreId());
155 
156  $this->quoteRepository->save($this->_quote);
157  $this->setQuoteId($this->_quote->getId());
158  $this->_quote = $this->quoteRepository->get($this->getQuoteId(), [$this->getStoreId()]);
159  } else {
160  $this->_quote = $this->quoteRepository->get($this->getQuoteId(), [$this->getStoreId()]);
161  $this->_quote->setStoreId($this->getStoreId());
162  }
163 
164  if ($this->getCustomerId() && $this->getCustomerId() != $this->_quote->getCustomerId()) {
165  $customer = $this->customerRepository->getById($this->getCustomerId());
166  $this->_quote->assignCustomer($customer);
167  $this->quoteRepository->save($this->_quote);
168  }
169  }
170  $this->_quote->setIgnoreOldQty(true);
171  $this->_quote->setIsSuperMode(true);
172  }
173 
174  return $this->_quote;
175  }
176 
182  public function getStore()
183  {
184  if ($this->_store === null) {
185  $this->_store = $this->_storeManager->getStore($this->getStoreId());
186  $currencyId = $this->getCurrencyId();
187  if ($currencyId) {
188  $this->_store->setCurrentCurrencyCode($currencyId);
189  }
190  }
191  return $this->_store;
192  }
193 
199  public function getOrder()
200  {
201  if ($this->_order === null) {
202  $this->_order = $this->_orderFactory->create();
203  if ($this->getOrderId()) {
204  $this->_order->load($this->getOrderId());
205  }
206  }
207  return $this->_order;
208  }
209 }
$customer
Definition: customers.php:11
$storeManager
__construct(\Magento\Framework\App\Request\Http $request, \Magento\Framework\Session\SidResolverInterface $sidResolver, \Magento\Framework\Session\Config\ConfigInterface $sessionConfig, \Magento\Framework\Session\SaveHandlerInterface $saveHandler, \Magento\Framework\Session\ValidatorInterface $validator, \Magento\Framework\Session\StorageInterface $storage, \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager, \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory, \Magento\Framework\App\State $appState, CustomerRepositoryInterface $customerRepository, \Magento\Quote\Api\CartRepositoryInterface $quoteRepository, \Magento\Sales\Model\OrderFactory $orderFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, GroupManagementInterface $groupManagement, \Magento\Quote\Model\QuoteFactory $quoteFactory)
Definition: Quote.php:102