-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from ihorvansach/refactor-code-loginascustomer…
…-sales Moved code related to the shopping cart from LoginAsCustomer to LogiAsCustomerSales extension
- Loading branch information
Showing
8 changed files
with
165 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
app/code/Magento/LoginAsCustomerSales/Plugin/AuthenticateCustomer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\\": "" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ | ||
); |