55 */
66namespace Magento \Persistent \Model ;
77
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+
817/**
9- * Class QuoteManager
18+ * Quote manager model
1019 *
1120 * @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1221 */
@@ -29,7 +38,7 @@ class QuoteManager
2938 /**
3039 * Persistent data
3140 *
32- * @var \Magento\Persistent\Helper\ Data
41+ * @var Data
3342 */
3443 protected $ persistentData ;
3544
@@ -41,26 +50,44 @@ class QuoteManager
4150 protected $ _setQuotePersistent = true ;
4251
4352 /**
44- * @var \Magento\Quote\Api\ CartRepositoryInterface
53+ * @var CartRepositoryInterface
4554 */
4655 protected $ quoteRepository ;
4756
57+ /**
58+ * @var ShippingAssignmentProcessor
59+ */
60+ private $ shippingAssignmentProcessor ;
61+
62+ /**
63+ * @var CartExtensionFactory
64+ */
65+ private $ cartExtensionFactory ;
66+
4867 /**
4968 * @param \Magento\Persistent\Helper\Session $persistentSession
50- * @param \Magento\Persistent\Helper\ Data $persistentData
69+ * @param Data $persistentData
5170 * @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
5374 */
5475 public function __construct (
5576 \Magento \Persistent \Helper \Session $ persistentSession ,
56- \ Magento \ Persistent \ Helper \ Data $ persistentData ,
77+ Data $ persistentData ,
5778 \Magento \Checkout \Model \Session $ checkoutSession ,
58- \Magento \Quote \Api \CartRepositoryInterface $ quoteRepository
79+ CartRepositoryInterface $ quoteRepository ,
80+ ?CartExtensionFactory $ cartExtensionFactory = null ,
81+ ?ShippingAssignmentProcessor $ shippingAssignmentProcessor = null
5982 ) {
6083 $ this ->persistentSession = $ persistentSession ;
6184 $ this ->persistentData = $ persistentData ;
6285 $ this ->checkoutSession = $ checkoutSession ;
6386 $ this ->quoteRepository = $ quoteRepository ;
87+ $ this ->cartExtensionFactory = $ cartExtensionFactory
88+ ?? ObjectManager::getInstance ()->get (CartExtensionFactory::class);
89+ $ this ->shippingAssignmentProcessor = $ shippingAssignmentProcessor
90+ ?? ObjectManager::getInstance ()->get (ShippingAssignmentProcessor::class);
6491 }
6592
6693 /**
@@ -71,7 +98,7 @@ public function __construct(
7198 */
7299 public function setGuest ($ checkQuote = false )
73100 {
74- /** @var $quote \Magento\Quote\Model\ Quote */
101+ /** @var $quote Quote */
75102 $ quote = $ this ->checkoutSession ->getQuote ();
76103 if ($ quote && $ quote ->getId ()) {
77104 if ($ checkQuote && !$ this ->persistentData ->isShoppingCartPersist () && !$ quote ->getIsPersistent ()) {
@@ -87,17 +114,19 @@ public function setGuest($checkQuote = false)
87114 ->setCustomerEmail (null )
88115 ->setCustomerFirstname (null )
89116 ->setCustomerLastname (null )
90- ->setCustomerGroupId (\ Magento \ Customer \ Api \ Data \ GroupInterface::NOT_LOGGED_IN_ID )
117+ ->setCustomerGroupId (GroupInterface::NOT_LOGGED_IN_ID )
91118 ->setIsPersistent (false )
92119 ->removeAllAddresses ();
93120 //Create guest addresses
94121 $ quote ->getShippingAddress ();
95122 $ quote ->getBillingAddress ();
123+ $ this ->setShippingAssignments ($ quote );
96124 $ quote ->collectTotals ();
97125 $ this ->quoteRepository ->save ($ quote );
98126 }
99127
100128 $ this ->persistentSession ->getSession ()->removePersistentCookie ();
129+ $ this ->persistentSession ->setSession (null );
101130 }
102131
103132 /**
@@ -111,7 +140,7 @@ public function setGuest($checkQuote = false)
111140 public function convertCustomerCartToGuest ()
112141 {
113142 $ quoteId = $ this ->checkoutSession ->getQuoteId ();
114- /** @var $quote \Magento\Quote\Model\ Quote */
143+ /** @var $quote Quote */
115144 $ quote = $ this ->quoteRepository ->get ($ quoteId );
116145 if ($ quote && $ quote ->getId ()) {
117146 $ this ->_setQuotePersistent = false ;
@@ -126,6 +155,7 @@ public function convertCustomerCartToGuest()
126155 $ quote ->getAddressesCollection ()->walk ('setEmail ' , ['email ' => null ]);
127156 $ quote ->collectTotals ();
128157 $ this ->persistentSession ->getSession ()->removePersistentCookie ();
158+ $ this ->persistentSession ->setSession (null );
129159 $ this ->quoteRepository ->save ($ quote );
130160 }
131161 }
@@ -144,7 +174,7 @@ public function expire()
144174 $ quote ->setIsActive (true )
145175 ->setIsPersistent (false )
146176 ->setCustomerId (null )
147- ->setCustomerGroupId (\ Magento \ Customer \ Api \ Data \ GroupInterface::NOT_LOGGED_IN_ID );
177+ ->setCustomerGroupId (GroupInterface::NOT_LOGGED_IN_ID );
148178 }
149179 }
150180
@@ -157,4 +187,23 @@ public function isPersistent()
157187 {
158188 return $ this ->_setQuotePersistent ;
159189 }
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+ }
160209}
0 commit comments