cartItemFactory = $cartItemFactory; $this->_customerRepositoryInterface = $customerRepositoryInterface; $this->request = $request; $this->objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $this->customerSession = $this->objectManager->get(\Magento\Customer\Model\Session::class); parent::__construct($context); } private function getCartItems($customerId) { $collection = $this->cartItemFactory->create()->getCollection(); $data = $collection->addFieldToFilter('customer_id', ['eq' => $customerId]) ->setOrder('name','ASC') ->getData(); return $data; } private function clearCartItem($customerId) { $collection = $this->cartItemFactory->create()->getCollection(); $count = $collection->addFieldToSelect('id') ->addFieldToFilter('customer_id', ['eq' => $customerId]) ->walk('delete'); } public function execute() { if($this->customerSession->isLoggedIn()) { $customer = $this->customerSession->getCustomer(); $this->clearCartItem($customer->getId()); echo json_encode($this->getCartItems($customer->getId())); exit; } else { echo json_encode(array()); } } }