Skip to content

Commit 60bf50a

Browse files
authored
Merge pull request #99 from ihorvansach/refactor-code-loginascustomer-sales
Moved code related to the shopping cart from LoginAsCustomer to LogiAsCustomerSales extension
2 parents c1dee1f + ecf444b commit 60bf50a

File tree

8 files changed

+165
-32
lines changed

8 files changed

+165
-32
lines changed

app/code/Magento/LoginAsCustomer/Model/AuthenticateCustomer.php

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,14 @@ class AuthenticateCustomer implements AuthenticateCustomerInterface
2020
*/
2121
private $customerSession;
2222

23-
/**
24-
* @var \Magento\Checkout\Model\Cart
25-
*/
26-
private $cart;
27-
28-
/**
29-
* @var \Magento\Checkout\Model\Session
30-
*/
31-
private $checkoutSession;
32-
3323
/**
3424
* AuthenticateCustomer constructor.
3525
* @param Session $customerSession
36-
* @param \Magento\Checkout\Model\Cart $cart
37-
* @param \Magento\Checkout\Model\Session $checkoutSession
3826
*/
3927
public function __construct(
40-
Session $customerSession,
41-
\Magento\Checkout\Model\Cart $cart,
42-
\Magento\Checkout\Model\Session $checkoutSession
28+
Session $customerSession
4329
) {
4430
$this->customerSession = $customerSession;
45-
$this->cart = $cart;
46-
$this->checkoutSession = $checkoutSession;
4731
}
4832

4933
/**
@@ -58,13 +42,6 @@ public function execute(int $customerId, int $adminId):bool
5842
if ($this->customerSession->getId()) {
5943
/* Logout if logged in */
6044
$this->customerSession->logout();
61-
} else {
62-
$quote = $this->cart->getQuote();
63-
/* Remove items from guest cart */
64-
foreach ($quote->getAllVisibleItems() as $item) {
65-
$this->cart->removeItem($item->getId());
66-
}
67-
$this->cart->save();
6845
}
6946

7047
$loggedIn = $this->customerSession->loginById($customerId);
@@ -73,13 +50,6 @@ public function execute(int $customerId, int $adminId):bool
7350
$this->customerSession->setLoggedAsCustomerAdmindId($adminId);
7451
}
7552

76-
/* Load Customer Quote */
77-
$this->checkoutSession->loadCustomerQuote();
78-
79-
$quote = $this->checkoutSession->getQuote();
80-
$quote->setCustomerIsGuest(0);
81-
$quote->save();
82-
8353
return $loggedIn;
8454
}
8555
}

app/code/Magento/LoginAsCustomer/composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"php": "~7.1.3||~7.2.0||~7.3.0",
66
"magento/framework": "*",
77
"magento/module-backend": "*",
8-
"magento/module-checkout": "*",
98
"magento/module-customer": "*",
109
"magento/module-sales": "*",
1110
"magento/module-store": "*",
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\LoginAsCustomerSales\Plugin;
10+
11+
use Magento\Customer\Model\Session as CustomerSession;
12+
use Magento\Checkout\Model\Session as CheckoutSession;
13+
use Magento\Quote\Api\CartRepositoryInterface;
14+
use Magento\LoginAsCustomer\Api\AuthenticateCustomerInterface;
15+
16+
/**
17+
* Class AuthenticateCustomer Plugin
18+
*/
19+
class AuthenticateCustomer
20+
{
21+
/**
22+
* @var CustomerSession
23+
*/
24+
private $customerSession;
25+
26+
/**
27+
* @var CheckoutSession
28+
*/
29+
private $checkoutSession;
30+
31+
/**
32+
* @var CartRepositoryInterface
33+
*/
34+
private $quoteRepository;
35+
36+
/**
37+
* AuthenticateCustomer constructor.
38+
* @param CustomerSession $customerSession
39+
* @param CheckoutSession $checkoutSession
40+
* @param CartRepositoryInterface $quoteRepository
41+
*/
42+
public function __construct(
43+
CustomerSession $customerSession,
44+
CheckoutSession $checkoutSession,
45+
CartRepositoryInterface $quoteRepository
46+
) {
47+
$this->customerSession = $customerSession;
48+
$this->checkoutSession = $checkoutSession;
49+
$this->quoteRepository = $quoteRepository;
50+
}
51+
52+
/**
53+
* Remove all items from guest shopping cart
54+
* @param AuthenticateCustomerInterface $subject
55+
* @param int $customerId
56+
* @param int $adminId
57+
*/
58+
public function beforeExecute(
59+
AuthenticateCustomerInterface $subject,
60+
int $customerId,
61+
int $adminId
62+
) {
63+
if (!$this->customerSession->getId()) {
64+
$quote = $this->checkoutSession->getQuote();
65+
/* Remove items from guest cart */
66+
foreach ($quote->getAllVisibleItems() as $item) {
67+
$quote->removeItem($item->getId());
68+
}
69+
$this->quoteRepository->save($quote);
70+
}
71+
}
72+
73+
/**
74+
* Mart customer cart as not guest
75+
* @return bool
76+
* @throws \Magento\Framework\Exception\LocalizedException
77+
* @throws \Magento\Framework\Exception\NoSuchEntityException
78+
* @param int $adminId
79+
* @param AuthenticateCustomerInterface $subject
80+
* @param bool $result
81+
* @param int $customerId
82+
*/
83+
public function afterExecute(
84+
AuthenticateCustomerInterface $subject,
85+
bool $result,
86+
int $customerId,
87+
int $adminId
88+
) {
89+
if ($result) {
90+
/* Load Customer Quote */
91+
$this->checkoutSession->loadCustomerQuote();
92+
93+
$quote = $this->checkoutSession->getQuote();
94+
$quote->setCustomerIsGuest(0);
95+
$this->quoteRepository->save($quote);
96+
}
97+
return $result;
98+
}
99+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Magento_LoginAsCustomerSales module
2+
3+
The Magento_LoginAsCustomerSales module is responsible for comunication between Magento_LoginAsCustomer and shopping cart state.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "magento/module-login-as-customer-sales",
3+
"description": "",
4+
"require": {
5+
"php": "~7.1.3||~7.2.0||~7.3.0",
6+
"magento/module-login-as-customer": "*",
7+
"magento/module-customer": "*",
8+
"magento/module-checkout": "*",
9+
"magento/module-quote": "*"
10+
},
11+
"type": "magento2-module",
12+
"license": [
13+
"OSL-3.0",
14+
"AFL-3.0"
15+
],
16+
"autoload": {
17+
"files": [ "registration.php" ],
18+
"psr-4": {
19+
"Magento\\LoginAsCustomerSales\\": ""
20+
}
21+
}
22+
}
Lines changed: 12 additions & 0 deletions
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\LoginAsCustomer\Api\AuthenticateCustomerInterface">
10+
<plugin disabled="false" name="login_as_customer_sales_authenticate_customer" sortOrder="10" type="Magento\LoginAsCustomerSales\Plugin\AuthenticateCustomer"/>
11+
</type>
12+
</config>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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:Module/etc/module.xsd">
9+
<module name="Magento_LoginAsCustomerSales" setup_version="2.2.1">
10+
<sequence>
11+
<module name="Magento_LoginAsCustomer"/>
12+
<module name="Magento_Customer"/>
13+
<module name="Magento_Checkout"/>
14+
<module name="Magento_Quote"/>
15+
</sequence>
16+
</module>
17+
</config>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
\Magento\Framework\Component\ComponentRegistrar::register(
8+
\Magento\Framework\Component\ComponentRegistrar::MODULE,
9+
'Magento_LoginAsCustomerSales',
10+
__DIR__
11+
);

0 commit comments

Comments
 (0)