6
6
7
7
namespace Magento \Sales \Block \Adminhtml \Order \Create \Form ;
8
8
9
+ use Magento \Backend \Block \Template \Context ;
10
+ use Magento \Backend \Model \Session \Quote ;
11
+ use Magento \Customer \Api \CustomerRepositoryInterface ;
12
+ use Magento \Customer \Api \Data \CustomerInterface ;
9
13
use Magento \Customer \Api \GroupManagementInterface ;
14
+ use Magento \Customer \Model \Metadata \Form ;
10
15
use Magento \Framework \Api \ExtensibleDataObjectConverter ;
11
16
use Magento \Framework \App \ObjectManager ;
12
17
use Magento \Framework \Data \Form \Element \AbstractElement ;
18
+ use Magento \Framework \Data \FormFactory ;
19
+ use Magento \Customer \Model \Metadata \FormFactory as MetadataFormFactory ;
20
+ use Magento \Framework \Exception \LocalizedException ;
21
+ use Magento \Framework \Exception \NoSuchEntityException ;
22
+ use Magento \Framework \Phrase ;
13
23
use Magento \Framework \Pricing \PriceCurrencyInterface ;
24
+ use Magento \Framework \Reflection \DataObjectProcessor ;
25
+ use Magento \Sales \Model \AdminOrder \Create ;
26
+ use Magento \Store \Model \ScopeInterface ;
14
27
15
28
/**
16
29
* Create order account form
@@ -25,46 +38,48 @@ class Account extends AbstractForm
25
38
/**
26
39
* Metadata form factory
27
40
*
28
- * @var \Magento\Customer\Model\Metadata\FormFactory
41
+ * @var MetadataFormFactory
29
42
*/
30
43
protected $ _metadataFormFactory ;
31
44
32
45
/**
33
46
* Customer repository
34
47
*
35
- * @var \Magento\Customer\Api\ CustomerRepositoryInterface
48
+ * @var CustomerRepositoryInterface
36
49
*/
37
50
protected $ customerRepository ;
38
51
39
52
/**
40
- * @var \Magento\Framework\Api\ ExtensibleDataObjectConverter
53
+ * @var ExtensibleDataObjectConverter
41
54
*/
42
55
protected $ _extensibleDataObjectConverter ;
56
+
43
57
private const XML_PATH_EMAIL_REQUIRED_CREATE_ORDER = 'customer/create_account/email_required_create_order ' ;
58
+
44
59
/**
45
- * @param \Magento\Backend\Block\Template\ Context $context
46
- * @param \Magento\Backend\Model\Session\ Quote $sessionQuote
47
- * @param \Magento\Sales\Model\AdminOrder\ Create $orderCreate
60
+ * @param Context $context
61
+ * @param Quote $sessionQuote
62
+ * @param Create $orderCreate
48
63
* @param PriceCurrencyInterface $priceCurrency
49
- * @param \Magento\Framework\Data\ FormFactory $formFactory
50
- * @param \Magento\Framework\Reflection\ DataObjectProcessor $dataObjectProcessor
51
- * @param \Magento\Customer\Model\Metadata\FormFactory $metadataFormFactory
52
- * @param \Magento\Customer\Api\ CustomerRepositoryInterface $customerRepository
64
+ * @param FormFactory $formFactory
65
+ * @param DataObjectProcessor $dataObjectProcessor
66
+ * @param MetadataFormFactory $metadataFormFactory
67
+ * @param CustomerRepositoryInterface $customerRepository
53
68
* @param ExtensibleDataObjectConverter $extensibleDataObjectConverter
54
69
* @param array $data
55
70
* @param GroupManagementInterface|null $groupManagement
56
71
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
57
72
*/
58
73
public function __construct (
59
- \ Magento \ Backend \ Block \ Template \ Context $ context ,
60
- \ Magento \ Backend \ Model \ Session \ Quote $ sessionQuote ,
61
- \ Magento \ Sales \ Model \ AdminOrder \ Create $ orderCreate ,
74
+ Context $ context ,
75
+ Quote $ sessionQuote ,
76
+ Create $ orderCreate ,
62
77
PriceCurrencyInterface $ priceCurrency ,
63
- \ Magento \ Framework \ Data \ FormFactory $ formFactory ,
64
- \ Magento \ Framework \ Reflection \ DataObjectProcessor $ dataObjectProcessor ,
65
- \ Magento \ Customer \ Model \ Metadata \ FormFactory $ metadataFormFactory ,
66
- \ Magento \ Customer \ Api \ CustomerRepositoryInterface $ customerRepository ,
67
- \ Magento \ Framework \ Api \ ExtensibleDataObjectConverter $ extensibleDataObjectConverter ,
78
+ FormFactory $ formFactory ,
79
+ DataObjectProcessor $ dataObjectProcessor ,
80
+ MetadataFormFactory $ metadataFormFactory ,
81
+ CustomerRepositoryInterface $ customerRepository ,
82
+ ExtensibleDataObjectConverter $ extensibleDataObjectConverter ,
68
83
array $ data = [],
69
84
?GroupManagementInterface $ groupManagement = null
70
85
) {
@@ -103,7 +118,7 @@ public function getHeaderCssClass()
103
118
/**
104
119
* Return header text
105
120
*
106
- * @return \Magento\Framework\ Phrase
121
+ * @return Phrase
107
122
*/
108
123
public function getHeaderText ()
109
124
{
@@ -114,10 +129,12 @@ public function getHeaderText()
114
129
* Prepare Form and add elements to form
115
130
*
116
131
* @return $this
132
+ * @throws LocalizedException
133
+ * @throws NoSuchEntityException
117
134
*/
118
135
protected function _prepareForm ()
119
136
{
120
- /** @var \Magento\Customer\Model\Metadata\ Form $customerForm */
137
+ /** @var Form $customerForm */
121
138
$ customerForm = $ this ->_metadataFormFactory ->create ('customer ' , 'adminhtml_checkout ' );
122
139
123
140
// prepare customer attributes to show
@@ -170,6 +187,8 @@ protected function _addAdditionalFormElementData(AbstractElement $element)
170
187
* Return Form Elements values
171
188
*
172
189
* @return array
190
+ * @throws LocalizedException
191
+ * @throws NoSuchEntityException
173
192
*/
174
193
public function getFormValues ()
175
194
{
@@ -183,7 +202,7 @@ public function getFormValues()
183
202
? $ this ->_extensibleDataObjectConverter ->toFlatArray (
184
203
$ customer ,
185
204
[],
186
- \ Magento \ Customer \ Api \ Data \ CustomerInterface::class
205
+ CustomerInterface::class
187
206
)
188
207
: [];
189
208
foreach ($ this ->getQuote ()->getData () as $ key => $ value ) {
@@ -193,7 +212,7 @@ public function getFormValues()
193
212
}
194
213
195
214
if (array_key_exists ('group_id ' , $ data ) && empty ($ data ['group_id ' ])) {
196
- $ data ['group_id ' ] = $ this ->groupManagement -> getDefaultGroup ( $ this -> getQuote ()-> getStoreId ())-> getId ();
215
+ $ data ['group_id ' ] = $ this ->getSelectedGroupId ();
197
216
}
198
217
199
218
if ($ this ->getQuote ()->getCustomerEmail ()) {
@@ -208,6 +227,8 @@ public function getFormValues()
208
227
*
209
228
* @param array $attributes
210
229
* @return array
230
+ * @throws LocalizedException
231
+ * @throws NoSuchEntityException
211
232
*/
212
233
private function extractValuesFromAttributes (array $ attributes ): array
213
234
{
@@ -231,7 +252,24 @@ private function isEmailRequiredToCreateOrder()
231
252
{
232
253
return $ this ->_scopeConfig ->getValue (
233
254
self ::XML_PATH_EMAIL_REQUIRED_CREATE_ORDER ,
234
- \ Magento \ Store \ Model \ ScopeInterface::SCOPE_STORE
255
+ ScopeInterface::SCOPE_STORE
235
256
);
236
257
}
258
+
259
+ /**
260
+ * Retrieve selected group id
261
+ *
262
+ * @return string
263
+ * @throws LocalizedException
264
+ * @throws NoSuchEntityException
265
+ */
266
+ private function getSelectedGroupId (): string
267
+ {
268
+ $ selectedGroupId = $ this ->groupManagement ->getDefaultGroup ($ this ->getQuote ()->getStoreId ())->getId ();
269
+ $ orderDetails = $ this ->getRequest ()->getParam ('order ' );
270
+ if (!empty ($ orderDetails ) && !empty ($ orderDetails ['account ' ]['group_id ' ])) {
271
+ $ selectedGroupId = $ orderDetails ['account ' ]['group_id ' ];
272
+ }
273
+ return $ selectedGroupId ;
274
+ }
237
275
}
0 commit comments