Skip to content

Commit 2e56980

Browse files
committed
ACP2E-278: [Magento Cloud] Issue with price tiers / user groups with Rest API
- Plugin with test
1 parent 203a44f commit 2e56980

File tree

3 files changed

+145
-5
lines changed

3 files changed

+145
-5
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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\Checkout\Model\Plugin;
9+
10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Customer\Model\Customer;
12+
use Magento\Customer\Model\ResourceModel\Customer as CustomerResource;
13+
use Magento\Framework\Exception\LocalizedException;
14+
use Magento\Framework\Exception\NoSuchEntityException;
15+
use Magento\Framework\Model\AbstractModel;
16+
use Magento\Quote\Api\CartRepositoryInterface;
17+
use Magento\Quote\Model\Quote;
18+
19+
/**
20+
* Recollect quote when customer group updated through API
21+
*/
22+
class RecollectQuoteOnCustomerGroupChange
23+
{
24+
/**
25+
* @var CartRepositoryInterface
26+
*/
27+
private $cartRepository;
28+
29+
/**
30+
* @var CustomerRepositoryInterface
31+
*/
32+
private $customerRepository;
33+
34+
/**
35+
* Initialize Constructor
36+
*
37+
* @param CartRepositoryInterface $cartRepository
38+
* @param CustomerRepositoryInterface $customerRepository
39+
*/
40+
public function __construct(
41+
CartRepositoryInterface $cartRepository,
42+
CustomerRepositoryInterface $customerRepository
43+
) {
44+
$this->cartRepository = $cartRepository;
45+
$this->customerRepository = $customerRepository;
46+
}
47+
48+
/**
49+
* Plugin around create customer that triggers to update and recollect all customer cart
50+
*
51+
* @param CustomerResource $subject
52+
* @param callable $proceed
53+
* @param AbstractModel $customer
54+
* @return CustomerResource
55+
*
56+
* @throws LocalizedException
57+
* @throws NoSuchEntityException
58+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
59+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
60+
*/
61+
public function aroundSave(
62+
CustomerResource $subject,
63+
callable $proceed,
64+
AbstractModel $customer
65+
): CustomerResource {
66+
$customerId = $customer->getId() ?: $customer->getEntityId();
67+
/** @var Customer $customer */
68+
if ($customerId && empty($customer->getTaxvat())) {
69+
try {
70+
$prevCustomerData = $this->customerRepository->getById($customerId);
71+
$previousCustomerData = $prevCustomerData->__toArray();
72+
} catch (NoSuchEntityException $e) {
73+
$previousCustomerData = [];
74+
}
75+
}
76+
77+
$result = $proceed($customer);
78+
79+
if (!empty($previousCustomerData)
80+
&& $previousCustomerData['group_id'] !== null
81+
&& $previousCustomerData['group_id'] != $customer->getGroupId()
82+
&& empty($previousCustomerData['taxvat'])
83+
) {
84+
try {
85+
/** @var Quote $quote */
86+
$quote = $this->cartRepository->getActiveForCustomer($customer->getId());
87+
$quote->setCustomerGroupId($customer->getGroupId());
88+
$quote->collectTotals();
89+
$this->cartRepository->save($quote);
90+
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
91+
} catch (NoSuchEntityException $e) {
92+
//no active cart for customer
93+
}
94+
}
95+
96+
return $result;
97+
}
98+
}

app/code/Magento/Checkout/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,7 @@
5353
type="Magento\Checkout\Model\CaptchaPaymentProcessingRateLimiter" />
5454
<preference for="Magento\Checkout\Api\PaymentSavingRateLimiterInterface"
5555
type="Magento\Checkout\Model\CaptchaPaymentSavingRateLimiter" />
56+
<type name="Magento\Customer\Model\ResourceModel\Customer">
57+
<plugin name="recollect_quote_on_customer_group_change" type="Magento\Checkout\Model\Plugin\RecollectQuoteOnCustomerGroupChange"/>
58+
</type>
5659
</config>

dev/tests/api-functional/testsuite/Magento/Customer/Api/CustomerRepositoryTest.php

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@
3838
*/
3939
class CustomerRepositoryTest extends WebapiAbstract
4040
{
41-
const SERVICE_VERSION = 'V1';
42-
const SERVICE_NAME = 'customerCustomerRepositoryV1';
43-
const RESOURCE_PATH = '/V1/customers';
41+
public const SERVICE_VERSION = 'V1';
42+
public const SERVICE_NAME = 'customerCustomerRepositoryV1';
43+
public const RESOURCE_PATH = '/V1/customers';
4444

4545
private const STUB_INVALID_CUSTOMER_GROUP_ID = 777;
4646

47+
private const STUB_RETAILER_GROUP_ID = 3;
48+
4749
/**
4850
* @var CustomerRepositoryInterface
4951
*/
@@ -517,6 +519,40 @@ public function testUpdateCustomerWithInvalidGroupId(): void
517519
}
518520
}
519521

522+
/**
523+
* Test customer update quote with valid customer group id change
524+
*
525+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
526+
*
527+
* @return void
528+
*/
529+
public function testUpdateCustomerQuoteOnGroupIdChange(): void
530+
{
531+
$customerId = 1;
532+
$customerData = $this->dataObjectProcessor->buildOutputDataArray(
533+
$this->getCustomerData($customerId),
534+
Customer::class
535+
);
536+
$customerData[Customer::GROUP_ID] = self::STUB_RETAILER_GROUP_ID;
537+
538+
$serviceInfo = [
539+
'rest' => [
540+
'resourcePath' => self::RESOURCE_PATH . '/' . $customerId,
541+
'httpMethod' => Request::HTTP_METHOD_PUT,
542+
],
543+
'soap' => [
544+
'service' => self::SERVICE_NAME,
545+
'serviceVersion' => self::SERVICE_VERSION,
546+
'operation' => self::SERVICE_NAME . 'Save',
547+
],
548+
];
549+
550+
$requestData['customer'] = $customerData;
551+
552+
$updateResults = $this->_webApiCall($serviceInfo, $requestData);
553+
$this->assertEquals($updateResults['group_id'], self::STUB_RETAILER_GROUP_ID);
554+
}
555+
520556
/**
521557
* Test customer create with invalid customer group id
522558
*
@@ -1069,8 +1105,11 @@ protected function _createCustomer(?array $additionalData = [])
10691105
*
10701106
* @dataProvider customerDataProvider
10711107
*/
1072-
public function testCreateCustomerWithInvalidCustomerFirstName(string $fieldName, string $fieldValue, string $expectedMessage): void
1073-
{
1108+
public function testCreateCustomerWithInvalidCustomerFirstName(
1109+
string $fieldName,
1110+
string $fieldValue,
1111+
string $expectedMessage
1112+
): void {
10741113
$customerData = $this->dataObjectProcessor->buildOutputDataArray(
10751114
$this->customerHelper->createSampleCustomerDataObject(),
10761115
Customer::class

0 commit comments

Comments
 (0)