Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddComment.php
Go to the documentation of this file.
1 <?php
9 
12 
14 {
20  const ADMIN_RESOURCE = 'Magento_Sales::comment';
21 
27  public function execute()
28  {
29  $order = $this->_initOrder();
30  if ($order) {
31  try {
32  $data = $this->getRequest()->getPost('history');
33  if (empty($data['comment']) && $data['status'] == $order->getDataByKey('status')) {
34  throw new \Magento\Framework\Exception\LocalizedException(
35  __('The comment is missing. Enter and try again.')
36  );
37  }
38 
39  $notify = isset($data['is_customer_notified']) ? $data['is_customer_notified'] : false;
40  $visible = isset($data['is_visible_on_front']) ? $data['is_visible_on_front'] : false;
41 
42  $history = $order->addStatusHistoryComment($data['comment'], $data['status']);
43  $history->setIsVisibleOnFront($visible);
44  $history->setIsCustomerNotified($notify);
45  $history->save();
46 
47  $comment = trim(strip_tags($data['comment']));
48 
49  $order->save();
51  $orderCommentSender = $this->_objectManager
52  ->create(\Magento\Sales\Model\Order\Email\Sender\OrderCommentSender::class);
53 
54  $orderCommentSender->send($order, $notify, $comment);
55 
56  return $this->resultPageFactory->create();
57  } catch (\Magento\Framework\Exception\LocalizedException $e) {
58  $response = ['error' => true, 'message' => $e->getMessage()];
59  } catch (\Exception $e) {
60  $response = ['error' => true, 'message' => __('We cannot add order history.')];
61  }
62  if (is_array($response)) {
63  $resultJson = $this->resultJsonFactory->create();
64  $resultJson->setData($response);
65  return $resultJson;
66  }
67  }
68  return $this->resultRedirectFactory->create()->setPath('sales/*/');
69  }
70 }
$response
Definition: 404.php:11
$order
Definition: order.php:55
__()
Definition: __.php:13