Skip to content

Commit 6db1257

Browse files
committed
ACP2E-2791: Not able to Save Customer attribute information in Admin Edit customer section;
1 parent 0d51f7b commit 6db1257

File tree

6 files changed

+180
-104
lines changed

6 files changed

+180
-104
lines changed

app/code/Magento/Customer/Controller/Adminhtml/Index/Save.php

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Magento\Customer\Model\EmailNotificationInterface;
2828
use Magento\Customer\Model\Metadata\Form;
2929
use Magento\Customer\Model\Metadata\FormFactory;
30+
use Magento\Customer\Model\SetCustomerStore;
3031
use Magento\Framework\Api\DataObjectHelper;
3132
use Magento\Framework\Api\ExtensibleDataObjectConverter;
3233
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
@@ -46,7 +47,6 @@
4647
use Magento\Framework\View\Result\PageFactory;
4748
use Magento\Newsletter\Model\SubscriberFactory;
4849
use Magento\Newsletter\Model\SubscriptionManagerInterface;
49-
use Magento\Store\Model\StoreManagerInterface;
5050

5151
/**
5252
* Save customer action.
@@ -71,9 +71,9 @@ class Save extends \Magento\Customer\Controller\Adminhtml\Index implements HttpP
7171
private $addressRegistry;
7272

7373
/**
74-
* @var StoreManagerInterface
74+
* @var SetCustomerStore|null
7575
*/
76-
private $storeManager;
76+
private $customerStore;
7777

7878
/**
7979
* Constructor
@@ -105,7 +105,7 @@ class Save extends \Magento\Customer\Controller\Adminhtml\Index implements HttpP
105105
* @param JsonFactory $resultJsonFactory
106106
* @param SubscriptionManagerInterface $subscriptionManager
107107
* @param AddressRegistry|null $addressRegistry
108-
* @param StoreManagerInterface|null $storeManager
108+
* @param SetCustomerStore|null $customerStore
109109
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
110110
*/
111111
public function __construct(
@@ -136,7 +136,7 @@ public function __construct(
136136
JsonFactory $resultJsonFactory,
137137
SubscriptionManagerInterface $subscriptionManager,
138138
AddressRegistry $addressRegistry = null,
139-
?StoreManagerInterface $storeManager = null
139+
?SetCustomerStore $customerStore = null
140140
) {
141141
parent::__construct(
142142
$context,
@@ -167,7 +167,7 @@ public function __construct(
167167
);
168168
$this->subscriptionManager = $subscriptionManager;
169169
$this->addressRegistry = $addressRegistry ?: ObjectManager::getInstance()->get(AddressRegistry::class);
170-
$this->storeManager = $storeManager ?? ObjectManager::getInstance()->get(StoreManagerInterface::class);
170+
$this->customerStore = $customerStore ?? ObjectManager::getInstance()->get(SetCustomerStore::class);
171171
}
172172

173173
/**
@@ -335,7 +335,9 @@ public function execute()
335335

336336
if ($this->getRequest()->getPostValue()) {
337337
try {
338-
$this->setCurrentCustomerStore();
338+
$this->customerStore->setStore(
339+
$this->getRequest()->getPostValue(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER)
340+
);
339341

340342
// optional fields might be set in request for future processing by observers in other modules
341343
$customerData = $this->_extractCustomerData();
@@ -372,13 +374,6 @@ public function execute()
372374
}
373375
}
374376

375-
$storeId = $customer->getStoreId();
376-
if (empty($storeId)) {
377-
$website = $this->storeManager->getWebsite($customer->getWebsiteId());
378-
$storeId = current($website->getStoreIds());
379-
}
380-
$this->storeManager->setCurrentStore($storeId);
381-
382377
// Save customer
383378
if ($customerId) {
384379
$this->_customerRepository->save($customer);
@@ -553,25 +548,6 @@ private function getCurrentCustomerId()
553548
return $customerId;
554549
}
555550

556-
/**
557-
* Set store ID for the current customer.
558-
*
559-
* @return void
560-
*/
561-
private function setCurrentCustomerStore(): void
562-
{
563-
$originalRequestData = $this->getRequest()->getPostValue(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER);
564-
if ($originalRequestData) {
565-
$storeId = $originalRequestData['store_id'] ?? null;
566-
if (!$storeId) {
567-
$websiteId = $originalRequestData['website_id'] ?? null;
568-
$website = $this->storeManager->getWebsite($websiteId);
569-
$storeId = $website ? current($website->getStoreIds()) : null;
570-
}
571-
$this->storeManager->setCurrentStore($storeId);
572-
}
573-
}
574-
575551
/**
576552
* Disable Customer Address Validation
577553
*

app/code/Magento/Customer/Controller/Adminhtml/Index/Validate.php

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Customer\Api\Data\AddressInterfaceFactory;
1313
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
1414
use Magento\Customer\Model\Address\Mapper;
15+
use Magento\Customer\Model\SetCustomerStore;
1516
use Magento\Framework\Api\DataObjectHelper;
1617
use Magento\Framework\App\Action\HttpGetActionInterface;
1718
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
@@ -20,7 +21,6 @@
2021
use Magento\Framework\DataObjectFactory as ObjectFactory;
2122
use Magento\Framework\Message\Error;
2223
use Magento\Customer\Controller\Adminhtml\Index as CustomerAction;
23-
use Magento\Store\Model\StoreManagerInterface;
2424

2525
/**
2626
* Class for validation of customer
@@ -30,9 +30,9 @@
3030
class Validate extends CustomerAction implements HttpPostActionInterface, HttpGetActionInterface
3131
{
3232
/**
33-
* @var StoreManagerInterface
33+
* @var SetCustomerStore|null
3434
*/
35-
private $storeManager;
35+
private $customerStore;
3636

3737
/**
3838
* @param \Magento\Backend\App\Action\Context $context
@@ -60,7 +60,7 @@ class Validate extends CustomerAction implements HttpPostActionInterface, HttpGe
6060
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
6161
* @param \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
6262
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
63-
* @param StoreManagerInterface|null $storeManager
63+
* @param SetCustomerStore|null $customerStore
6464
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
6565
*/
6666
public function __construct(
@@ -89,7 +89,7 @@ public function __construct(
8989
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
9090
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
9191
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
92-
?StoreManagerInterface $storeManager = null
92+
?SetCustomerStore $customerStore = null
9393
) {
9494
parent::__construct(
9595
$context,
@@ -118,8 +118,7 @@ public function __construct(
118118
$resultForwardFactory,
119119
$resultJsonFactory
120120
);
121-
122-
$this->storeManager = $storeManager ?? ObjectManager::getInstance()->get(StoreManagerInterface::class);
121+
$this->customerStore = $customerStore ?? ObjectManager::getInstance()->get(SetCustomerStore::class);
123122
}
124123

125124
/**
@@ -146,7 +145,9 @@ protected function _validateCustomer($response)
146145
);
147146
$customerForm->setInvisibleIgnored(true);
148147

149-
$this->setCurrentCustomerStore();
148+
$this->customerStore->setStore(
149+
$this->getRequest()->getParam(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER)
150+
);
150151
$data = $customerForm->extractData($this->getRequest(), 'customer');
151152

152153
if ($customer->getWebsiteId()) {
@@ -203,23 +204,4 @@ public function execute()
203204
$resultJson->setData($response);
204205
return $resultJson;
205206
}
206-
207-
/**
208-
* Set store ID for the current customer.
209-
*
210-
* @return void
211-
*/
212-
private function setCurrentCustomerStore(): void
213-
{
214-
$requestData = $this->getRequest()->getParam(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER);
215-
if ($requestData) {
216-
$storeId = $requestData['store_id'] ?? null;
217-
if (!$storeId) {
218-
$websiteId = $requestData['website_id'] ?? null;
219-
$website = $this->storeManager->getWebsite($websiteId);
220-
$storeId = $website ? current($website->getStoreIds()) : null;
221-
}
222-
$this->storeManager->setCurrentStore($storeId);
223-
}
224-
}
225207
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2023 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* ************************************************************************
16+
*/
17+
declare(strict_types=1);
18+
19+
namespace Magento\Customer\Model;
20+
21+
use Magento\Customer\Api\Data\CustomerInterface;
22+
use Magento\Framework\Exception\LocalizedException;
23+
use Magento\Store\Model\StoreManagerInterface;
24+
25+
class SetCustomerStore
26+
{
27+
/**
28+
* @param StoreManagerInterface $storeManager
29+
*/
30+
public function __construct(private StoreManagerInterface $storeManager)
31+
{
32+
}
33+
34+
/**
35+
* Set store ID for the current customer.
36+
*
37+
* @param array|null $requestData
38+
* @return void
39+
*/
40+
public function setStore(array|null $requestData = null): void
41+
{
42+
$storeId = $requestData[CustomerInterface::STORE_ID] ?? null;
43+
if (!$storeId) {
44+
$websiteId = $requestData[CustomerInterface::WEBSITE_ID] ?? null;
45+
try {
46+
$website = $this->storeManager->getWebsite($websiteId);
47+
$storeId = $website ? current($website->getStoreIds()) : null;
48+
} catch (LocalizedException $e) {
49+
$storeId = null;
50+
}
51+
}
52+
$this->storeManager->setCurrentStore($storeId);
53+
}
54+
}

app/code/Magento/Customer/Test/Unit/Controller/Adminhtml/Index/SaveTest.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Magento\Customer\Model\EmailNotificationInterface;
2727
use Magento\Customer\Model\Metadata\Form;
2828
use Magento\Customer\Model\Metadata\FormFactory;
29+
use Magento\Customer\Model\SetCustomerStore;
2930
use Magento\Framework\Api\DataObjectHelper;
3031
use Magento\Framework\App\Request\Http;
3132
use Magento\Framework\App\RequestInterface;
@@ -44,8 +45,6 @@
4445
use Magento\Framework\View\Result\PageFactory;
4546
use Magento\Newsletter\Model\SubscriberFactory;
4647
use Magento\Newsletter\Model\SubscriptionManagerInterface;
47-
use Magento\Store\Api\Data\WebsiteInterface;
48-
use Magento\Store\Model\StoreManagerInterface;
4948
use PHPUnit\Framework\MockObject\MockObject;
5049
use PHPUnit\Framework\TestCase;
5150

@@ -286,14 +285,9 @@ protected function setUp(): void
286285
$this->emailNotificationMock = $this->getMockBuilder(EmailNotificationInterface::class)
287286
->disableOriginalConstructor()
288287
->getMockForAbstractClass();
289-
$website = $this->createPartialMock(\Magento\Store\Model\Website::class, ['getStoreIds']);
290-
$website->expects($this->exactly(2))->method('getStoreIds')
291-
->willReturn([1]);
292-
$storeManager = $this->getMockBuilder(StoreManagerInterface::class)
293-
->getMockForAbstractClass();
294-
$storeManager->expects($this->exactly(2))->method('getWebsite')
295-
->willReturn($website);
296-
$storeManager->expects($this->exactly(2))->method('setCurrentStore')->with(1);
288+
289+
$customerStoreMock = $this->createMock(SetCustomerStore::class);
290+
$customerStoreMock->expects($this->once())->method('setStore');
297291

298292
$objectManager = new ObjectManager($this);
299293

@@ -320,7 +314,7 @@ protected function setUp(): void
320314
'addressRepository' => $this->customerAddressRepositoryMock,
321315
'addressMapper' => $this->customerAddressMapperMock,
322316
'subscriptionManager' => $this->subscriptionManager,
323-
'storeManager' => $storeManager,
317+
'customerStore' => $customerStoreMock
324318
]
325319
);
326320

@@ -348,7 +342,6 @@ public function testExecuteWithExistentCustomer()
348342
'code' => 'value',
349343
'coolness' => false,
350344
'disable_auto_group_change' => 'false',
351-
'website_id' => 1
352345
],
353346
'subscription_status' => [$subscriptionWebsite => $subscriptionStatus],
354347
'subscription_store' => [$subscriptionWebsite => $subscriptionStore],
@@ -358,14 +351,12 @@ public function testExecuteWithExistentCustomer()
358351
'code' => 'value',
359352
'coolness' => false,
360353
'disable_auto_group_change' => 'false',
361-
'website_id' => 1
362354
];
363355
$compactedData = [
364356
'entity_id' => $customerId,
365357
'code' => 'value',
366358
'coolness' => false,
367359
'disable_auto_group_change' => 'false',
368-
'website_id' => 1,
369360
CustomerInterface::DEFAULT_BILLING => 2,
370361
CustomerInterface::DEFAULT_SHIPPING => 2
371362
];
@@ -385,7 +376,6 @@ public function testExecuteWithExistentCustomer()
385376
'confirmation' => false,
386377
'sendemail_store_id' => '1',
387378
'id' => $customerId,
388-
'website_id' => 1
389379
];
390380

391381
/** @var AttributeMetadataInterface|MockObject $customerFormMock */

0 commit comments

Comments
 (0)