-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Fix: Update customer email in quote #24049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
magento-engcom-team
merged 7 commits into
magento:2.3-develop
from
UncleTioma:hotfix/t24012_update_customer_email_in_quote
Aug 19, 2019
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9177050
Add obbserver for updating quote customer_email
UncleTioma a62f72b
Unit test for UpgradeQuoteCustomerEmailObserver
UncleTioma 7ef77d7
Replace event from frontend to base
UncleTioma 766db37
Add Copyright to UpgradeQuoteCustomerEmailObserver and Test
UncleTioma b0ba2ac
Check for existing origCustomerData. Catch NoSuchEnityException for c…
UncleTioma e270394
Refactoring and best practices for UpgradeQuoteCustomerEmailObserver
UncleTioma cacbe13
Delete blank line before brace
UncleTioma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
app/code/Magento/Customer/Observer/UpgradeQuoteCustomerEmailObserver.php
This file contains hidden or 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,66 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Customer\Observer; | ||
|
||
use Magento\Framework\Event\ObserverInterface; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Quote\Api\CartRepositoryInterface; | ||
|
||
/** | ||
* Class observer UpgradeQuoteCustomerEmailObserver | ||
*/ | ||
class UpgradeQuoteCustomerEmailObserver implements ObserverInterface | ||
{ | ||
/** | ||
* @var CartRepositoryInterface | ||
*/ | ||
private $quoteRepository; | ||
|
||
/** | ||
* @param CartRepositoryInterface $quoteRepository | ||
*/ | ||
public function __construct( | ||
CartRepositoryInterface $quoteRepository | ||
) { | ||
$this->quoteRepository = $quoteRepository; | ||
} | ||
|
||
/** | ||
* Upgrade quote customer email when customer has changed email | ||
* | ||
* @param Observer $observer | ||
* @return void | ||
*/ | ||
public function execute(Observer $observer): void | ||
{ | ||
/** @var \Magento\Customer\Model\Data\Customer $customerOrig */ | ||
$customerOrig = $observer->getEvent()->getOrigCustomerDataObject(); | ||
if (!$customerOrig) { | ||
return; | ||
} | ||
|
||
$emailOrig = $customerOrig->getEmail(); | ||
|
||
/** @var \Magento\Customer\Model\Data\Customer $customer */ | ||
$customer = $observer->getEvent()->getCustomerDataObject(); | ||
$email = $customer->getEmail(); | ||
|
||
if ($email == $emailOrig) { | ||
return; | ||
} | ||
|
||
try { | ||
$quote = $this->quoteRepository->getForCustomer($customer->getId()); | ||
UncleTioma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$quote->setCustomerEmail($email); | ||
$this->quoteRepository->save($quote); | ||
} catch (NoSuchEntityException $e) { | ||
return; | ||
} | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
app/code/Magento/Customer/Test/Unit/Observer/UpgradeQuoteCustomerEmailObserverTest.php
This file contains hidden or 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,107 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Customer\Test\Unit\Observer; | ||
|
||
use Magento\Customer\Observer\UpgradeQuoteCustomerEmailObserver; | ||
|
||
/** | ||
* Class UpgradeQuoteCustomerEmailObserverTest for testing upgrade quote customer email | ||
*/ | ||
class UpgradeQuoteCustomerEmailObserverTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* @var UpgradeQuoteCustomerEmailObserver | ||
*/ | ||
protected $model; | ||
|
||
/** | ||
* @var \Magento\Quote\Api\CartRepositoryInterface | ||
*/ | ||
protected $quoteRepositoryMock; | ||
|
||
/** | ||
* @var \Magento\Framework\Event\Observer | ||
*/ | ||
protected $observerMock; | ||
|
||
/** | ||
* @var \Magento\Framework\Event | ||
*/ | ||
protected $eventMock; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->observerMock = $this->getMockBuilder(\Magento\Framework\Event\Observer::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->eventMock = $this->getMockBuilder(\Magento\Framework\Event::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['getCustomerDataObject', 'getOrigCustomerDataObject']) | ||
->getMock(); | ||
|
||
$this->observerMock->expects($this->any())->method('getEvent')->will($this->returnValue($this->eventMock)); | ||
|
||
$this->quoteRepositoryMock = $this | ||
->getMockBuilder(\Magento\Quote\Api\CartRepositoryInterface::class) | ||
->getMockForAbstractClass(); | ||
$this->model = new UpgradeQuoteCustomerEmailObserver($this->quoteRepositoryMock); | ||
} | ||
|
||
/** | ||
* Unit test for verifying quote customers email upgrade observer | ||
*/ | ||
public function testUpgradeQuoteCustomerEmail() | ||
{ | ||
$email = "[email protected]"; | ||
$origEmail = "[email protected]"; | ||
|
||
$customer = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$customerOrig = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class) | ||
->setMethods(['setCustomerEmail']) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->eventMock->expects($this->any()) | ||
->method('getCustomerDataObject') | ||
->will($this->returnValue($customer)); | ||
$this->eventMock->expects($this->any()) | ||
->method('getOrigCustomerDataObject') | ||
->will($this->returnValue($customerOrig)); | ||
|
||
$customerOrig->expects($this->any()) | ||
->method('getEmail') | ||
->willReturn($this->returnValue($origEmail)); | ||
|
||
$customer->expects($this->any()) | ||
->method('getEmail') | ||
->willReturn($this->returnValue($email)); | ||
|
||
$this->quoteRepositoryMock->expects($this->once()) | ||
->method('getForCustomer') | ||
->willReturn($quoteMock); | ||
|
||
$quoteMock->expects($this->once()) | ||
->method('setCustomerEmail'); | ||
|
||
$this->quoteRepositoryMock->expects($this->once()) | ||
->method('save') | ||
->with($quoteMock); | ||
|
||
$this->model->execute($this->observerMock); | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.