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

标签 magento magento-1.4

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

- 编辑 -

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

最佳答案

在 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/

相关文章:

php - fatal error : Out of memory on Dedicated Server running Magento

php - Magento - 在 1.8 上删除 "ShortDescription"Div

magento - ReadTheDocs 系统的代码格式

php - 即使配置产品被禁用,Magento 也会从简单产品中获取可配置产品

magento - Magento 1.7 中的导入产品未出现

php - 在 magento 后端翻译自定义模块管理面板

magento 删除我的帐户

php - Magento:使用 getModel 获取产品和类别

magento - 如何删除 Magento 管理表单元素?

magento - Magento 中产品详细信息页面上的自定义变量