1

Please let me know how can I change my current quote to any other quote saved in database using an event observer in magento 2.2

  • You mean you want to copy your old quote object into new quote object ? – Keyur Shah Oct 10 '17 at 6:15
  • That's correct. – DEEP JOSHI Oct 10 '17 at 7:13
2

Give a try

   $quote = $this->quoteRepository->get(**<Your_quote_id>**);
   $quote->setIsActive(1);
   $quoteRepository->save($quote);
   $this->checkoutSession->replaceQuote($quote);

where variables

$this->quoteRepository instance of Magento\Quote\Api\CartRepositoryInterface

$this->checkoutSession instance of Magento\Checkout\Model\Session
0

The following controller resets the quote Magento2, setting of 'other quote id' should be similar - just replace the 'null' to appropriate quoteId:

class Start extends \Magento\Framework\App\Action\Action
{
    /** @var \Magento\Checkout\Model\Session */
    private $session;

    /** @var \Magento\Quote\Model\QuoteRepository */
    private $quoteRepository;

    /**
     * Constructor
     *
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Checkout\Model\Session $session
     * @param \Magento\Quote\Model\QuoteRepository $quoteRepository
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Checkout\Model\Session $session,
        \Magento\Quote\Model\QuoteRepository $quoteRepository
    ) {
        parent::__construct($context);

        $this->session = $session;
        $this->quoteRepository = $quoteRepository;
    }

    /**
     * Controller execute action
     *
     * @return \Magento\Framework\App\ResponseInterface
     */
    public function execute()
    {

        $quote = $this->session->getQuote();
        $this->session->setQuoteId(null);
        $quote->setIsActive(false);
        $this->quoteRepository->save($quote);

        return $this->_redirect($this->_redirect->getRefererUrl());
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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