Skip to content

Commit c9fef1b

Browse files
committed
Fixing bug with non-existent customer group breaking quote
1 parent 94f8ea3 commit c9fef1b

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

app/code/Magento/Quote/Model/Quote.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Directory\Model\AllowedCountries;
1111
use Magento\Framework\Api\AttributeValueFactory;
1212
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
13+
use Magento\Framework\Exception\NoSuchEntityException;
1314
use Magento\Framework\Model\AbstractExtensibleModel;
1415
use Magento\Quote\Api\Data\PaymentInterface;
1516
use Magento\Quote\Model\Quote\Address;
@@ -1104,7 +1105,22 @@ public function getCustomerTaxClassId()
11041105
//if (!$this->getData('customer_group_id') && !$this->getData('customer_tax_class_id')) {
11051106
$groupId = $this->getCustomerGroupId();
11061107
if ($groupId !== null) {
1107-
$taxClassId = $this->groupRepository->getById($this->getCustomerGroupId())->getTaxClassId();
1108+
$taxClassId = null;
1109+
try {
1110+
$taxClassId = $this->groupRepository->getById($this->getCustomerGroupId())->getTaxClassId();
1111+
} catch (NoSuchEntityException $e) {
1112+
/**
1113+
* A customer MAY create a quote and AFTER that customer group MAY be deleted.
1114+
* That breaks a quote because it still refers no a non-existent customer group.
1115+
* In such a case we should load a new customer group id from the current customer
1116+
* object and use it to retrieve tax class and update quote.
1117+
*/
1118+
$groupId = $this->getCustomer()->getGroupId();
1119+
$this->setCustomerGroupId($groupId);
1120+
if ($groupId !== null) {
1121+
$taxClassId = $this->groupRepository->getById($groupId)->getTaxClassId();
1122+
}
1123+
}
11081124
$this->setCustomerTaxClassId($taxClassId);
11091125
}
11101126

app/code/Magento/Quote/Test/Unit/Model/QuoteTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
use Magento\Framework\DataObject\Copy;
3131
use Magento\Framework\DataObject\Factory;
3232
use Magento\Framework\Event\Manager;
33+
use Magento\Framework\Exception\NoSuchEntityException;
3334
use Magento\Framework\Model\Context;
35+
use Magento\Framework\Phrase;
3436
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
3537
use Magento\Quote\Api\Data\CartInterface;
3638
use Magento\Quote\Model\Quote;
@@ -640,6 +642,48 @@ public function testGetCustomerTaxClassId()
640642
$this->assertEquals($taxClassId, $result);
641643
}
642644

645+
/**
646+
* Test case when non-existent customer group is stored into the quote.
647+
* In such a case we should get a NoSuchEntityException exception and try
648+
* to get a valid customer group from the current customer object.
649+
*/
650+
public function testGetCustomerTaxClassIdForNonExistentCustomerGroup()
651+
{
652+
$customerId = 1;
653+
$nonExistentGroupId = 100;
654+
$groupId = 1;
655+
$taxClassId = 1;
656+
$groupMock = $this->getMockForAbstractClass(GroupInterface::class, [], '', false);
657+
$this->groupRepositoryMock->expects($this->at(0))
658+
->method('getById')
659+
->with($nonExistentGroupId)
660+
->willThrowException(new NoSuchEntityException(new Phrase('Entity Id does not exist')));
661+
$customerMock = $this->getMockForAbstractClass(
662+
CustomerInterface::class,
663+
[],
664+
'',
665+
false
666+
);
667+
$customerMock->expects($this->once())
668+
->method('getGroupId')
669+
->willReturn($groupId);
670+
$this->customerRepositoryMock->expects($this->once())
671+
->method('getById')
672+
->with($customerId)
673+
->willReturn($customerMock);
674+
$this->groupRepositoryMock->expects($this->at(1))
675+
->method('getById')
676+
->with($groupId)
677+
->willReturn($groupMock);
678+
$groupMock->expects($this->once())
679+
->method('getTaxClassId')
680+
->willReturn($taxClassId);
681+
$this->quote->setData('customer_id', $customerId);
682+
$this->quote->setData('customer_group_id', $nonExistentGroupId);
683+
$result = $this->quote->getCustomerTaxClassId();
684+
$this->assertEquals($taxClassId, $result);
685+
}
686+
643687
public function testGetAllAddresses()
644688
{
645689
$id = 1;

0 commit comments

Comments
 (0)