9
9
use Magento \Backend \App \Action ;
10
10
use Magento \Customer \Api \CustomerRepositoryInterface ;
11
11
use Magento \Customer \Model \Config \Share ;
12
+ use Magento \Customer \Model \CustomerRegistry ;
12
13
use Magento \Framework \App \Action \HttpGetActionInterface ;
13
14
use Magento \Framework \App \Action \HttpPostActionInterface as HttpPostActionInterface ;
14
15
use Magento \Framework \App \ObjectManager ;
@@ -44,40 +45,40 @@ class Validate extends Action implements HttpPostActionInterface, HttpGetActionI
44
45
private $ shareConfig ;
45
46
46
47
/**
47
- * @var CustomerRepositoryInterface
48
+ * @var StoreManagerInterface
48
49
*/
49
- private $ customerRepository ;
50
+ private $ storeManager ;
50
51
51
52
/**
52
- * @var StoreManagerInterface
53
+ * @var CustomerRegistry
53
54
*/
54
- private $ storeManager ;
55
+ private $ customerRegistry ;
55
56
56
57
/**
57
58
* @param Action\Context $context
58
59
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
59
60
* @param \Magento\Customer\Model\Metadata\FormFactory $formFactory
60
61
* @param Share|null $shareConfig
61
- * @param CustomerRepositoryInterface|null $customerRepository
62
62
* @param StoreManagerInterface|null $storeManager
63
+ * @param CustomerRegistry|null $customerRegistry
63
64
*/
64
65
public function __construct (
65
66
Action \Context $ context ,
66
67
\Magento \Framework \Controller \Result \JsonFactory $ resultJsonFactory ,
67
68
\Magento \Customer \Model \Metadata \FormFactory $ formFactory ,
68
69
?Share $ shareConfig = null ,
69
- ?CustomerRepositoryInterface $ customerRepository = null ,
70
- ?StoreManagerInterface $ storeManager = null
70
+ ?StoreManagerInterface $ storeManager = null ,
71
+ ?CustomerRegistry $ customerRegistry = null
71
72
) {
72
73
parent ::__construct ($ context );
73
74
$ this ->resultJsonFactory = $ resultJsonFactory ;
74
75
$ this ->formFactory = $ formFactory ;
75
76
$ this ->shareConfig = $ shareConfig
76
77
?? ObjectManager::getInstance ()->get (Share::class);
77
- $ this ->customerRepository = $ customerRepository
78
- ?? ObjectManager::getInstance ()->get (CustomerRepositoryInterface::class);
79
78
$ this ->storeManager = $ storeManager
80
79
?? ObjectManager::getInstance ()->get (StoreManagerInterface::class);
80
+ $ this ->customerRegistry = $ customerRegistry
81
+ ?? ObjectManager::getInstance ()->get (CustomerRegistry::class);
81
82
}
82
83
83
84
/**
@@ -87,6 +88,13 @@ public function __construct(
87
88
*/
88
89
public function execute (): Json
89
90
{
91
+ $ customerId = $ this ->getRequest ()->getParam ('parent_id ' );
92
+ if ($ customerId ) {
93
+ $ customerModel = $ this ->customerRegistry ->retrieve ($ customerId );
94
+ if (!$ this ->shareConfig ->isGlobalScope () && $ customerModel ->getStoreId ()) {
95
+ $ this ->storeManager ->setCurrentStore ($ customerModel ->getStoreId ());
96
+ }
97
+ }
90
98
/** @var \Magento\Framework\DataObject $response */
91
99
$ response = new \Magento \Framework \DataObject ();
92
100
$ response ->setError (false );
@@ -113,12 +121,6 @@ public function execute(): Json
113
121
private function validateCustomerAddress (DataObject $ response ): DataObject
114
122
{
115
123
$ addressForm = $ this ->formFactory ->create ('customer_address ' , 'adminhtml_customer_address ' );
116
- if ($ this ->getRequest ()->getParam ('parent_id ' )) {
117
- $ customer = $ this ->customerRepository ->getById ($ this ->getRequest ()->getParam ('parent_id ' ));
118
- if (!$ this ->shareConfig ->isGlobalScope () && $ customer ->getStoreId ()) {
119
- $ this ->storeManager ->setCurrentStore ($ customer ->getStoreId ());
120
- }
121
- }
122
124
$ formData = $ addressForm ->extractData ($ this ->getRequest ());
123
125
$ errors = $ addressForm ->validateData ($ formData );
124
126
if ($ errors !== true ) {
0 commit comments