Skip to content

Commit 2a6dc66

Browse files
committed
Fix for merging guest cart items and customer cart items
1 parent ea71192 commit 2a6dc66

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

app/code/Magento/Quote/Model/QuoteManagement.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -298,23 +298,28 @@ public function assignCustomer($cartId, $customerId, $storeId)
298298
);
299299
}
300300
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);
306302
} 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);
307309
}
308310

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+
);
315322
}
316-
$this->quoteRepository->save($quote);
317-
return true;
318323
}
319324

320325
/**

0 commit comments

Comments
 (0)