8
8
namespace Magento \Quote \Model ;
9
9
10
10
use Magento \Authorization \Model \UserContextInterface ;
11
+ use Magento \Checkout \Model \Session as CheckoutSession ;
12
+ use Magento \Customer \Api \AccountManagementInterface ;
13
+ use Magento \Customer \Api \AddressRepositoryInterface ;
14
+ use Magento \Customer \Api \CustomerRepositoryInterface ;
11
15
use Magento \Customer \Api \Data \GroupInterface ;
16
+ use Magento \Customer \Model \CustomerFactory ;
17
+ use Magento \Customer \Model \Session as CustomerSession ;
18
+ use Magento \Framework \Api \DataObjectHelper ;
12
19
use Magento \Framework \App \ObjectManager ;
20
+ use Magento \Framework \App \RequestInterface ;
13
21
use Magento \Framework \Event \ManagerInterface as EventManager ;
14
22
use Magento \Framework \Exception \CouldNotSaveException ;
15
23
use Magento \Framework \Exception \LocalizedException ;
24
+ use Magento \Framework \Exception \NoSuchEntityException ;
16
25
use Magento \Framework \Exception \StateException ;
26
+ use Magento \Framework \HTTP \PhpEnvironment \RemoteAddress ;
27
+ use Magento \Framework \Model \AbstractExtensibleModel ;
28
+ use Magento \Payment \Model \Method \AbstractMethod ;
29
+ use Magento \Quote \Api \CartManagementInterface ;
30
+ use Magento \Quote \Api \CartRepositoryInterface ;
17
31
use Magento \Quote \Api \Data \PaymentInterface ;
18
32
use Magento \Quote \Model \Quote \Address \ToOrder as ToOrderConverter ;
19
33
use Magento \Quote \Model \Quote \Address \ToOrderAddress as ToOrderAddressConverter ;
20
34
use Magento \Quote \Model \Quote as QuoteEntity ;
35
+ use Magento \Quote \Model \Quote \AddressFactory ;
21
36
use Magento \Quote \Model \Quote \Item \ToOrderItem as ToOrderItemConverter ;
22
37
use Magento \Quote \Model \Quote \Payment \ToOrderPayment as ToOrderPaymentConverter ;
38
+ use Magento \Quote \Model \ResourceModel \Quote \Item ;
39
+ use Magento \Sales \Api \Data \OrderInterface ;
23
40
use Magento \Sales \Api \Data \OrderInterfaceFactory as OrderFactory ;
24
41
use Magento \Sales \Api \OrderManagementInterface as OrderManagement ;
25
42
use Magento \Store \Model \StoreManagerInterface ;
31
48
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
32
49
* @SuppressWarnings(PHPMD.TooManyFields)
33
50
*/
34
- class QuoteManagement implements \ Magento \ Quote \ Api \ CartManagementInterface
51
+ class QuoteManagement implements CartManagementInterface
35
52
{
36
53
/**
37
54
* @var EventManager
@@ -84,27 +101,27 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
84
101
protected $ userContext ;
85
102
86
103
/**
87
- * @var \Magento\Quote\Api\ CartRepositoryInterface
104
+ * @var CartRepositoryInterface
88
105
*/
89
106
protected $ quoteRepository ;
90
107
91
108
/**
92
- * @var \Magento\Customer\Api\ CustomerRepositoryInterface
109
+ * @var CustomerRepositoryInterface
93
110
*/
94
111
protected $ customerRepository ;
95
112
96
113
/**
97
- * @var \Magento\Customer\Model\ CustomerFactory
114
+ * @var CustomerFactory
98
115
*/
99
116
protected $ customerModelFactory ;
100
117
101
118
/**
102
- * @var \Magento\Quote\Model\Quote\ AddressFactory
119
+ * @var AddressFactory
103
120
*/
104
121
protected $ quoteAddressFactory ;
105
122
106
123
/**
107
- * @var \Magento\Framework\Api\ DataObjectHelper
124
+ * @var DataObjectHelper
108
125
*/
109
126
protected $ dataObjectHelper ;
110
127
@@ -114,17 +131,17 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
114
131
protected $ storeManager ;
115
132
116
133
/**
117
- * @var \Magento\Checkout\Model\Session
134
+ * @var CheckoutSession
118
135
*/
119
136
protected $ checkoutSession ;
120
137
121
138
/**
122
- * @var \Magento\Customer\Model\Session
139
+ * @var CustomerSession
123
140
*/
124
141
protected $ customerSession ;
125
142
126
143
/**
127
- * @var \Magento\Customer\Api\ AccountManagementInterface
144
+ * @var AccountManagementInterface
128
145
*/
129
146
protected $ accountManagement ;
130
147
@@ -134,12 +151,12 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
134
151
protected $ quoteFactory ;
135
152
136
153
/**
137
- * @var \Magento\Quote\Model\ QuoteIdMaskFactory
154
+ * @var QuoteIdMaskFactory
138
155
*/
139
156
private $ quoteIdMaskFactory ;
140
157
141
158
/**
142
- * @var \Magento\Customer\Api\ AddressRepositoryInterface
159
+ * @var AddressRepositoryInterface
143
160
*/
144
161
private $ addressRepository ;
145
162
@@ -149,12 +166,12 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
149
166
private $ addressesToSync = [];
150
167
151
168
/**
152
- * @var \Magento\Framework\App\ RequestInterface
169
+ * @var RequestInterface
153
170
*/
154
171
private $ request ;
155
172
156
173
/**
157
- * @var \Magento\Framework\HTTP\PhpEnvironment\ RemoteAddress
174
+ * @var RemoteAddress
158
175
*/
159
176
private $ remoteAddress ;
160
177
@@ -169,20 +186,20 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
169
186
* @param ToOrderItemConverter $quoteItemToOrderItem
170
187
* @param ToOrderPaymentConverter $quotePaymentToOrderPayment
171
188
* @param UserContextInterface $userContext
172
- * @param \Magento\Quote\Api\ CartRepositoryInterface $quoteRepository
173
- * @param \Magento\Customer\Api\ CustomerRepositoryInterface $customerRepository
174
- * @param \Magento\Customer\Model\ CustomerFactory $customerModelFactory
175
- * @param \Magento\Quote\Model\Quote\ AddressFactory $quoteAddressFactory
176
- * @param \Magento\Framework\Api\ DataObjectHelper $dataObjectHelper
189
+ * @param CartRepositoryInterface $quoteRepository
190
+ * @param CustomerRepositoryInterface $customerRepository
191
+ * @param CustomerFactory $customerModelFactory
192
+ * @param AddressFactory $quoteAddressFactory
193
+ * @param DataObjectHelper $dataObjectHelper
177
194
* @param StoreManagerInterface $storeManager
178
- * @param \Magento\Checkout\Model\Session $checkoutSession
179
- * @param \Magento\Customer\Model\Session $customerSession
180
- * @param \Magento\Customer\Api\ AccountManagementInterface $accountManagement
195
+ * @param CheckoutSession $checkoutSession
196
+ * @param CustomerSession $customerSession
197
+ * @param AccountManagementInterface $accountManagement
181
198
* @param QuoteFactory $quoteFactory
182
- * @param \Magento\Quote\Model\ QuoteIdMaskFactory|null $quoteIdMaskFactory
183
- * @param \Magento\Customer\Api\ AddressRepositoryInterface|null $addressRepository
184
- * @param \Magento\Framework\App\ RequestInterface|null $request
185
- * @param \Magento\Framework\HTTP\PhpEnvironment\ RemoteAddress $remoteAddress
199
+ * @param QuoteIdMaskFactory|null $quoteIdMaskFactory
200
+ * @param AddressRepositoryInterface|null $addressRepository
201
+ * @param RequestInterface|null $request
202
+ * @param RemoteAddress $remoteAddress
186
203
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
187
204
*/
188
205
public function __construct (
@@ -196,20 +213,20 @@ public function __construct(
196
213
ToOrderItemConverter $ quoteItemToOrderItem ,
197
214
ToOrderPaymentConverter $ quotePaymentToOrderPayment ,
198
215
UserContextInterface $ userContext ,
199
- \ Magento \ Quote \ Api \ CartRepositoryInterface $ quoteRepository ,
200
- \ Magento \ Customer \ Api \ CustomerRepositoryInterface $ customerRepository ,
201
- \ Magento \ Customer \ Model \ CustomerFactory $ customerModelFactory ,
202
- \ Magento \ Quote \ Model \ Quote \ AddressFactory $ quoteAddressFactory ,
203
- \ Magento \ Framework \ Api \ DataObjectHelper $ dataObjectHelper ,
216
+ CartRepositoryInterface $ quoteRepository ,
217
+ CustomerRepositoryInterface $ customerRepository ,
218
+ CustomerFactory $ customerModelFactory ,
219
+ AddressFactory $ quoteAddressFactory ,
220
+ DataObjectHelper $ dataObjectHelper ,
204
221
StoreManagerInterface $ storeManager ,
205
- \ Magento \ Checkout \ Model \ Session $ checkoutSession ,
206
- \ Magento \ Customer \ Model \ Session $ customerSession ,
207
- \ Magento \ Customer \ Api \ AccountManagementInterface $ accountManagement ,
208
- \ Magento \ Quote \ Model \ QuoteFactory $ quoteFactory ,
209
- \ Magento \ Quote \ Model \ QuoteIdMaskFactory $ quoteIdMaskFactory = null ,
210
- \ Magento \ Customer \ Api \ AddressRepositoryInterface $ addressRepository = null ,
211
- \ Magento \ Framework \ App \ RequestInterface $ request = null ,
212
- \ Magento \ Framework \ HTTP \ PhpEnvironment \ RemoteAddress $ remoteAddress = null
222
+ CheckoutSession $ checkoutSession ,
223
+ CustomerSession $ customerSession ,
224
+ AccountManagementInterface $ accountManagement ,
225
+ QuoteFactory $ quoteFactory ,
226
+ QuoteIdMaskFactory $ quoteIdMaskFactory = null ,
227
+ AddressRepositoryInterface $ addressRepository = null ,
228
+ RequestInterface $ request = null ,
229
+ RemoteAddress $ remoteAddress = null
213
230
) {
214
231
$ this ->eventManager = $ eventManager ;
215
232
$ this ->submitQuoteValidator = $ submitQuoteValidator ;
@@ -232,13 +249,13 @@ public function __construct(
232
249
$ this ->customerSession = $ customerSession ;
233
250
$ this ->quoteFactory = $ quoteFactory ;
234
251
$ this ->quoteIdMaskFactory = $ quoteIdMaskFactory ?: ObjectManager::getInstance ()
235
- ->get (\ Magento \ Quote \ Model \ QuoteIdMaskFactory::class);
252
+ ->get (QuoteIdMaskFactory::class);
236
253
$ this ->addressRepository = $ addressRepository ?: ObjectManager::getInstance ()
237
- ->get (\ Magento \ Customer \ Api \ AddressRepositoryInterface::class);
254
+ ->get (AddressRepositoryInterface::class);
238
255
$ this ->request = $ request ?: ObjectManager::getInstance ()
239
- ->get (\ Magento \ Framework \ App \ RequestInterface::class);
256
+ ->get (RequestInterface::class);
240
257
$ this ->remoteAddress = $ remoteAddress ?: ObjectManager::getInstance ()
241
- ->get (\ Magento \ Framework \ HTTP \ PhpEnvironment \ RemoteAddress::class);
258
+ ->get (RemoteAddress::class);
242
259
}
243
260
244
261
/**
@@ -307,14 +324,14 @@ public function assignCustomer($cartId, $customerId, $storeId)
307
324
$ this ->quoteRepository ->save ($ customerActiveQuote );
308
325
309
326
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
310
- } catch (\ Magento \ Framework \ Exception \ NoSuchEntityException $ e ) {
327
+ } catch (NoSuchEntityException $ e ) {
311
328
}
312
329
313
330
$ quote ->setCustomer ($ customer );
314
331
$ quote ->setCustomerIsGuest (0 );
315
332
$ quote ->setIsActive (1 );
316
333
317
- /** @var \Magento\Quote\Model\ QuoteIdMask $quoteIdMask */
334
+ /** @var QuoteIdMask $quoteIdMask */
318
335
$ quoteIdMask = $ this ->quoteIdMaskFactory ->create ()->load ($ cartId , 'quote_id ' );
319
336
if ($ quoteIdMask ->getId ()) {
320
337
$ quoteIdMask ->delete ();
@@ -329,11 +346,11 @@ public function assignCustomer($cartId, $customerId, $storeId)
329
346
* Creates an anonymous cart.
330
347
*
331
348
* @param int $storeId
332
- * @return \Magento\Quote\Model\ Quote Cart object.
349
+ * @return Quote Cart object.
333
350
*/
334
351
protected function createAnonymousCart ($ storeId )
335
352
{
336
- /** @var \Magento\Quote\Model\ Quote $quote */
353
+ /** @var Quote $quote */
337
354
$ quote = $ this ->quoteFactory ->create ();
338
355
$ quote ->setStoreId ($ storeId );
339
356
return $ quote ;
@@ -344,16 +361,16 @@ protected function createAnonymousCart($storeId)
344
361
*
345
362
* @param int $customerId
346
363
* @param int $storeId
347
- * @return \Magento\Quote\Model\ Quote Cart object.
364
+ * @return Quote Cart object.
348
365
* @throws CouldNotSaveException The cart could not be created.
349
366
*/
350
367
protected function createCustomerCart ($ customerId , $ storeId )
351
368
{
352
369
try {
353
370
$ quote = $ this ->quoteRepository ->getActiveForCustomer ($ customerId );
354
- } catch (\ Magento \ Framework \ Exception \ NoSuchEntityException $ e ) {
371
+ } catch (NoSuchEntityException $ e ) {
355
372
$ customer = $ this ->customerRepository ->getById ($ customerId );
356
- /** @var \Magento\Quote\Model\ Quote $quote */
373
+ /** @var Quote $quote */
357
374
$ quote = $ this ->quoteFactory ->create ();
358
375
$ quote ->setStoreId ($ storeId );
359
376
$ quote ->setCustomer ($ customer );
@@ -364,18 +381,21 @@ protected function createCustomerCart($customerId, $storeId)
364
381
365
382
/**
366
383
* @inheritdoc
384
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
367
385
*/
368
386
public function placeOrder ($ cartId , PaymentInterface $ paymentMethod = null )
369
387
{
370
388
$ quote = $ this ->quoteRepository ->getActive ($ cartId );
389
+ $ customer = $ quote ->getCustomer ();
390
+
371
391
if ($ paymentMethod ) {
372
392
$ paymentMethod ->setChecks (
373
393
[
374
- \ Magento \ Payment \ Model \ Method \ AbstractMethod::CHECK_USE_CHECKOUT ,
375
- \ Magento \ Payment \ Model \ Method \ AbstractMethod::CHECK_USE_FOR_COUNTRY ,
376
- \ Magento \ Payment \ Model \ Method \ AbstractMethod::CHECK_USE_FOR_CURRENCY ,
377
- \ Magento \ Payment \ Model \ Method \ AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX ,
378
- \ Magento \ Payment \ Model \ Method \ AbstractMethod::CHECK_ZERO_TOTAL
394
+ AbstractMethod::CHECK_USE_CHECKOUT ,
395
+ AbstractMethod::CHECK_USE_FOR_COUNTRY ,
396
+ AbstractMethod::CHECK_USE_FOR_CURRENCY ,
397
+ AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX ,
398
+ AbstractMethod::CHECK_ZERO_TOTAL
379
399
]
380
400
);
381
401
$ quote ->getPayment ()->setQuote ($ quote );
@@ -386,18 +406,22 @@ public function placeOrder($cartId, PaymentInterface $paymentMethod = null)
386
406
$ quote ->collectTotals ();
387
407
}
388
408
389
- if ($ quote ->getCheckoutMethod () === self ::METHOD_GUEST ) {
409
+ if ($ quote ->getCheckoutMethod () === self ::METHOD_GUEST || ! $ customer ) {
390
410
$ quote ->setCustomerId (null );
391
- $ quote ->setCustomerEmail ($ quote ->getBillingAddress ()->getEmail ());
392
- if ($ quote ->getCustomerFirstname () === null && $ quote ->getCustomerLastname () === null ) {
393
- $ quote ->setCustomerFirstname ($ quote ->getBillingAddress ()->getFirstname ());
394
- $ quote ->setCustomerLastname ($ quote ->getBillingAddress ()->getLastname ());
395
- if ($ quote ->getBillingAddress ()->getMiddlename () === null ) {
396
- $ quote ->setCustomerMiddlename ($ quote ->getBillingAddress ()->getMiddlename ());
411
+ $ billingAddress = $ quote ->getBillingAddress ();
412
+ $ quote ->setCustomerEmail ($ billingAddress ? $ billingAddress ->getEmail () : null );
413
+ if ($ quote ->getCustomerFirstname () === null
414
+ && $ quote ->getCustomerLastname () === null
415
+ && $ billingAddress
416
+ ) {
417
+ $ quote ->setCustomerFirstname ($ billingAddress ->getFirstname ());
418
+ $ quote ->setCustomerLastname ($ billingAddress ->getLastname ());
419
+ if ($ billingAddress ->getMiddlename () === null ) {
420
+ $ quote ->setCustomerMiddlename ($ billingAddress ->getMiddlename ());
397
421
}
398
422
}
399
423
$ quote ->setCustomerIsGuest (true );
400
- $ groupId = $ quote -> getCustomer ()-> getGroupId () ? : GroupInterface::NOT_LOGGED_IN_ID ;
424
+ $ groupId = $ customer ? $ customer -> getGroupId () : GroupInterface::NOT_LOGGED_IN_ID ;
401
425
$ quote ->setCustomerGroupId ($ groupId );
402
426
}
403
427
@@ -442,9 +466,9 @@ public function getCartForCustomer($customerId)
442
466
*
443
467
* @param Quote $quote
444
468
* @param array $orderData
445
- * @return \Magento\Framework\Model\ AbstractExtensibleModel|\Magento\Sales\Api\Data\ OrderInterface|object|null
469
+ * @return AbstractExtensibleModel|OrderInterface|object|null
446
470
* @throws \Exception
447
- * @throws \Magento\Framework\Exception\ LocalizedException
471
+ * @throws LocalizedException
448
472
*/
449
473
public function submit (QuoteEntity $ quote , $ orderData = [])
450
474
{
@@ -473,7 +497,7 @@ protected function resolveItems(QuoteEntity $quote)
473
497
}
474
498
475
499
$ parentItemId = $ quoteItem ->getParentItemId ();
476
- /** @var \Magento\Quote\Model\ResourceModel\Quote\ Item $parentItem */
500
+ /** @var Item $parentItem */
477
501
if ($ parentItemId && !isset ($ orderItems [$ parentItemId ])) {
478
502
$ orderItems [$ parentItemId ] = $ this ->quoteItemToOrderItem ->convert (
479
503
$ quoteItem ->getParentItem (),
@@ -491,9 +515,9 @@ protected function resolveItems(QuoteEntity $quote)
491
515
*
492
516
* @param Quote $quote
493
517
* @param array $orderData
494
- * @return \Magento\Framework\Model\ AbstractExtensibleModel|\Magento\Sales\Api\Data\ OrderInterface|object
518
+ * @return AbstractExtensibleModel|OrderInterface|object
495
519
* @throws \Exception
496
- * @throws \Magento\Framework\Exception\ LocalizedException
520
+ * @throws LocalizedException
497
521
*/
498
522
protected function submitQuote (QuoteEntity $ quote , $ orderData = [])
499
523
{
@@ -510,13 +534,13 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
510
534
$ quote ->reserveOrderId ();
511
535
if ($ quote ->isVirtual ()) {
512
536
$ this ->dataObjectHelper ->mergeDataObjects (
513
- \ Magento \ Sales \ Api \ Data \ OrderInterface::class,
537
+ OrderInterface::class,
514
538
$ order ,
515
539
$ this ->quoteAddressToOrder ->convert ($ quote ->getBillingAddress (), $ orderData )
516
540
);
517
541
} else {
518
542
$ this ->dataObjectHelper ->mergeDataObjects (
519
- \ Magento \ Sales \ Api \ Data \ OrderInterface::class,
543
+ OrderInterface::class,
520
544
$ order ,
521
545
$ this ->quoteAddressToOrder ->convert ($ quote ->getShippingAddress (), $ orderData )
522
546
);
@@ -683,13 +707,13 @@ protected function _prepareCustomerQuote($quote)
683
707
* Remove related to order and quote addresses and submit exception to further processing.
684
708
*
685
709
* @param Quote $quote
686
- * @param \Magento\Sales\Api\Data\ OrderInterface $order
710
+ * @param OrderInterface $order
687
711
* @param \Exception $e
688
712
* @throws \Exception
689
713
*/
690
714
private function rollbackAddresses (
691
715
QuoteEntity $ quote ,
692
- \ Magento \ Sales \ Api \ Data \ OrderInterface $ order ,
716
+ OrderInterface $ order ,
693
717
\Exception $ e
694
718
): void {
695
719
try {
0 commit comments