Skip to content

Commit

Permalink
Merge pull request #99 from ihorvansach/refactor-code-loginascustomer…
Browse files Browse the repository at this point in the history
…-sales

Moved code related to the shopping cart from LoginAsCustomer to LogiAsCustomerSales extension
  • Loading branch information
naydav authored Apr 27, 2020
2 parents c1dee1f + ecf444b commit 60bf50a
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 32 deletions.
32 changes: 1 addition & 31 deletions app/code/Magento/LoginAsCustomer/Model/AuthenticateCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,14 @@ class AuthenticateCustomer implements AuthenticateCustomerInterface
*/
private $customerSession;

/**
* @var \Magento\Checkout\Model\Cart
*/
private $cart;

/**
* @var \Magento\Checkout\Model\Session
*/
private $checkoutSession;

/**
* AuthenticateCustomer constructor.
* @param Session $customerSession
* @param \Magento\Checkout\Model\Cart $cart
* @param \Magento\Checkout\Model\Session $checkoutSession
*/
public function __construct(
Session $customerSession,
\Magento\Checkout\Model\Cart $cart,
\Magento\Checkout\Model\Session $checkoutSession
Session $customerSession
) {
$this->customerSession = $customerSession;
$this->cart = $cart;
$this->checkoutSession = $checkoutSession;
}

/**
Expand All @@ -58,13 +42,6 @@ public function execute(int $customerId, int $adminId):bool
if ($this->customerSession->getId()) {
/* Logout if logged in */
$this->customerSession->logout();
} else {
$quote = $this->cart->getQuote();
/* Remove items from guest cart */
foreach ($quote->getAllVisibleItems() as $item) {
$this->cart->removeItem($item->getId());
}
$this->cart->save();
}

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

/* Load Customer Quote */
$this->checkoutSession->loadCustomerQuote();

$quote = $this->checkoutSession->getQuote();
$quote->setCustomerIsGuest(0);
$quote->save();

return $loggedIn;
}
}
1 change: 0 additions & 1 deletion app/code/Magento/LoginAsCustomer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"php": "~7.1.3||~7.2.0||~7.3.0",
"magento/framework": "*",
"magento/module-backend": "*",
"magento/module-checkout": "*",
"magento/module-customer": "*",
"magento/module-sales": "*",
"magento/module-store": "*",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\LoginAsCustomerSales\Plugin;

use Magento\Customer\Model\Session as CustomerSession;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\LoginAsCustomer\Api\AuthenticateCustomerInterface;

/**
* Class AuthenticateCustomer Plugin
*/
class AuthenticateCustomer
{
/**
* @var CustomerSession
*/
private $customerSession;

/**
* @var CheckoutSession
*/
private $checkoutSession;

/**
* @var CartRepositoryInterface
*/
private $quoteRepository;

/**
* AuthenticateCustomer constructor.
* @param CustomerSession $customerSession
* @param CheckoutSession $checkoutSession
* @param CartRepositoryInterface $quoteRepository
*/
public function __construct(
CustomerSession $customerSession,
CheckoutSession $checkoutSession,
CartRepositoryInterface $quoteRepository
) {
$this->customerSession = $customerSession;
$this->checkoutSession = $checkoutSession;
$this->quoteRepository = $quoteRepository;
}

/**
* Remove all items from guest shopping cart
* @param AuthenticateCustomerInterface $subject
* @param int $customerId
* @param int $adminId
*/
public function beforeExecute(
AuthenticateCustomerInterface $subject,
int $customerId,
int $adminId
) {
if (!$this->customerSession->getId()) {
$quote = $this->checkoutSession->getQuote();
/* Remove items from guest cart */
foreach ($quote->getAllVisibleItems() as $item) {
$quote->removeItem($item->getId());
}
$this->quoteRepository->save($quote);
}
}

/**
* Mart customer cart as not guest
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @param int $adminId
* @param AuthenticateCustomerInterface $subject
* @param bool $result
* @param int $customerId
*/
public function afterExecute(
AuthenticateCustomerInterface $subject,
bool $result,
int $customerId,
int $adminId
) {
if ($result) {
/* Load Customer Quote */
$this->checkoutSession->loadCustomerQuote();

$quote = $this->checkoutSession->getQuote();
$quote->setCustomerIsGuest(0);
$this->quoteRepository->save($quote);
}
return $result;
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/LoginAsCustomerSales/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Magento_LoginAsCustomerSales module

The Magento_LoginAsCustomerSales module is responsible for comunication between Magento_LoginAsCustomer and shopping cart state.
22 changes: 22 additions & 0 deletions app/code/Magento/LoginAsCustomerSales/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "magento/module-login-as-customer-sales",
"description": "",
"require": {
"php": "~7.1.3||~7.2.0||~7.3.0",
"magento/module-login-as-customer": "*",
"magento/module-customer": "*",
"magento/module-checkout": "*",
"magento/module-quote": "*"
},
"type": "magento2-module",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [ "registration.php" ],
"psr-4": {
"Magento\\LoginAsCustomerSales\\": ""
}
}
}
12 changes: 12 additions & 0 deletions app/code/Magento/LoginAsCustomerSales/etc/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\LoginAsCustomer\Api\AuthenticateCustomerInterface">
<plugin disabled="false" name="login_as_customer_sales_authenticate_customer" sortOrder="10" type="Magento\LoginAsCustomerSales\Plugin\AuthenticateCustomer"/>
</type>
</config>
17 changes: 17 additions & 0 deletions app/code/Magento/LoginAsCustomerSales/etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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:Module/etc/module.xsd">
<module name="Magento_LoginAsCustomerSales" setup_version="2.2.1">
<sequence>
<module name="Magento_LoginAsCustomer"/>
<module name="Magento_Customer"/>
<module name="Magento_Checkout"/>
<module name="Magento_Quote"/>
</sequence>
</module>
</config>
11 changes: 11 additions & 0 deletions app/code/Magento/LoginAsCustomerSales/registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Magento_LoginAsCustomerSales',
__DIR__
);

0 comments on commit 60bf50a

Please sign in to comment.