0

after customer login, I want to change some of the session params like customer name. I do like this

$this->_customerSession->getCustomer()->setName("ddddddd");

also i tried this

$this->_customerSession->setName("ddddddd");

but it does not work.so how I can change the customer basic data, also how Magento handles session data? I think after every request the Magento set session data from database...hmmm am I going wrong? is there anyone to talk about this?

I want to change the customer name and this change should be staying until the customer is login ...lets to review my problem ... I have a customer that can have sub customer (or may should I say, members) when a sub customer want to login with his/her username and password I check them and if they were correct I get parent customer and set it login but I want to replace parent name with sub customer

1

You can Set and get Customer session by using Magento\Customer\Model\Session

protected $customerSession;

public function __construct(   
    \Magento\Customer\Model\Session $customerSession
){
    $this->customerSession = $customerSession;
}

$this->customerSession->setMyValue('test');
$this->customerSession->getMyValue();

Or by object manager.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
$customerSession->setMyValue('test');
$customerSession->getMyValue();
| improve this answer | |
1

Please try to below code. But make sure you need to logged in.

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();  

$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$websiteId = $storeManager->getStore()->getWebsiteId();

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerRepository = $objectManager->create('Magento\Customer\Api\CustomerRepositoryInterface');

$customerSession = $objectManager->create('Magento\Customer\Model\Session');
if ($customerSession->isLoggedIn()) {
    echo 'Customer Id: ' . $customerSession->getCustomer()->getId() . '<br/>';
    echo 'Customer Name: ' . $customerSession->getCustomer()->getName() . '<br/>';
    echo 'Customer Email: ' . $customerSession->getCustomer()->getEmail() . '<br/>';

    $id = $customerSession->getCustomer()->getId() ;

    $customer = $customerRepository->getById($id);
    $firstName ='Sarvesh 1';
    $lastName ='Patel 1';

    $customer->setFirstname($firstName);
    $customer->setLastname($lastName);
    $customerRepository->save($customer);
}

But Make sure after run this code logout and try again login. Hope it works!
| improve this answer | |
  • thank but I think i could not descript my problem ... I want to change the customer name and this change should be staying until the customer is login ...lets to review my problem ... I have a customer that can have sub customer (or may should I say, members) when a sub customer want to login with his/her username and password i check them and if they were correct i get parent customer and set it login but i want to replace parent name with sub customer – gh darvishani Jan 6 at 6:22
  • ok, you need to make a clear explanation. please share me more clear. thanks – sarvesh Dineshkumar Patel Jan 6 at 6:26
  • ok, you need to make the first original name as save another session variable and then once you logout then set that name to your original one. because that logic you need to implement. Here I just give code to update the name. now you need to manage it on a session. If you can not I can explain you how. okay. – sarvesh Dineshkumar Patel Jan 6 at 6:40
  • i think the best way is overriding the session class...thanks for your response – gh darvishani Jan 6 at 6:49
  • yes, thanks a lot. so please if it is a useful flag down and accept so this helps someone too. thanks, mate. – sarvesh Dineshkumar Patel Jan 6 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.