persistenceSessionHelper = $persistenceSessionHelper; $this->customerSession = $customerSession; $this->quoteManager = $quoteManager; $this->persistenceDataHelper = $persistenceDataHelper; $this->cartRepository = $cartRepository; } /** * Set persistent session to guest if cart has been emptied and customer not logged in * * @param \Magento\Framework\Event\Observer $observer * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute(\Magento\Framework\Event\Observer $observer) { if (!$this->persistenceSessionHelper->isPersistent() || $this->customerSession->isLoggedIn() || !$this->persistenceDataHelper->isShoppingCartPersist() ) { return; } try { $custId = $this->persistenceSessionHelper->getSession()->getCustomerId(); /** @var \Magento\Quote\Api\Data\CartInterface $cart */ $cart = $this->cartRepository->getActiveForCustomer($custId); } catch (NoSuchEntityException $entityException) { $cart = null; } if (!$cart || $cart->getItemsCount() == 0) { $this->quoteManager->setGuest(); } } }