5
5
*/
6
6
namespace Magento \Persistent \Model ;
7
7
8
+ use Magento \Customer \Api \Data \GroupInterface ;
9
+ use Magento \Framework \App \ObjectManager ;
10
+ use Magento \Persistent \Helper \Data ;
11
+ use Magento \Quote \Api \CartRepositoryInterface ;
12
+ use Magento \Quote \Api \Data \CartExtensionFactory ;
13
+ use Magento \Quote \Api \Data \CartInterface ;
14
+ use Magento \Quote \Model \Quote ;
15
+ use Magento \Quote \Model \Quote \ShippingAssignment \ShippingAssignmentProcessor ;
16
+
8
17
/**
9
- * Class QuoteManager
18
+ * Quote manager model
10
19
*
11
20
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
12
21
*/
@@ -29,7 +38,7 @@ class QuoteManager
29
38
/**
30
39
* Persistent data
31
40
*
32
- * @var \Magento\Persistent\Helper\ Data
41
+ * @var Data
33
42
*/
34
43
protected $ persistentData ;
35
44
@@ -41,26 +50,44 @@ class QuoteManager
41
50
protected $ _setQuotePersistent = true ;
42
51
43
52
/**
44
- * @var \Magento\Quote\Api\ CartRepositoryInterface
53
+ * @var CartRepositoryInterface
45
54
*/
46
55
protected $ quoteRepository ;
47
56
57
+ /**
58
+ * @var ShippingAssignmentProcessor
59
+ */
60
+ private $ shippingAssignmentProcessor ;
61
+
62
+ /**
63
+ * @var CartExtensionFactory
64
+ */
65
+ private $ cartExtensionFactory ;
66
+
48
67
/**
49
68
* @param \Magento\Persistent\Helper\Session $persistentSession
50
- * @param \Magento\Persistent\Helper\ Data $persistentData
69
+ * @param Data $persistentData
51
70
* @param \Magento\Checkout\Model\Session $checkoutSession
52
- * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
71
+ * @param CartRepositoryInterface $quoteRepository
72
+ * @param CartExtensionFactory|null $cartExtensionFactory
73
+ * @param ShippingAssignmentProcessor|null $shippingAssignmentProcessor
53
74
*/
54
75
public function __construct (
55
76
\Magento \Persistent \Helper \Session $ persistentSession ,
56
- \ Magento \ Persistent \ Helper \ Data $ persistentData ,
77
+ Data $ persistentData ,
57
78
\Magento \Checkout \Model \Session $ checkoutSession ,
58
- \Magento \Quote \Api \CartRepositoryInterface $ quoteRepository
79
+ CartRepositoryInterface $ quoteRepository ,
80
+ ?CartExtensionFactory $ cartExtensionFactory = null ,
81
+ ?ShippingAssignmentProcessor $ shippingAssignmentProcessor = null
59
82
) {
60
83
$ this ->persistentSession = $ persistentSession ;
61
84
$ this ->persistentData = $ persistentData ;
62
85
$ this ->checkoutSession = $ checkoutSession ;
63
86
$ this ->quoteRepository = $ quoteRepository ;
87
+ $ this ->cartExtensionFactory = $ cartExtensionFactory
88
+ ?? ObjectManager::getInstance ()->get (CartExtensionFactory::class);
89
+ $ this ->shippingAssignmentProcessor = $ shippingAssignmentProcessor
90
+ ?? ObjectManager::getInstance ()->get (ShippingAssignmentProcessor::class);
64
91
}
65
92
66
93
/**
@@ -71,7 +98,7 @@ public function __construct(
71
98
*/
72
99
public function setGuest ($ checkQuote = false )
73
100
{
74
- /** @var $quote \Magento\Quote\Model\ Quote */
101
+ /** @var $quote Quote */
75
102
$ quote = $ this ->checkoutSession ->getQuote ();
76
103
if ($ quote && $ quote ->getId ()) {
77
104
if ($ checkQuote && !$ this ->persistentData ->isShoppingCartPersist () && !$ quote ->getIsPersistent ()) {
@@ -87,17 +114,19 @@ public function setGuest($checkQuote = false)
87
114
->setCustomerEmail (null )
88
115
->setCustomerFirstname (null )
89
116
->setCustomerLastname (null )
90
- ->setCustomerGroupId (\ Magento \ Customer \ Api \ Data \ GroupInterface::NOT_LOGGED_IN_ID )
117
+ ->setCustomerGroupId (GroupInterface::NOT_LOGGED_IN_ID )
91
118
->setIsPersistent (false )
92
119
->removeAllAddresses ();
93
120
//Create guest addresses
94
121
$ quote ->getShippingAddress ();
95
122
$ quote ->getBillingAddress ();
123
+ $ this ->setShippingAssignments ($ quote );
96
124
$ quote ->collectTotals ();
97
125
$ this ->quoteRepository ->save ($ quote );
98
126
}
99
127
100
128
$ this ->persistentSession ->getSession ()->removePersistentCookie ();
129
+ $ this ->persistentSession ->setSession (null );
101
130
}
102
131
103
132
/**
@@ -111,7 +140,7 @@ public function setGuest($checkQuote = false)
111
140
public function convertCustomerCartToGuest ()
112
141
{
113
142
$ quoteId = $ this ->checkoutSession ->getQuoteId ();
114
- /** @var $quote \Magento\Quote\Model\ Quote */
143
+ /** @var $quote Quote */
115
144
$ quote = $ this ->quoteRepository ->get ($ quoteId );
116
145
if ($ quote && $ quote ->getId ()) {
117
146
$ this ->_setQuotePersistent = false ;
@@ -126,6 +155,7 @@ public function convertCustomerCartToGuest()
126
155
$ quote ->getAddressesCollection ()->walk ('setEmail ' , ['email ' => null ]);
127
156
$ quote ->collectTotals ();
128
157
$ this ->persistentSession ->getSession ()->removePersistentCookie ();
158
+ $ this ->persistentSession ->setSession (null );
129
159
$ this ->quoteRepository ->save ($ quote );
130
160
}
131
161
}
@@ -144,7 +174,7 @@ public function expire()
144
174
$ quote ->setIsActive (true )
145
175
->setIsPersistent (false )
146
176
->setCustomerId (null )
147
- ->setCustomerGroupId (\ Magento \ Customer \ Api \ Data \ GroupInterface::NOT_LOGGED_IN_ID );
177
+ ->setCustomerGroupId (GroupInterface::NOT_LOGGED_IN_ID );
148
178
}
149
179
}
150
180
@@ -157,4 +187,23 @@ public function isPersistent()
157
187
{
158
188
return $ this ->_setQuotePersistent ;
159
189
}
190
+
191
+ /**
192
+ * Create shipping assignment for shopping cart
193
+ *
194
+ * @param CartInterface $quote
195
+ */
196
+ private function setShippingAssignments (CartInterface $ quote ): void
197
+ {
198
+ $ shippingAssignments = [];
199
+ if (!$ quote ->isVirtual () && $ quote ->getItemsQty () > 0 ) {
200
+ $ shippingAssignments [] = $ this ->shippingAssignmentProcessor ->create ($ quote );
201
+ }
202
+ $ cartExtension = $ quote ->getExtensionAttributes ();
203
+ if ($ cartExtension === null ) {
204
+ $ cartExtension = $ this ->cartExtensionFactory ->create ();
205
+ }
206
+ $ cartExtension ->setShippingAssignments ($ shippingAssignments );
207
+ $ quote ->setExtensionAttributes ($ cartExtension );
208
+ }
160
209
}
0 commit comments