forked from tomispepe/magento2-1
/
AddComment.php
executable file
·60 lines (52 loc) · 2.3 KB
/
AddComment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/**
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Controller\Adminhtml\Order;
use Magento\Backend\App\Action;
use Magento\Sales\Model\Order\Email\Sender\OrderCommentSender;
class AddComment extends \Magento\Sales\Controller\Adminhtml\Order
{
/**
* Add order comment action
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
$order = $this->_initOrder();
if ($order) {
try {
$data = $this->getRequest()->getPost('history');
if (empty($data['comment']) && $data['status'] == $order->getDataByKey('status')) {
throw new \Magento\Framework\Exception\LocalizedException(__('Comment text cannot be empty.'));
}
$notify = isset($data['is_customer_notified']) ? $data['is_customer_notified'] : false;
$visible = isset($data['is_visible_on_front']) ? $data['is_visible_on_front'] : false;
$history = $order->addStatusHistoryComment($data['comment'], $data['status']);
$history->setIsVisibleOnFront($visible);
$history->setIsCustomerNotified($notify);
$history->save();
$comment = trim(strip_tags($data['comment']));
$order->save();
/** @var OrderCommentSender $orderCommentSender */
$orderCommentSender = $this->_objectManager
->create('Magento\Sales\Model\Order\Email\Sender\OrderCommentSender');
$orderCommentSender->send($order, $notify, $comment);
return $this->resultPageFactory->create();
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$response = ['error' => true, 'message' => $e->getMessage()];
} catch (\Exception $e) {
$response = ['error' => true, 'message' => __('We cannot add order history.')];
}
if (is_array($response)) {
$resultJson = $this->resultJsonFactory->create();
$resultJson->setData($response);
return $resultJson;
}
}
return $this->resultRedirectFactory->create()->setPath('sales/*/');
}
}