Skip to content

Commit 1bd7aa6

Browse files
committed
Merge remote-tracking branch 'origin/ACP2E-4072' into PR_2025_09_19_flowers
2 parents 588eac3 + e745aed commit 1bd7aa6

File tree

6 files changed

+132
-19
lines changed

6 files changed

+132
-19
lines changed

app/code/Magento/Customer/Model/Vat.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Customer\Model;
77

@@ -139,14 +139,14 @@ public function getCustomerGroupIdBasedOnVatNumber($customerCountryCode, $vatVal
139139
];
140140

141141
if (isset($vatClassToGroupXmlPathMap[$vatClass])) {
142-
$groupId = (int)$this->scopeConfig->getValue(
142+
$groupId = $this->scopeConfig->getValue(
143143
$vatClassToGroupXmlPathMap[$vatClass],
144144
ScopeInterface::SCOPE_STORE,
145145
$store
146146
);
147147
}
148148

149-
return $groupId;
149+
return $groupId ? (int)$groupId : null;
150150
}
151151

152152
/**
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Model;
9+
10+
use Magento\Framework\DataObject;
11+
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
12+
13+
/**
14+
* Storage for validated VAT data
15+
*/
16+
class VatValidationResultStorage extends DataObject implements ResetAfterRequestInterface
17+
{
18+
/**
19+
* Store VAT validated data by key
20+
*
21+
* @param string|int $vatNumber
22+
* @param string $countryCode
23+
* @param DataObject $vatValidationResult
24+
* @return void
25+
*/
26+
public function set(string|int $vatNumber, string $countryCode, DataObject $vatValidationResult): void
27+
{
28+
$this->setData($vatNumber . $countryCode, $vatValidationResult);
29+
}
30+
31+
/**
32+
* Retrieve VAT validated data by key
33+
*
34+
* @param string|int $vatNumber
35+
* @param string $countryCode
36+
* @return DataObject|null
37+
*/
38+
public function get(string|int $vatNumber, string $countryCode): ?DataObject
39+
{
40+
return $this->getData($vatNumber . $countryCode);
41+
}
42+
43+
/**
44+
* @inheritDoc
45+
*/
46+
public function _resetState(): void
47+
{
48+
$this->setData([]);
49+
}
50+
}

app/code/Magento/Customer/Observer/AfterAddressSaveObserver.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -159,7 +159,10 @@ public function execute(Observer $observer)
159159
$customer->getStore()
160160
);
161161

162-
if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $newGroupId) {
162+
if (!$customer->getDisableAutoGroupChange() &&
163+
$newGroupId !== null &&
164+
$customer->getGroupId() != $newGroupId
165+
) {
163166
$customer->setGroupId($newGroupId);
164167
$customer->save();
165168
$this->customerSession->setCustomerGroupId($newGroupId);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Plugin\Model;
9+
10+
use Magento\Customer\Model\Vat;
11+
use Magento\Customer\Model\VatValidationResultStorage;
12+
13+
/**
14+
* Plugin to cache the VAT number validation result based on provided VAT number and country code
15+
*/
16+
class VatPlugin
17+
{
18+
/**
19+
* @param VatValidationResultStorage $vatValidationResultStorage
20+
*/
21+
public function __construct(
22+
private readonly VatValidationResultStorage $vatValidationResultStorage
23+
) {
24+
}
25+
26+
/**
27+
* Cache VAT number validation result
28+
*
29+
* @param Vat $subject
30+
* @param \Closure $proceed
31+
* @param string $countryCode
32+
* @param string $vatNumber
33+
* @param string $requesterCountryCode
34+
* @param string $requesterVatNumber
35+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
36+
*/
37+
public function aroundCheckVatNumber(
38+
Vat $subject,
39+
\Closure $proceed,
40+
$countryCode,
41+
$vatNumber,
42+
$requesterCountryCode = '',
43+
$requesterVatNumber = ''
44+
) {
45+
$storedValidationResult = $this->vatValidationResultStorage->get($vatNumber, $countryCode);
46+
47+
if ($storedValidationResult) {
48+
return $storedValidationResult;
49+
}
50+
$validationResult = $proceed($countryCode, $vatNumber, $requesterCountryCode, $requesterVatNumber);
51+
52+
if ($validationResult->getRequestSuccess()) {
53+
$this->vatValidationResultStorage->set($vatNumber, $countryCode, $validationResult);
54+
}
55+
56+
return $validationResult;
57+
}
58+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,7 @@
133133
<type name="Magento\Customer\Api\CustomerRepositoryInterface">
134134
<plugin name="customer_dobvalidation_plugin" type="Magento\Customer\Plugin\ValidateDobOnSave" sortOrder="10"/>
135135
</type>
136+
<type name="Magento\Customer\Model\Vat">
137+
<plugin name="cache_vat_validation_result" type="Magento\Customer\Plugin\Model\VatPlugin"/>
138+
</type>
136139
</config>

app/code/Magento/Quote/Observer/Frontend/Quote/Address/VatValidator.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2013 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Quote\Observer\Frontend\Quote\Address;
77

8+
use Magento\Customer\Helper\Address;
9+
use Magento\Customer\Model\Vat;
10+
811
class VatValidator
912
{
1013
/**
11-
* Customer address
12-
*
13-
* @var \Magento\Customer\Helper\Address
14+
* @var Address
1415
*/
1516
protected $customerAddress;
1617

1718
/**
18-
* Customer VAT
19-
*
20-
* @var \Magento\Customer\Model\Vat
19+
* @var Vat
2120
*/
2221
protected $customerVat;
2322

2423
/**
25-
* @param \Magento\Customer\Helper\Address $customerAddress
26-
* @param \Magento\Customer\Model\Vat $customerVat
24+
* @param Address $customerAddress
25+
* @param Vat $customerVat
2726
*/
2827
public function __construct(
29-
\Magento\Customer\Helper\Address $customerAddress,
30-
\Magento\Customer\Model\Vat $customerVat
28+
Address $customerAddress,
29+
Vat $customerVat
3130
) {
3231
$this->customerVat = $customerVat;
3332
$this->customerAddress = $customerAddress;

0 commit comments

Comments
 (0)