Add a quote to cart and block adding new products to cart – Magento 2

When we submit a quote all products loaded in the quote will be added to cart , and adding a new product again to cart is not applicable ,so when ever we add a quote all the products loaded in the cart must be removed and replaced with that quote.

Here is code to remove items from cart before adding a quote:

public function __construct(
\Magento\Checkout\Model\Session $session
\JJ\Quote\Model\QuoteFactory     $quoteFactory, // loading from custom table
)
{
        parent::__construct($context);
$this->_session = $session;
$this->_quoteFactory = $quoteFactory;
}

//$quotes are loaded with _quoteFactory 
$quotes = $this->_quoteFactory->create()->getCollection();// add filter if needed
$Cartitems = $this->_session->getQuote()->getAllVisibleItems();
            if (count($quotes) > 0) {
                foreach ($Cartitems as $cartItem) {
                $itemId = $cartItem->getItemId();
                $this->cart->removeItem($itemId)->save();
                }
            }

in the add to cart controller passing a quote id as’ Request quote id’ and check whether that exists with cart table

if(count($cartitems)>0) {
                if ($this->checkoutSession->getQuote()->getRequestQuoteId() > 0) {
// Your Statements
}

Leave a comment

Your email address will not be published. Required fields are marked *