Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OrderService.php
Go to the documentation of this file.
1 <?php
7 
10 
17 {
21  protected $orderRepository;
22 
26  protected $historyRepository;
27 
31  protected $criteriaBuilder;
32 
36  protected $filterBuilder;
37 
41  protected $notifier;
42 
46  protected $eventManager;
47 
52 
56  private $paymentFailures;
57 
70  public function __construct(
72  \Magento\Sales\Api\OrderStatusHistoryRepositoryInterface $historyRepository,
74  \Magento\Framework\Api\FilterBuilder $filterBuilder,
75  \Magento\Sales\Model\OrderNotifier $notifier,
76  \Magento\Framework\Event\ManagerInterface $eventManager,
78  \Magento\Sales\Api\PaymentFailuresInterface $paymentFailures = null
79  ) {
80  $this->orderRepository = $orderRepository;
81  $this->historyRepository = $historyRepository;
82  $this->criteriaBuilder = $criteriaBuilder;
83  $this->filterBuilder = $filterBuilder;
84  $this->notifier = $notifier;
85  $this->eventManager = $eventManager;
86  $this->orderCommentSender = $orderCommentSender;
87  $this->paymentFailures = $paymentFailures ? : \Magento\Framework\App\ObjectManager::getInstance()
88  ->get(\Magento\Sales\Api\PaymentFailuresInterface::class);
89  }
90 
97  public function cancel($id)
98  {
99  $order = $this->orderRepository->get($id);
100  if ($order->canCancel()) {
101  $order->cancel();
102  $this->orderRepository->save($order);
103  return true;
104  }
105 
106  return false;
107  }
108 
115  public function getCommentsList($id)
116  {
117  $this->criteriaBuilder->addFilters(
118  [$this->filterBuilder->setField('parent_id')->setValue($id)->setConditionType('eq')->create()]
119  );
120  $searchCriteria = $this->criteriaBuilder->create();
121  return $this->historyRepository->getList($searchCriteria);
122  }
123 
131  public function addComment($id, \Magento\Sales\Api\Data\OrderStatusHistoryInterface $statusHistory)
132  {
133  $order = $this->orderRepository->get($id);
134  $order->addStatusHistory($statusHistory);
135  $this->orderRepository->save($order);
136  $notify = isset($statusHistory['is_customer_notified']) ? $statusHistory['is_customer_notified'] : false;
137  $comment = trim(strip_tags($statusHistory->getComment()));
138  $this->orderCommentSender->send($order, $notify, $comment);
139  return true;
140  }
141 
148  public function notify($id)
149  {
150  $order = $this->orderRepository->get($id);
151  return $this->notifier->notify($order);
152  }
153 
160  public function getStatus($id)
161  {
162  return $this->orderRepository->get($id)->getStatus();
163  }
164 
171  public function hold($id)
172  {
173  $order = $this->orderRepository->get($id);
174  $order->hold();
175  return (bool)$this->orderRepository->save($order);
176  }
177 
184  public function unHold($id)
185  {
186  $object = $this->orderRepository->get($id);
187  $object->unhold();
188  return (bool)$this->orderRepository->save($object);
189  }
190 
196  public function place(\Magento\Sales\Api\Data\OrderInterface $order)
197  {
198  // transaction will be here
199  //begin transaction
200  try {
201  $order->place();
202  return $this->orderRepository->save($order);
203  //commit
204  } catch (\Exception $e) {
205  if ($e instanceof CommandException) {
206  $this->paymentFailures->handle((int)$order->getQuoteId(), __($e->getMessage()));
207  }
208  throw $e;
209  //rollback;
210  }
211  }
212 
230  public function setState(
231  \Magento\Sales\Api\Data\OrderInterface $order,
232  $state,
233  $status = false,
234  $comment = '',
235  $isCustomerNotified = null,
236  $shouldProtectState = true
237  ) {
238  // attempt to set the specified state
239  if ($shouldProtectState) {
240  if ($order->isStateProtected($state)) {
241  throw new \Magento\Framework\Exception\LocalizedException(
242  __('The Order State "%1" must not be set manually.', $state)
243  );
244  }
245  }
246 
247  $transport = new \Magento\Framework\DataObject(
248  [
249  'state' => $state,
250  'status' => $status,
251  'comment' => $comment,
252  'is_customer_notified' => $isCustomerNotified
253  ]
254  );
255 
256  $this->eventManager->dispatch(
257  'sales_order_state_change_before',
258  ['order' => $this, 'transport' => $transport]
259  );
260  $status = $transport->getStatus();
261  $order->setData('state', $transport->getState());
262 
263  // add status history
264  if ($status) {
265  if ($status === true) {
266  $status = $order->getConfig()->getStateDefaultStatus($transport->getState());
267  }
268  $order->setStatus($status);
269  $history = $order->addStatusHistoryComment($transport->getComment(), false);
270  // no sense to set $status again
271  $history->setIsCustomerNotified($transport->getIsCustomerNotified());
272  }
273  return $this;
274  }
275 }
__construct(\Magento\Sales\Api\OrderRepositoryInterface $orderRepository, \Magento\Sales\Api\OrderStatusHistoryRepositoryInterface $historyRepository, \Magento\Framework\Api\SearchCriteriaBuilder $criteriaBuilder, \Magento\Framework\Api\FilterBuilder $filterBuilder, \Magento\Sales\Model\OrderNotifier $notifier, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Sales\Model\Order\Email\Sender\OrderCommentSender $orderCommentSender, \Magento\Sales\Api\PaymentFailuresInterface $paymentFailures=null)
$id
Definition: fieldset.phtml:14
$order
Definition: order.php:55
__()
Definition: __.php:13
$searchCriteria
place(\Magento\Sales\Api\Data\OrderInterface $order)
$status
Definition: order_status.php:8
addComment($id, \Magento\Sales\Api\Data\OrderStatusHistoryInterface $statusHistory)
setState(\Magento\Sales\Api\Data\OrderInterface $order, $state, $status=false, $comment='', $isCustomerNotified=null, $shouldProtectState=true)