gpt4 book ai didi

magento - 将用户名添加到订单评论历史记录

转载 作者:行者123 更新时间:2023-12-03 23:43:20 24 4
gpt4 key购买 nike

有没有一种简单的方法可以将在管理员历史记录中发表评论的人的用户名添加到订单的评论线程中?

- 编辑 -

另一种询问方式是如何向评论历史模型添加一个附加字段,以便我可以覆盖将数据插入数据结构的适当模型和模板。

最佳答案

在 Magento 2

您需要覆盖 AddComment.php文件在 vendor/magento/module-sales

如果要编辑核心文件AddComment.php然后您可以将以下代码添加到您的 AddComment.php文件

$username = $this->authSession->getUser()->getUsername();
$append = " (by ".$username.")";
$history = $order->addStatusHistoryComment($data['comment'].$append, $data['status']);

但这不是直接修改核心文件的好习惯。您需要制作新模块并覆盖
Vendor/Module/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Sales\Controller\Adminhtml\Order\AddComment" type="Vendor\Module\Controller\Adminhtml\Order\AddComment" />
</config>
Vendor\Module\Controller\Adminhtml\Order\AddComment.php
<?php
namespace Vendor\Module\Controller\Adminhtml\Order;

use Magento\Backend\App\Action;
use Magento\Sales\Model\Order\Email\Sender\OrderCommentSender;

use Magento\Sales\Api\OrderManagementInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\InputException;
use Psr\Log\LoggerInterface;

class AddComment extends \Magento\Sales\Controller\Adminhtml\Order
{
/**
* Authorization level of a basic admin session
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_Sales::comment';

/**
* Core registry
*
* @var \Magento\Framework\Registry
*/
protected $_coreRegistry = null;

/**
* @var \Magento\Framework\App\Response\Http\FileFactory
*/
protected $_fileFactory;

/**
* @var \Magento\Framework\Translate\InlineInterface
*/
protected $_translateInline;

/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;

/**
* @var \Magento\Framework\Controller\Result\JsonFactory
*/
protected $resultJsonFactory;

/**
* @var \Magento\Framework\View\Result\LayoutFactory
*/
protected $resultLayoutFactory;

/**
* @var \Magento\Framework\Controller\Result\RawFactory
*/
protected $resultRawFactory;

/**
* @var OrderManagementInterface
*/
protected $orderManagement;

/**
* @var OrderRepositoryInterface
*/
protected $orderRepository;

/**
* @var LoggerInterface
*/
protected $logger;

protected $authSession;

public function __construct(
Action\Context $context,
\Magento\Framework\Registry $coreRegistry,
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
\Magento\Framework\Translate\InlineInterface $translateInline,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
\Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory,
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
OrderManagementInterface $orderManagement,
OrderRepositoryInterface $orderRepository,
LoggerInterface $logger,
\Magento\Backend\Model\Auth\Session $authSession
) {
$this->authSession = $authSession;
parent::__construct($context, $coreRegistry,$fileFactory,$translateInline,$resultPageFactory,$resultJsonFactory,$resultLayoutFactory,$resultRawFactory,$orderManagement,$orderRepository,$logger);
}

/**
* 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(__('Please enter a comment.'));
}

$notify = isset($data['is_customer_notified']) ? $data['is_customer_notified'] : false;
$visible = isset($data['is_visible_on_front']) ? $data['is_visible_on_front'] : false;

$username = $this->authSession->getUser()->getUsername();
$append = " (by ".$username.")";

$history = $order->addStatusHistoryComment($data['comment'].$append, $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::class);

$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/*/');
}
}

关于magento - 将用户名添加到订单评论历史记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6219620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com