0

I am facing an issue with cart merging

this is the scenario.

  1. I logged in to my account, i added one product to favorites then from favorites page i added that product to cart
  2. Next i logged out, added the same product from PDP or PLP as a guest.
  3. Then i logged in to the same account.

Now instead of showing the single product with quantity 2, it showing like two products in the cart with quantity 1 and 1.

I need to show like single product with updated qty. How can i achieve this.

4
  • What is the product type ? it's a simple product ? Jun 21, 2023 at 11:22
  • Yes @MohammadBharmal, Any suggestions?
    – Mani
    Jun 21, 2023 at 13:24
  • What is PDP or PLP ? Jun 21, 2023 at 17:09
  • From PLP @MohammadBharmal
    – Mani
    Jun 22, 2023 at 14:22

1 Answer 1

0

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 .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.