0

In my module, I did forcefully logged in to the customer without a password. And After logged in customers redirect to the Homepage only. And everything works as expected, except customer name not showing in Header user links.

Note:- I am working Magento 2.2.5 Enterprise version of Magento.

public function execute()
{
    $data = $this->getRequest()->getParams();
    $customer = null;
    $placeHolderCustomer = null;
    if (isset($data['winid']) && isset($data['email'])) {
        try {
            $placeHolderEmail = $data['winid'].self::DEFAULT_PLACEHOLDER_EMAIL;
            $placeHolderCustomer = $this->customerRepositoryInterface->get($placeHolderEmail);
        } catch (NoSuchEntityException $noSuchEntityException) {
            $urlKey = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
            $this->_redirect($urlKey);
            return false;
        }

        try {
            $customer = $this->customerRepositoryInterface->get($data['email']);
            $this->updateCustomerAddressInfo($placeHolderCustomer, $customer, $data['winid']);
        } catch (NoSuchEntityException $noSuchEntityException) {
            if (!is_null($placeHolderCustomer)) {
                try {
                    if (isset($data['firstname']) && isset($data['lastname'])) {
                        $customer = $this->saveCustomerAccountData(
                            $placeHolderCustomer,
                            $data['email'],
                            $data['firstname'],
                            $data['lastname']
                        );
                    } else {
                        $customer = $this->saveCustomerAccountData($placeHolderCustomer, $data['email']);
                    }
                    $this->saveCustomerAddressData($placeHolderCustomer, $customer);
                } catch (\Exception $e) {

                    $urlKey = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
                    $this->_redirect($urlKey);
                    return false;
                }
            }
        }

        $customerId = (!is_null($customer)) ? $customer->getId(): null;
        if (!is_null($customerId)) {
            $searchCriteria = $this->getCustomerAddressSearchCriteria($data['winid'], $customerId);
            $customerAddressesResult = $this->customerAddressRepositoryInterface->getList($searchCriteria);
            if ($customerAddressesResult->getTotalCount() > 0) {
                // Updating existing customer name, received from post data
                if (isset($data['firstname']) && isset($data['lastname']) && $placeHolderCustomer == null) {
                    $customer->setFirstname($data['firstname']);
                    $customer->setLastname($data['lastname']);
                    $this->customerRepositoryInterface->save($customer);
                }

                $items = $customerAddressesResult->getItems();
                $shippingAddress = (isset($items[count($items) - 1])) ? $items[count($items) - 1]: $items[0];
                $customerZipCode = $shippingAddress->getPostcode();

                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $customerData = $objectManager->create('Magento\Customer\Model\Customer')->load($customerId);

                $this->_session->loginById($customerId);
                $this->_session->setWinId($data['winid']);
                $this->_session->setZipCode($customerZipCode);
                $this->_session->setCustomerAsLoggedIn($customerData);
                try{
                 if ($this->cookieManager->getCookie('mage-cache-sessid')) {
                        $metadata = $this->cookieMetadataFactory->createCookieMetadata();
                        $metadata->setPath('/');
                        $this->cookieManager->deleteCookie('mage-cache-sessid', $metadata);
                    }
                    $customerFullMessage = 'Welcome, '.$data['firstname'].' '.$data['lastname']. ' ,You are successfully Logged In to the System';
                    $this->messageManager->addSuccessMessage(__($customerFullMessage));
                } catch (\Exception $exception) {
                    $this->messageManager->addErrorMessage(
                        __('An error occurred while processing your login. Please try again later.')
                    );
                }




                // if(isset($data['catId'])){
                    // $categoryObj = $this->categoryRepository->get($data['catId']);
                    // $urlKey =   $this->categoryHelper->getCategoryUrl($categoryObj);
                // } else if(isset($data['urlkey'])){
                    // $categories = $this->_categoryFactory->create()->getCollection()
                                // ->addAttributeToFilter('url_key',$data['urlkey'])
                                // ->addAttributeToSelect(['entity_id']);
                    // $categoryId = $categories->getFirstItem()->getEntityId();
                    // $categoryObj = $this->categoryRepository->get($categoryId);
                    // $urlKey =   $this->categoryHelper->getCategoryUrl($categoryObj);
                // } else if(isset($data['part_number'])){
                    // $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/singlesignon.log');
                    // $logger = new \Zend\Log\Logger();
                    // $logger->addWriter($writer);
                    // $logger->info('Your text message');
                    // $logger->info(print_r($data['part_number'], true));

                    // if($this->_product->getIdBySku($data['part_number'])) {
                        // // if(isSalable){

                        // // } else{
                        // // }
                        // $_product = $this->productRepository->get($data['part_number']);
                        // $urlKey = $_product->getProductUrl();
                        // $logger->info(print_r($urlKey, true));

                    // } else{
                        // $urlKey = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
                    // }

                // } else {
                    // $urlKey = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
                // }
                // if(empty($categoryObj)){
                    // $this->_redirect($urlKey);
                // }else{
                    // //$urlKey =   $this->categoryHelper->getCategoryUrl($categoryObj);
                    // $this->_redirect($urlKey);
                // }

                if(isset($data['part_number'])){
                    if($this->_product->getIdBySku($data['part_number'])) {
                        $_product = $this->productRepository->get($data['part_number']);
                        if(in_array($placeHolderCustomer->getWebsiteId() , $_product->getWebsiteIds())){
                            if($_product->isSalable() == 1){
                                if($_product->isAvailable() == 1){
                                    $urlKey = $_product->getProductUrl();
                                } else{
                                    $urlKey = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
                                }
                            } else {
                                $urlKey = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
                            }
                        } else {
                            $urlKey = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
                        }
                    } else{
                        $urlKey = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
                    }
                } else{
                    $urlKey = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
                }                       
                $this->_redirect($urlKey);



            } else {
                $urlKey = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
                $this->_redirect($urlKey);
                return false;
            }
        } else {
            $urlKey = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
            $this->_redirect($urlKey);
            return false;
        }
    } else {
        $urlKey = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
        $this->_redirect($urlKey);
        return false;
    }
}

Can anyone help me to find out the actual problem? enter image description here

4
  • please share code screenshot please Dec 17, 2019 at 4:50
  • Please check my edited comment Dec 17, 2019 at 10:09
  • so what other data you will get? email , customer id? Dec 18, 2019 at 4:43
  • Are you able to resolve this issue? @NilavaChakraborty Aug 4, 2020 at 3:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.