@@ -298,23 +298,28 @@ public function assignCustomer($cartId, $customerId, $storeId)
298
298
);
299
299
}
300
300
try {
301
- $ this ->quoteRepository ->getForCustomer ($ customerId );
302
- throw new StateException (
303
- __ ("The customer can't be assigned to the cart because the customer already has an active cart. " )
304
- );
305
- // phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
301
+ $ customerActiveQuote = $ this ->quoteRepository ->getForCustomer ($ customerId );
306
302
} catch (\Magento \Framework \Exception \NoSuchEntityException $ e ) {
303
+ /** This exception appear when customer have no active cart*/
304
+ $ customerActiveQuote = $ this ->quoteFactory ->create ();
305
+ $ customerActiveQuote ->setCustomer ($ customer );
306
+ $ customerActiveQuote ->setCustomerIsGuest (0 );
307
+ $ customerActiveQuote ->setStoreId ($ storeId );
308
+ $ customerActiveQuote ->setIsActive (true );
307
309
}
308
310
309
- $ quote ->setCustomer ($ customer );
310
- $ quote ->setCustomerIsGuest (0 );
311
- /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
312
- $ quoteIdMask = $ this ->quoteIdMaskFactory ->create ()->load ($ cartId , 'quote_id ' );
313
- if ($ quoteIdMask ->getId ()) {
314
- $ quoteIdMask ->delete ();
311
+ if ($ customerActiveQuote ->getIsActive ()) {
312
+ /** Merge carts */
313
+ $ customerActiveQuote ->merge ($ quote );
314
+ $ this ->quoteRepository ->delete ($ quote );
315
+ $ this ->quoteRepository ->save ($ customerActiveQuote );
316
+
317
+ return true ;
318
+ } else {
319
+ throw new \Magento \Framework \Exception \NoSuchEntityException (
320
+ __ ("The customer can't be assigned to the cart. No active cart for customer. " )
321
+ );
315
322
}
316
- $ this ->quoteRepository ->save ($ quote );
317
- return true ;
318
323
}
319
324
320
325
/**
0 commit comments