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

Ronak Rathod
  • 6,322
  • 17
  • 42
gh darvishani
  • 855
  • 1
  • 9
  • 37

2 Answers2

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();
Narayan Jat
  • 455
  • 2
  • 7
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!
Sarvesh Patel
  • 955
  • 9
  • 27
  • 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 06 '20 at 06:22
  • ok, you need to make a clear explanation. please share me more clear. thanks – Sarvesh Patel Jan 06 '20 at 06: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 Patel Jan 06 '20 at 06:40
  • i think the best way is overriding the session class...thanks for your response – gh darvishani Jan 06 '20 at 06:49
  • yes, thanks a lot. so please if it is a useful flag down and accept so this helps someone too. thanks, mate. – Sarvesh Patel Jan 06 '20 at 06:53
  • @ghdarvishani thanks mate. – Sarvesh Patel Jan 06 '20 at 06:54
  • me too @sarvesh Dineshkumar Patel – gh darvishani Jan 06 '20 at 06:55