66
77namespace Magento \Sales \Block \Adminhtml \Order \Create \Form ;
88
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 ;
913use Magento \Customer \Api \GroupManagementInterface ;
14+ use Magento \Customer \Model \Metadata \Form ;
1015use Magento \Framework \Api \ExtensibleDataObjectConverter ;
1116use Magento \Framework \App \ObjectManager ;
1217use 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 ;
1323use Magento \Framework \Pricing \PriceCurrencyInterface ;
24+ use Magento \Framework \Reflection \DataObjectProcessor ;
25+ use Magento \Sales \Model \AdminOrder \Create ;
26+ use Magento \Store \Model \ScopeInterface ;
1427
1528/**
1629 * Create order account form
@@ -25,46 +38,48 @@ class Account extends AbstractForm
2538 /**
2639 * Metadata form factory
2740 *
28- * @var \Magento\Customer\Model\Metadata\FormFactory
41+ * @var MetadataFormFactory
2942 */
3043 protected $ _metadataFormFactory ;
3144
3245 /**
3346 * Customer repository
3447 *
35- * @var \Magento\Customer\Api\ CustomerRepositoryInterface
48+ * @var CustomerRepositoryInterface
3649 */
3750 protected $ customerRepository ;
3851
3952 /**
40- * @var \Magento\Framework\Api\ ExtensibleDataObjectConverter
53+ * @var ExtensibleDataObjectConverter
4154 */
4255 protected $ _extensibleDataObjectConverter ;
56+
4357 private const XML_PATH_EMAIL_REQUIRED_CREATE_ORDER = 'customer/create_account/email_required_create_order ' ;
58+
4459 /**
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
4863 * @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
5368 * @param ExtensibleDataObjectConverter $extensibleDataObjectConverter
5469 * @param array $data
5570 * @param GroupManagementInterface|null $groupManagement
5671 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
5772 */
5873 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 ,
6277 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 ,
6883 array $ data = [],
6984 ?GroupManagementInterface $ groupManagement = null
7085 ) {
@@ -103,7 +118,7 @@ public function getHeaderCssClass()
103118 /**
104119 * Return header text
105120 *
106- * @return \Magento\Framework\ Phrase
121+ * @return Phrase
107122 */
108123 public function getHeaderText ()
109124 {
@@ -114,10 +129,12 @@ public function getHeaderText()
114129 * Prepare Form and add elements to form
115130 *
116131 * @return $this
132+ * @throws LocalizedException
133+ * @throws NoSuchEntityException
117134 */
118135 protected function _prepareForm ()
119136 {
120- /** @var \Magento\Customer\Model\Metadata\ Form $customerForm */
137+ /** @var Form $customerForm */
121138 $ customerForm = $ this ->_metadataFormFactory ->create ('customer ' , 'adminhtml_checkout ' );
122139
123140 // prepare customer attributes to show
@@ -170,6 +187,8 @@ protected function _addAdditionalFormElementData(AbstractElement $element)
170187 * Return Form Elements values
171188 *
172189 * @return array
190+ * @throws LocalizedException
191+ * @throws NoSuchEntityException
173192 */
174193 public function getFormValues ()
175194 {
@@ -183,7 +202,7 @@ public function getFormValues()
183202 ? $ this ->_extensibleDataObjectConverter ->toFlatArray (
184203 $ customer ,
185204 [],
186- \ Magento \ Customer \ Api \ Data \ CustomerInterface::class
205+ CustomerInterface::class
187206 )
188207 : [];
189208 foreach ($ this ->getQuote ()->getData () as $ key => $ value ) {
@@ -193,7 +212,7 @@ public function getFormValues()
193212 }
194213
195214 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 ();
197216 }
198217
199218 if ($ this ->getQuote ()->getCustomerEmail ()) {
@@ -208,6 +227,8 @@ public function getFormValues()
208227 *
209228 * @param array $attributes
210229 * @return array
230+ * @throws LocalizedException
231+ * @throws NoSuchEntityException
211232 */
212233 private function extractValuesFromAttributes (array $ attributes ): array
213234 {
@@ -231,7 +252,24 @@ private function isEmailRequiredToCreateOrder()
231252 {
232253 return $ this ->_scopeConfig ->getValue (
233254 self ::XML_PATH_EMAIL_REQUIRED_CREATE_ORDER ,
234- \ Magento \ Store \ Model \ ScopeInterface::SCOPE_STORE
255+ ScopeInterface::SCOPE_STORE
235256 );
236257 }
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+ }
237275}
0 commit comments