In my website i have bounded the user to only add one product into the cart , however from the session quote object it loads multiple products. How can i restrict quote object to show only last added the product in the checkout. I have tried out my solution in the session.php by overriding it.
Can any one suggest what change i need to make more in the below code to restrict the quote object with lastly added item.
<?php
namespace Project\Cart\Model;
class Session extends \Magento\Checkout\Model\Session{
const CHECKOUT_STATE_BEGIN = 'begin';
/**
* Quote instance
*
* @var Quote
*/
protected $_quote;
/**
* Customer Data Object
*
* @var CustomerInterface|null
*/
protected $_customer;
/**
* Whether load only active quote
*
* @var bool
*/
protected $_loadInactive = false;
/**
* Loaded order instance
*
* @var \Magento\Sales\Model\Order
*/
protected $_order;
/**
* @var \Magento\Sales\Model\OrderFactory
*/
protected $_orderFactory;
/**
* @var \Magento\Customer\Model\Session
*/
protected $_customerSession;
/**
* @var \Magento\Quote\Api\CartRepositoryInterface
*/
protected $quoteRepository;
/**
* @var \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress
*/
protected $_remoteAddress;
/**
* @var \Magento\Framework\Event\ManagerInterface
*/
protected $_eventManager;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
/**
* @var \Magento\Customer\Api\CustomerRepositoryInterface
*/
protected $customerRepository;
/**
* @param QuoteIdMaskFactory
*/
protected $quoteIdMaskFactory;
/**
* @param bool
*/
protected $isQuoteMasked;
/**
* @var \Magento\Quote\Model\QuoteFactory
*/
protected $quoteFactory;
public function loadCustomerQuote()
{
if (!$this->_customerSession->getCustomerId()) {
return $this;
}
$this->_eventManager->dispatch('load_customer_quote_before', ['checkout_session' => $this]);
try {
$customerQuote = $this->quoteRepository->getForCustomer($this->_customerSession->getCustomerId());
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
$customerQuote = $this->quoteFactory->create();
}
$customerQuote->setStoreId($this->_storeManager->getStore()->getId());
$allItems = $customerQuote->getAllVisibleItems();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
//$quoteId = get quote id
$quoteItemObj = $objectManager->create('\Magento\Quote\Model\Quote\Item');
$i=0;
$cartObj = $objectManager->create('\Magento\Checkout\Model\Cart');
if(isset($allItems) && count($allItems) > 0) {
foreach ($allItems as $item)
{
$i++;
$quoteItem = $quoteItemObj->load($item->getItemId());
if(count($allItems) != $i) {
$quoteItem->delete();//delete item
}
}
$cartObj->save();
}
if ($customerQuote->getId() && $this->getQuoteId() != $customerQuote->getId()) {
if($this->getQuoteId() != "") {
$quote = $this->quoteRepository->get($this->getQuoteId());
if ($this->_quote) {
$this->quoteRepository->delete($this->getQuote());
}
//$this->quoteRepository->delete($this->getQuoteId());
//$items = $quote->getAllItems();
$this->setQuoteId($customerQuote->getId());
//$quote = $this->quoteRepository->get($quote->getId());
//$quote->setCustomerId($this->_customerSession->getCustomerId());
$this->quoteRepository->save($customerQuote);
// if ($this->_quote) {
// $this->quoteRepository->delete($this->_quote);
// }
$this->_quote = $customerQuote;
}
} else {
$this->getQuote()->getBillingAddress();
$this->getQuote()->getShippingAddress();
$this->getQuote()->setCustomer($this->_customerSession->getCustomerDataObject())
->setTotalsCollectedFlag(false)
->collectTotals();
$this->quoteRepository->save($this->getQuote());
}
return $this;
}
}