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 );
@@ -371,11 +388,11 @@ public function placeOrder($cartId, PaymentInterface $paymentMethod = null)
371
388
if ($ paymentMethod ) {
372
389
$ paymentMethod ->setChecks (
373
390
[
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
391
+ AbstractMethod::CHECK_USE_CHECKOUT ,
392
+ AbstractMethod::CHECK_USE_FOR_COUNTRY ,
393
+ AbstractMethod::CHECK_USE_FOR_CURRENCY ,
394
+ AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX ,
395
+ AbstractMethod::CHECK_ZERO_TOTAL
379
396
]
380
397
);
381
398
$ quote ->getPayment ()->setQuote ($ quote );
@@ -386,7 +403,7 @@ public function placeOrder($cartId, PaymentInterface $paymentMethod = null)
386
403
$ quote ->collectTotals ();
387
404
}
388
405
389
- if ($ quote ->getCheckoutMethod () === self ::METHOD_GUEST ) {
406
+ if ($ quote ->getCheckoutMethod () === self ::METHOD_GUEST || ! $ quote -> getCustomer () ) {
390
407
$ quote ->setCustomerId (null );
391
408
$ quote ->setCustomerEmail ($ quote ->getBillingAddress ()->getEmail ());
392
409
if ($ quote ->getCustomerFirstname () === null && $ quote ->getCustomerLastname () === null ) {
@@ -442,9 +459,9 @@ public function getCartForCustomer($customerId)
442
459
*
443
460
* @param Quote $quote
444
461
* @param array $orderData
445
- * @return \Magento\Framework\Model\ AbstractExtensibleModel|\Magento\Sales\Api\Data\ OrderInterface|object|null
462
+ * @return AbstractExtensibleModel|OrderInterface|object|null
446
463
* @throws \Exception
447
- * @throws \Magento\Framework\Exception\ LocalizedException
464
+ * @throws LocalizedException
448
465
*/
449
466
public function submit (QuoteEntity $ quote , $ orderData = [])
450
467
{
@@ -473,7 +490,7 @@ protected function resolveItems(QuoteEntity $quote)
473
490
}
474
491
475
492
$ parentItemId = $ quoteItem ->getParentItemId ();
476
- /** @var \Magento\Quote\Model\ResourceModel\Quote\ Item $parentItem */
493
+ /** @var Item $parentItem */
477
494
if ($ parentItemId && !isset ($ orderItems [$ parentItemId ])) {
478
495
$ orderItems [$ parentItemId ] = $ this ->quoteItemToOrderItem ->convert (
479
496
$ quoteItem ->getParentItem (),
@@ -491,9 +508,9 @@ protected function resolveItems(QuoteEntity $quote)
491
508
*
492
509
* @param Quote $quote
493
510
* @param array $orderData
494
- * @return \Magento\Framework\Model\ AbstractExtensibleModel|\Magento\Sales\Api\Data\ OrderInterface|object
511
+ * @return AbstractExtensibleModel|OrderInterface|object
495
512
* @throws \Exception
496
- * @throws \Magento\Framework\Exception\ LocalizedException
513
+ * @throws LocalizedException
497
514
*/
498
515
protected function submitQuote (QuoteEntity $ quote , $ orderData = [])
499
516
{
@@ -510,13 +527,13 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
510
527
$ quote ->reserveOrderId ();
511
528
if ($ quote ->isVirtual ()) {
512
529
$ this ->dataObjectHelper ->mergeDataObjects (
513
- \ Magento \ Sales \ Api \ Data \ OrderInterface::class,
530
+ OrderInterface::class,
514
531
$ order ,
515
532
$ this ->quoteAddressToOrder ->convert ($ quote ->getBillingAddress (), $ orderData )
516
533
);
517
534
} else {
518
535
$ this ->dataObjectHelper ->mergeDataObjects (
519
- \ Magento \ Sales \ Api \ Data \ OrderInterface::class,
536
+ OrderInterface::class,
520
537
$ order ,
521
538
$ this ->quoteAddressToOrder ->convert ($ quote ->getShippingAddress (), $ orderData )
522
539
);
@@ -683,13 +700,13 @@ protected function _prepareCustomerQuote($quote)
683
700
* Remove related to order and quote addresses and submit exception to further processing.
684
701
*
685
702
* @param Quote $quote
686
- * @param \Magento\Sales\Api\Data\ OrderInterface $order
703
+ * @param OrderInterface $order
687
704
* @param \Exception $e
688
705
* @throws \Exception
689
706
*/
690
707
private function rollbackAddresses (
691
708
QuoteEntity $ quote ,
692
- \ Magento \ Sales \ Api \ Data \ OrderInterface $ order ,
709
+ OrderInterface $ order ,
693
710
\Exception $ e
694
711
): void {
695
712
try {
0 commit comments