customerSession = $customerSession; $this->layout = $layout; $this->customerFactory = $customerFactory; $this->customerRepository = $customerRepository; $this->request = $request; $this->moduleManager = $moduleManager; $this->view = $view; } /** * Returns customer Data with customer group only * * @return \Magento\Customer\Api\Data\CustomerInterface */ protected function getDepersonalizedCustomer() { $customer = $this->customerFactory->create(); $customer->setGroupId($this->customerSession->getCustomerGroupId()); return $customer; } /** * Returns customer Data from service * * @return \Magento\Customer\Api\Data\CustomerInterface */ protected function getCustomerFromService() { return $this->customerRepository->getById($this->customerSession->getId()); } /** * Returns current customer according to session and context * * @return \Magento\Customer\Api\Data\CustomerInterface */ public function getCustomer() { if ($this->moduleManager->isEnabled('Magento_PageCache') && !$this->request->isAjax() && $this->view->isLayoutLoaded() && $this->layout->isCacheable() ) { return $this->getDepersonalizedCustomer(); } else { return $this->getCustomerFromService(); } } /** * Returns customer id from session * * @return int|null */ public function getCustomerId() { return $this->customerSession->getId(); } /** * Set customer id * * @param int|null $customerId * @return void */ public function setCustomerId($customerId) { $this->customerSession->setId($customerId); } }