-1

I want to remove the customer quote when the guest customer login into the checkout page then guest quote is assigned to the customer.

Please suggest me how can I delete the customer quote and assign the guest quote to the customer on login time

2 Answers 2

0

Instead of removing the quote on the checkout page you can remove the old quote item on checkout and assign the new guest item to the login customer. Please follow the below steps for this

{your_module_path}/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_login">
        <observer name="controller_action_postdispatch_customer_ajax_login" instance="Vendor\Module\Observer\CustomerOldItem" />
    </event>
</config>

{your_module_path}/Observer/CustomerOldItem.php

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Model\QuoteRepository;


class CustomerOldItem implements ObserverInterface
{
    protected $quoteRepository;




    public function __construct(
        QuoteRepository $quoteRepository
    ) {
        $this->quoteRepository = $quoteRepository;
    }




    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $customer = $observer->getEvent()->getCustomer();
        $quote = $this->quoteRepository->getForCustomer($customer->getId());
        $quote->removeAllItems()->save();
    }
}
0

Since this is a guess checkout, the only way I can think of is to let the customer re-load the quote in their browser. So you would need to either email them a link with the quote id or let them enter the quote id on your site.

To Reload Quote in your controller

 $id = $this->getRequest()->getParam('quote_id');
 $store_id = $this->getRequest()->getParam('store_id');

 ....   
 $this->_getSession()->clear();
 $this->_getQuote()
      ->setQuoteId($id)
      ->setStoreId($store_id)
      ->setIsActive(true)
      ->load($id);

Your Answer

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