magento merge the guest and customer quote from the class
vendor/magento/module-quote/Model/Quote.php
find the merge function and which should be on line number 2401 and replace the function
public function merge(Quote $quote)
{
$this->_eventManager->dispatch(
$this->_eventPrefix . '_merge_before',
[$this->_eventObject => $this, 'source' => $quote]
);
foreach ($quote->getAllVisibleItems() as $item) {
$found = false;
foreach ($this->getAllItems() as $quoteItem) {
if ($quoteItem->compare($item)) {
$quoteItem->setQty($quoteItem->getQty() + $item->getQty());
$this->itemProcessor->merge($item, $quoteItem);
$found = true;
break;
}
}
if (!$found) {
$newItem = clone $item;
$this->addItem($newItem);
if ($item->getHasChildren()) {
foreach ($item->getChildren() as $child) {
$newChild = clone $child;
$newChild->setParentItem($newItem);
$this->addItem($newChild);
}
}
}
}
/**
* Init shipping and billing address if quote is new
*/
if (!$this->getId()) {
$this->getShippingAddress();
$this->getBillingAddress();
}
if ($quote->getCouponCode()) {
$this->setCouponCode($quote->getCouponCode());
}
$this->_eventManager->dispatch(
$this->_eventPrefix . '_merge_after',
[$this->_eventObject => $this, 'source' => $quote]
);
return $this;
}
with the below customized function
public function merge(Quote $quote)
{
$this->_eventManager->dispatch(
$this->_eventPrefix . '_merge_before',
[$this->_eventObject => $this, 'source' => $quote]
);
foreach ($quote->getAllVisibleItems() as $item) {
$found = false;
foreach ($this->getAllItems() as $quoteItem) {
/* resolving quote merge bug issue @ritesh24feb2021
* this will merge the products if product id is same
* please note : this will not check for custom options now
*/
$sameProductId = false;
if ($quoteItem->getProductId() == $item->getProductId()) {
$sameProductId = true;
}
if ($quoteItem->compare($item) || $sameProductId) {
$quoteItem->setQty($quoteItem->getQty() + $item->getQty());
$this->itemProcessor->merge($item, $quoteItem);
$found = true;
break;
}
}
if (!$found) {
$newItem = clone $item;
$this->addItem($newItem);
if ($item->getHasChildren()) {
foreach ($item->getChildren() as $child) {
$newChild = clone $child;
$newChild->setParentItem($newItem);
$this->addItem($newChild);
}
}
}
}
/**
* Init shipping and billing address if quote is new
*/
if (!$this->getId()) {
$this->getShippingAddress();
$this->getBillingAddress();
}
if ($quote->getCouponCode()) {
$this->setCouponCode($quote->getCouponCode());
}
$this->_eventManager->dispatch(
$this->_eventPrefix . '_merge_after',
[$this->_eventObject => $this, 'source' => $quote]
);
return $this;
}
Please Note : i am not advising to change the code of vendor directory directly . if it works for you , you can overwrite this class in your own module , i would be happy to assist further if it solves your issue .