Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…er/issues/28

magento2-login-as-customer/issues/28: Admin order noted in Order Comments.
  • Loading branch information
naydav authored Apr 18, 2020
2 parents 0f35ebd + 8920a7a commit 06bbdb8
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\LoginAsCustomer\Plugin;

use Magento\Backend\Model\Auth\Session;
use Magento\Sales\Model\Order;

/**
* Add comment after order placed by admin using admin panel.
*/
class AdminAddCommentOnOrderPlacementPlugin
{
/**
* @var Session
*/
private $userSession;

/**
* @param Session $session
*/
public function __construct(
Session $session
) {
$this->userSession = $session;
}

/**
* Add comment after order placed by admin using admin panel.
*
* @param Order $subject
* @param Order $result
* @return Order
*/
public function afterPlace(Order $subject, Order $result): Order
{
$adminUser = $this->userSession->getUser();
$subject->addCommentToStatusHistory(
'Order Placed by Store Administrator',
false,
true
)->setIsCustomerNotified(false);
$subject->addCommentToStatusHistory(
"Order Placed by {$adminUser->getFirstName()} {$adminUser->getLastName()} using Admin Panel",
false,
false
)->setIsCustomerNotified(false);

return $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\LoginAsCustomer\Plugin;

use Magento\Customer\Model\Session;
use Magento\Sales\Model\Order;
use Magento\User\Model\UserFactory;

/**
* Add comment after order placed by admin using login-as-customer.
*/
class FrontAddCommentOnOrderPlacementPlugin
{
/**
* @var Session
*/
private $customerSession;

/**
* @var UserFactory
*/
private $userFactory;

/**
* @param Session $session
* @param UserFactory $userFactory
*/
public function __construct(
Session $session,
UserFactory $userFactory
) {
$this->customerSession = $session;
$this->userFactory = $userFactory;
}

/**
* Add comment after order placed by admin using login-as-customer.
*
* @param Order $subject
* @param Order $result
* @return Order
*/
public function afterPlace(Order $subject, Order $result): Order
{
$adminId = $this->customerSession->getLoggedAsCustomerAdmindId();
if ($adminId) {
$adminUser = $this->userFactory->create()->load($adminId);
$subject->addCommentToStatusHistory(
'Order Placed by Store Administrator',
false,
true
)->setIsCustomerNotified(false);
$subject->addCommentToStatusHistory(
"Order Placed by {$adminUser->getFirstName()} {$adminUser->getLastName()} using Login as Customer",
false,
false
)->setIsCustomerNotified(false);
}

return $result;
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/LoginAsCustomer/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
<type name="Magento\Backend\Block\Widget\Button\Toolbar">
<plugin name="Magento_LoginAsCustomer::toolbar_button" type="Magento\LoginAsCustomer\Plugin\Button\ToolbarPlugin" />
</type>
<type name="Magento\Sales\Model\Order">
<plugin name="lac-admin-order-placement-comment" type="Magento\LoginAsCustomer\Plugin\AdminAddCommentOnOrderPlacementPlugin"/>
</type>
</config>
12 changes: 12 additions & 0 deletions app/code/Magento/LoginAsCustomer/etc/webapi_rest/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Sales\Model\Order">
<plugin name="lac-front-order-placement-comment" type="Magento\LoginAsCustomer\Plugin\FrontAddCommentOnOrderPlacementPlugin"/>
</type>
</config>

0 comments on commit 06bbdb8

Please sign in to comment.