Skip to content

Commit f5aeea6

Browse files
committed
MAGETWO-91639: Tax is added despite customer group changes
- Add recollect quote after customer group change
1 parent ca42048 commit f5aeea6

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Plugin;
9+
10+
use Magento\Quote\Api\CartRepositoryInterface;
11+
use Magento\Customer\Model\ResourceModel\Customer as CustomerResource;
12+
use Magento\Customer\Model\Customer;
13+
14+
/**
15+
* Recollect quote totals after change customer group
16+
*/
17+
class RecollectOnGroupChange
18+
{
19+
/**
20+
* @var CartRepositoryInterface
21+
*/
22+
private $cartRepository;
23+
24+
/**
25+
* @param CartRepositoryInterface $cartRepository
26+
*/
27+
public function __construct(
28+
CartRepositoryInterface $cartRepository
29+
) {
30+
$this->cartRepository = $cartRepository;
31+
}
32+
33+
/**
34+
* Recollect totals if customer group change
35+
*
36+
* @param CustomerResource $subject
37+
* @param CustomerResource $result
38+
* @param Customer $customer
39+
* @return CustomerResource
40+
*
41+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
42+
*/
43+
public function afterSave(CustomerResource $subject, CustomerResource $result, Customer $customer)
44+
{
45+
if ($customer->getOrigData('group_id') !== null
46+
&& $customer->getOrigData('group_id') != $customer->getGroupId()
47+
) {
48+
try {
49+
/** @var \Magento\Quote\Model\Quote $quote */
50+
$quote = $this->cartRepository->getActiveForCustomer($customer->getId());
51+
$quote->setCustomerGroupId($customer->getGroupId());
52+
$quote->collectTotals();
53+
$this->cartRepository->save($quote);
54+
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
55+
//no active cart for customer
56+
}
57+
}
58+
59+
return $result;
60+
}
61+
}

app/code/Magento/Quote/etc/frontend/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@
1515
<type name="Magento\Store\Api\StoreCookieManagerInterface">
1616
<plugin name="update_quote_store_after_switch_store_view" type="Magento\Quote\Plugin\UpdateQuoteStore"/>
1717
</type>
18+
<type name="Magento\Customer\Model\ResourceModel\Customer">
19+
<plugin name="cart_recollect_on_group_change" type="Magento\Quote\Plugin\RecollectOnGroupChange"/>
20+
</type>
1821
</config>

0 commit comments

Comments
 (0)