Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OrderRepository.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Sales\Model;
8 
12 use Magento\Sales\Api\Data\OrderExtensionFactory;
13 use Magento\Sales\Api\Data\OrderExtensionInterface;
15 use Magento\Sales\Api\Data\OrderSearchResultInterfaceFactory as SearchResultFactory;
20 
27 {
31  protected $metadata;
32 
36  protected $searchResultFactory = null;
37 
41  private $orderExtensionFactory;
42 
46  private $shippingAssignmentBuilder;
47 
51  private $collectionProcessor;
52 
56  protected $registry = [];
57 
66  public function __construct(
68  SearchResultFactory $searchResultFactory,
69  CollectionProcessorInterface $collectionProcessor = null,
70  \Magento\Sales\Api\Data\OrderExtensionFactory $orderExtensionFactory = null
71  ) {
72  $this->metadata = $metadata;
73  $this->searchResultFactory = $searchResultFactory;
74  $this->collectionProcessor = $collectionProcessor ?: ObjectManager::getInstance()
75  ->get(\Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class);
76  $this->orderExtensionFactory = $orderExtensionFactory ?: ObjectManager::getInstance()
77  ->get(\Magento\Sales\Api\Data\OrderExtensionFactory::class);
78  }
79 
88  public function get($id)
89  {
90  if (!$id) {
91  throw new InputException(__('An ID is needed. Set the ID and try again.'));
92  }
93  if (!isset($this->registry[$id])) {
95  $entity = $this->metadata->getNewInstance()->load($id);
96  if (!$entity->getEntityId()) {
97  throw new NoSuchEntityException(
98  __("The entity that was requested doesn't exist. Verify the entity and try again.")
99  );
100  }
101  $this->setShippingAssignments($entity);
102  $this->registry[$id] = $entity;
103  }
104  return $this->registry[$id];
105  }
106 
113  public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
114  {
116  $searchResult = $this->searchResultFactory->create();
117  $this->collectionProcessor->process($searchCriteria, $searchResult);
118  $searchResult->setSearchCriteria($searchCriteria);
119  foreach ($searchResult->getItems() as $order) {
120  $this->setShippingAssignments($order);
121  }
122  return $searchResult;
123  }
124 
132  {
133  $this->metadata->getMapper()->delete($entity);
134  unset($this->registry[$entity->getEntityId()]);
135  return true;
136  }
137 
144  public function deleteById($id)
145  {
146  $entity = $this->get($id);
147  return $this->delete($entity);
148  }
149 
156  public function save(\Magento\Sales\Api\Data\OrderInterface $entity)
157  {
159  $extensionAttributes = $entity->getExtensionAttributes();
160  if ($entity->getIsNotVirtual() && $extensionAttributes && $extensionAttributes->getShippingAssignments()) {
161  $shippingAssignments = $extensionAttributes->getShippingAssignments();
162  if (!empty($shippingAssignments)) {
163  $shipping = array_shift($shippingAssignments)->getShipping();
164  $entity->setShippingAddress($shipping->getAddress());
165  $entity->setShippingMethod($shipping->getMethod());
166  }
167  }
168  $this->metadata->getMapper()->save($entity);
169  $this->registry[$entity->getEntityId()] = $entity;
170  return $this->registry[$entity->getEntityId()];
171  }
172 
177  private function setShippingAssignments(OrderInterface $order)
178  {
180  $extensionAttributes = $order->getExtensionAttributes();
181 
182  if ($extensionAttributes === null) {
183  $extensionAttributes = $this->orderExtensionFactory->create();
184  } elseif ($extensionAttributes->getShippingAssignments() !== null) {
185  return;
186  }
188  $shippingAssignments = $this->getShippingAssignmentBuilderDependency();
189  $shippingAssignments->setOrderId($order->getEntityId());
190  $extensionAttributes->setShippingAssignments($shippingAssignments->create());
191  $order->setExtensionAttributes($extensionAttributes);
192  }
193 
200  private function getShippingAssignmentBuilderDependency()
201  {
202  if (!$this->shippingAssignmentBuilder instanceof ShippingAssignmentBuilder) {
203  $this->shippingAssignmentBuilder = \Magento\Framework\App\ObjectManager::getInstance()->get(
204  \Magento\Sales\Model\Order\ShippingAssignmentBuilder::class
205  );
206  }
207  return $this->shippingAssignmentBuilder;
208  }
209 
219  protected function addFilterGroupToCollection(
220  \Magento\Framework\Api\Search\FilterGroup $filterGroup,
221  \Magento\Sales\Api\Data\OrderSearchResultInterface $searchResult
222  ) {
223  $fields = [];
224  $conditions = [];
225  foreach ($filterGroup->getFilters() as $filter) {
226  $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
227  $conditions[] = [$condition => $filter->getValue()];
228  $fields[] = $filter->getField();
229  }
230  if ($fields) {
231  $searchResult->addFieldToFilter($fields, $conditions);
232  }
233  }
234 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14
$order
Definition: order.php:55
$fields
Definition: details.phtml:14
addFilterGroupToCollection(\Magento\Framework\Api\Search\FilterGroup $filterGroup, \Magento\Sales\Api\Data\OrderSearchResultInterface $searchResult)
__()
Definition: __.php:13
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
$searchCriteria
save(\Magento\Sales\Api\Data\OrderInterface $entity)
$extensionAttributes
Definition: payment.php:22
$entity
Definition: element.phtml:22
__construct(Metadata $metadata, SearchResultFactory $searchResultFactory, CollectionProcessorInterface $collectionProcessor=null, \Magento\Sales\Api\Data\OrderExtensionFactory $orderExtensionFactory=null)