Skip to content

Commit 06bbdb8

Browse files
authoredApr 18, 2020
magento2-login-as-customer/issues/28: Admin order noted in Order Comments.
2 parents 0f35ebd + 8920a7a commit 06bbdb8

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\LoginAsCustomer\Plugin;
9+
10+
use Magento\Backend\Model\Auth\Session;
11+
use Magento\Sales\Model\Order;
12+
13+
/**
14+
* Add comment after order placed by admin using admin panel.
15+
*/
16+
class AdminAddCommentOnOrderPlacementPlugin
17+
{
18+
/**
19+
* @var Session
20+
*/
21+
private $userSession;
22+
23+
/**
24+
* @param Session $session
25+
*/
26+
public function __construct(
27+
Session $session
28+
) {
29+
$this->userSession = $session;
30+
}
31+
32+
/**
33+
* Add comment after order placed by admin using admin panel.
34+
*
35+
* @param Order $subject
36+
* @param Order $result
37+
* @return Order
38+
*/
39+
public function afterPlace(Order $subject, Order $result): Order
40+
{
41+
$adminUser = $this->userSession->getUser();
42+
$subject->addCommentToStatusHistory(
43+
'Order Placed by Store Administrator',
44+
false,
45+
true
46+
)->setIsCustomerNotified(false);
47+
$subject->addCommentToStatusHistory(
48+
"Order Placed by {$adminUser->getFirstName()} {$adminUser->getLastName()} using Admin Panel",
49+
false,
50+
false
51+
)->setIsCustomerNotified(false);
52+
53+
return $result;
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\LoginAsCustomer\Plugin;
9+
10+
use Magento\Customer\Model\Session;
11+
use Magento\Sales\Model\Order;
12+
use Magento\User\Model\UserFactory;
13+
14+
/**
15+
* Add comment after order placed by admin using login-as-customer.
16+
*/
17+
class FrontAddCommentOnOrderPlacementPlugin
18+
{
19+
/**
20+
* @var Session
21+
*/
22+
private $customerSession;
23+
24+
/**
25+
* @var UserFactory
26+
*/
27+
private $userFactory;
28+
29+
/**
30+
* @param Session $session
31+
* @param UserFactory $userFactory
32+
*/
33+
public function __construct(
34+
Session $session,
35+
UserFactory $userFactory
36+
) {
37+
$this->customerSession = $session;
38+
$this->userFactory = $userFactory;
39+
}
40+
41+
/**
42+
* Add comment after order placed by admin using login-as-customer.
43+
*
44+
* @param Order $subject
45+
* @param Order $result
46+
* @return Order
47+
*/
48+
public function afterPlace(Order $subject, Order $result): Order
49+
{
50+
$adminId = $this->customerSession->getLoggedAsCustomerAdmindId();
51+
if ($adminId) {
52+
$adminUser = $this->userFactory->create()->load($adminId);
53+
$subject->addCommentToStatusHistory(
54+
'Order Placed by Store Administrator',
55+
false,
56+
true
57+
)->setIsCustomerNotified(false);
58+
$subject->addCommentToStatusHistory(
59+
"Order Placed by {$adminUser->getFirstName()} {$adminUser->getLastName()} using Login as Customer",
60+
false,
61+
false
62+
)->setIsCustomerNotified(false);
63+
}
64+
65+
return $result;
66+
}
67+
}

‎app/code/Magento/LoginAsCustomer/etc/adminhtml/di.xml

+3
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@
99
<type name="Magento\Backend\Block\Widget\Button\Toolbar">
1010
<plugin name="Magento_LoginAsCustomer::toolbar_button" type="Magento\LoginAsCustomer\Plugin\Button\ToolbarPlugin" />
1111
</type>
12+
<type name="Magento\Sales\Model\Order">
13+
<plugin name="lac-admin-order-placement-comment" type="Magento\LoginAsCustomer\Plugin\AdminAddCommentOnOrderPlacementPlugin"/>
14+
</type>
1215
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="Magento\Sales\Model\Order">
10+
<plugin name="lac-front-order-placement-comment" type="Magento\LoginAsCustomer\Plugin\FrontAddCommentOnOrderPlacementPlugin"/>
11+
</type>
12+
</config>

0 commit comments

Comments
 (0)