Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
QuoteRepository.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Quote\Model;
7 
17 use Magento\Quote\Model\ResourceModel\Quote\CollectionFactory as QuoteCollectionFactory;
22 
27 {
31  protected $quotesById = [];
32 
36  protected $quotesByCustomerId = [];
37 
41  protected $quoteFactory;
42 
46  protected $storeManager;
47 
52  protected $quoteCollection;
53 
58 
62  private $extensionAttributesJoinProcessor;
63 
67  private $saveHandler;
68 
72  private $loadHandler;
73 
77  private $collectionProcessor;
78 
82  private $quoteCollectionFactory;
83 
96  public function __construct(
97  QuoteFactory $quoteFactory,
100  \Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory $searchResultsDataFactory,
101  JoinProcessorInterface $extensionAttributesJoinProcessor,
102  CollectionProcessorInterface $collectionProcessor = null,
103  \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory $quoteCollectionFactory = null
104  ) {
105  $this->quoteFactory = $quoteFactory;
106  $this->storeManager = $storeManager;
107  $this->searchResultsDataFactory = $searchResultsDataFactory;
108  $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
109  $this->collectionProcessor = $collectionProcessor ?: \Magento\Framework\App\ObjectManager::getInstance()
110  ->get(\Magento\Framework\Api\SearchCriteria\CollectionProcessor::class);
111  $this->quoteCollectionFactory = $quoteCollectionFactory ?: \Magento\Framework\App\ObjectManager::getInstance()
112  ->get(\Magento\Quote\Model\ResourceModel\Quote\CollectionFactory::class);
113  }
114 
118  public function get($cartId, array $sharedStoreIds = [])
119  {
120  if (!isset($this->quotesById[$cartId])) {
121  $quote = $this->loadQuote('loadByIdWithoutStore', 'cartId', $cartId, $sharedStoreIds);
122  $this->getLoadHandler()->load($quote);
123  $this->quotesById[$cartId] = $quote;
124  }
125  return $this->quotesById[$cartId];
126  }
127 
131  public function getForCustomer($customerId, array $sharedStoreIds = [])
132  {
133  if (!isset($this->quotesByCustomerId[$customerId])) {
134  $quote = $this->loadQuote('loadByCustomer', 'customerId', $customerId, $sharedStoreIds);
135  $this->getLoadHandler()->load($quote);
136  $this->quotesById[$quote->getId()] = $quote;
137  $this->quotesByCustomerId[$customerId] = $quote;
138  }
139  return $this->quotesByCustomerId[$customerId];
140  }
141 
145  public function getActive($cartId, array $sharedStoreIds = [])
146  {
147  $quote = $this->get($cartId, $sharedStoreIds);
148  if (!$quote->getIsActive()) {
150  }
151  return $quote;
152  }
153 
157  public function getActiveForCustomer($customerId, array $sharedStoreIds = [])
158  {
159  $quote = $this->getForCustomer($customerId, $sharedStoreIds);
160  if (!$quote->getIsActive()) {
161  throw NoSuchEntityException::singleField('customerId', $customerId);
162  }
163  return $quote;
164  }
165 
169  public function save(\Magento\Quote\Api\Data\CartInterface $quote)
170  {
171  if ($quote->getId()) {
172  $currentQuote = $this->get($quote->getId(), [$quote->getStoreId()]);
173 
174  foreach ($currentQuote->getData() as $key => $value) {
175  if (!$quote->hasData($key)) {
176  $quote->setData($key, $value);
177  }
178  }
179  }
180 
181  $this->getSaveHandler()->save($quote);
182  unset($this->quotesById[$quote->getId()]);
183  unset($this->quotesByCustomerId[$quote->getCustomerId()]);
184  }
185 
190  {
191  $quoteId = $quote->getId();
192  $customerId = $quote->getCustomerId();
193  $quote->delete();
194  unset($this->quotesById[$quoteId]);
195  unset($this->quotesByCustomerId[$customerId]);
196  }
197 
208  protected function loadQuote($loadMethod, $loadField, $identifier, array $sharedStoreIds = [])
209  {
211  $quote = $this->quoteFactory->create();
212  if ($sharedStoreIds) {
213  $quote->setSharedStoreIds($sharedStoreIds);
214  }
215  $quote->setStoreId($this->storeManager->getStore()->getId())->$loadMethod($identifier);
216  if (!$quote->getId()) {
217  throw NoSuchEntityException::singleField($loadField, $identifier);
218  }
219  return $quote;
220  }
221 
225  public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
226  {
227  $this->quoteCollection = $this->quoteCollectionFactory->create();
229  $searchData = $this->searchResultsDataFactory->create();
230  $searchData->setSearchCriteria($searchCriteria);
231 
232  $this->collectionProcessor->process($searchCriteria, $this->quoteCollection);
233  $this->extensionAttributesJoinProcessor->process($this->quoteCollection);
234  foreach ($this->quoteCollection->getItems() as $quote) {
236  $this->getLoadHandler()->load($quote);
237  }
238  $searchData->setItems($this->quoteCollection->getItems());
239  $searchData->setTotalCount($this->quoteCollection->getSize());
240  return $searchData;
241  }
242 
252  protected function addFilterGroupToCollection(FilterGroup $filterGroup, QuoteCollection $collection)
253  {
254  $fields = [];
255  $conditions = [];
256  foreach ($filterGroup->getFilters() as $filter) {
257  $fields[] = $filter->getField();
258  $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
259  $conditions[] = [$condition => $filter->getValue()];
260  }
261  if ($fields) {
262  $collection->addFieldToFilter($fields, $conditions);
263  }
264  }
265 
271  private function getSaveHandler()
272  {
273  if (!$this->saveHandler) {
274  $this->saveHandler = ObjectManager::getInstance()->get(SaveHandler::class);
275  }
276  return $this->saveHandler;
277  }
278 
283  private function getLoadHandler()
284  {
285  if (!$this->loadHandler) {
286  $this->loadHandler = ObjectManager::getInstance()->get(LoadHandler::class);
287  }
288  return $this->loadHandler;
289  }
290 }
$quote
$fields
Definition: details.phtml:14
getForCustomer($customerId, array $sharedStoreIds=[])
$searchCriteria
save(\Magento\Quote\Api\Data\CartInterface $quote)
getActiveForCustomer($customerId, array $sharedStoreIds=[])
__construct(QuoteFactory $quoteFactory, StoreManagerInterface $storeManager, \Magento\Quote\Model\ResourceModel\Quote\Collection $quoteCollection, \Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory $searchResultsDataFactory, JoinProcessorInterface $extensionAttributesJoinProcessor, CollectionProcessorInterface $collectionProcessor=null, \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory $quoteCollectionFactory=null)
$value
Definition: gender.phtml:16
$cartId
Definition: quote.php:22
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
addFilterGroupToCollection(FilterGroup $filterGroup, QuoteCollection $collection)
getActive($cartId, array $sharedStoreIds=[])