How to set and get customer session data in magento 2 Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?How to call a model method from controller in Magento2getting error in while running Cutom admin controller URL in magento 2Magento 2.1: Invoke urlBuilder->getUrl() in a controller in a custom moduleMagento 2: How to override newsletter Subscriber modelMagento 2 Add new field to Magento_User admin formMagento offline custom Payment method with drop down listMagento 2 Create dynamic array From different Model Collection to use in multi select in gridMagento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 Can't view module's front end page output?
Fundamental Solution of the Pell Equation
8 Prisoners wearing hats
Around usage results
How to show element name in portuguese using elements package?
Trademark violation for app?
What would be the ideal power source for a cybernetic eye?
Did MS DOS itself ever use blinking text?
How do I stop a creek from eroding my steep embankment?
How to compare two different files line by line in unix?
Can an alien society believe that their star system is the universe?
Is it common practice to audition new musicians 1-2-1 before rehearsing with the entire band?
Find the length x such that the two distances in the triangle are the same
Should I use a zero-interest credit card for a large one-time purchase?
What is homebrew?
When a candle burns, why does the top of wick glow if bottom of flame is hottest?
How to down pick a chord with skipped strings?
Is safe to use va_start macro with this as parameter?
An adverb for when you're not exaggerating
First console to have temporary backward compatibility
What is the escape velocity of a neutron particle (not neutron star)
Why are both D and D# fitting into my E minor key?
Closed form of recurrent arithmetic series summation
Compare a given version number in the form major.minor.build.patch and see if one is less than the other
Maximum summed powersets with non-adjacent items
How to set and get customer session data in magento 2
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?How to call a model method from controller in Magento2getting error in while running Cutom admin controller URL in magento 2Magento 2.1: Invoke urlBuilder->getUrl() in a controller in a custom moduleMagento 2: How to override newsletter Subscriber modelMagento 2 Add new field to Magento_User admin formMagento offline custom Payment method with drop down listMagento 2 Create dynamic array From different Model Collection to use in multi select in gridMagento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 Can't view module's front end page output?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am struggling with magento 2 session. I have created below controller file as a sample code.
<?php
namespace vendor_namemodule_nameControllerSetGetSession;
use MagentoFrameworkAppActionAction;
class SetGetSession extends Action
protected $customerSession;
public function _construct(
MagentoCustomerModelSession $customerSession
)
$this->customerSession = $customerSession;
public function execute()
?>
Can anyone please help me with how to assign data and retrieve it from session variable?
Thank you.
magento2 customer-session
add a comment |
I am struggling with magento 2 session. I have created below controller file as a sample code.
<?php
namespace vendor_namemodule_nameControllerSetGetSession;
use MagentoFrameworkAppActionAction;
class SetGetSession extends Action
protected $customerSession;
public function _construct(
MagentoCustomerModelSession $customerSession
)
$this->customerSession = $customerSession;
public function execute()
?>
Can anyone please help me with how to assign data and retrieve it from session variable?
Thank you.
magento2 customer-session
add a comment |
I am struggling with magento 2 session. I have created below controller file as a sample code.
<?php
namespace vendor_namemodule_nameControllerSetGetSession;
use MagentoFrameworkAppActionAction;
class SetGetSession extends Action
protected $customerSession;
public function _construct(
MagentoCustomerModelSession $customerSession
)
$this->customerSession = $customerSession;
public function execute()
?>
Can anyone please help me with how to assign data and retrieve it from session variable?
Thank you.
magento2 customer-session
I am struggling with magento 2 session. I have created below controller file as a sample code.
<?php
namespace vendor_namemodule_nameControllerSetGetSession;
use MagentoFrameworkAppActionAction;
class SetGetSession extends Action
protected $customerSession;
public function _construct(
MagentoCustomerModelSession $customerSession
)
$this->customerSession = $customerSession;
public function execute()
?>
Can anyone please help me with how to assign data and retrieve it from session variable?
Thank you.
magento2 customer-session
magento2 customer-session
asked Jul 27 '16 at 6:08
Aniket ShindeAniket Shinde
73119
73119
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can Set and get Customer session by using MagentoCustomerModelSession
protected $customerSession;
public function __construct(
MagentoCustomerModelSession $customerSession
)
$this->customerSession = $customerSession;
$this->customerSession->setMyValue('test');
$this->customerSession->getMyValue();
Or by object manager.
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerSession = $objectManager->create('MagentoCustomerModelSession');
$customerSession->setMyValue('test');
$customerSession->getMyValue();
Setting an information to the customer session:
$om = MagentoFrameworkAppObjectManager::getInstance(); $session =
$om->get('MagentoCustomerModelSession');
$session->setTestKey('test value');Getting an information from the customer session:
$om = MagentoFrameworkAppObjectManager::getInstance(); $session =
$om->get('MagentoCustomerModelSession');
echo $session->getTestKey();
Session will extends core class MagentoFrameworkSessionSessionManager to handle the session.
Hope this answer will help you.
I am getting error as "Call to a member function setMyValue() on null" with provided set and get session code.
– Aniket Shinde
Jul 27 '16 at 6:21
Please check modified answer added by object manager.
– Krishna ijjada
Jul 27 '16 at 6:27
Thanks for the help. It works with object manager, but it looks like it is increasing page load time. I tried it before posting the question.
– Aniket Shinde
Jul 27 '16 at 6:34
add a comment |
You need to inject MagentoCustomerModelSession class for set and get data in customer session
Using Dependency Injection
protected $customerSession;
public function _construct(
...
MagentoCustomerModelSession $customerSession
...
)
...
$this->customerSession = $customerSession;
...
public function setValue()
return $this->customerSession->setMyValue('YourValue'); //set value in customer session
public function getValue()
return $this->customerSession->getMyValue(); //Get value from customer session
Using Object Manager
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerSession = $objectManager->get('MagentoCustomerModelSession');
$customerSession->setMyValue('YourValue'); //set value in customer session
echo $customerSession->getMyValue(); //Get value from customer session
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f127614%2fhow-to-set-and-get-customer-session-data-in-magento-2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can Set and get Customer session by using MagentoCustomerModelSession
protected $customerSession;
public function __construct(
MagentoCustomerModelSession $customerSession
)
$this->customerSession = $customerSession;
$this->customerSession->setMyValue('test');
$this->customerSession->getMyValue();
Or by object manager.
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerSession = $objectManager->create('MagentoCustomerModelSession');
$customerSession->setMyValue('test');
$customerSession->getMyValue();
Setting an information to the customer session:
$om = MagentoFrameworkAppObjectManager::getInstance(); $session =
$om->get('MagentoCustomerModelSession');
$session->setTestKey('test value');Getting an information from the customer session:
$om = MagentoFrameworkAppObjectManager::getInstance(); $session =
$om->get('MagentoCustomerModelSession');
echo $session->getTestKey();
Session will extends core class MagentoFrameworkSessionSessionManager to handle the session.
Hope this answer will help you.
I am getting error as "Call to a member function setMyValue() on null" with provided set and get session code.
– Aniket Shinde
Jul 27 '16 at 6:21
Please check modified answer added by object manager.
– Krishna ijjada
Jul 27 '16 at 6:27
Thanks for the help. It works with object manager, but it looks like it is increasing page load time. I tried it before posting the question.
– Aniket Shinde
Jul 27 '16 at 6:34
add a comment |
You can Set and get Customer session by using MagentoCustomerModelSession
protected $customerSession;
public function __construct(
MagentoCustomerModelSession $customerSession
)
$this->customerSession = $customerSession;
$this->customerSession->setMyValue('test');
$this->customerSession->getMyValue();
Or by object manager.
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerSession = $objectManager->create('MagentoCustomerModelSession');
$customerSession->setMyValue('test');
$customerSession->getMyValue();
Setting an information to the customer session:
$om = MagentoFrameworkAppObjectManager::getInstance(); $session =
$om->get('MagentoCustomerModelSession');
$session->setTestKey('test value');Getting an information from the customer session:
$om = MagentoFrameworkAppObjectManager::getInstance(); $session =
$om->get('MagentoCustomerModelSession');
echo $session->getTestKey();
Session will extends core class MagentoFrameworkSessionSessionManager to handle the session.
Hope this answer will help you.
I am getting error as "Call to a member function setMyValue() on null" with provided set and get session code.
– Aniket Shinde
Jul 27 '16 at 6:21
Please check modified answer added by object manager.
– Krishna ijjada
Jul 27 '16 at 6:27
Thanks for the help. It works with object manager, but it looks like it is increasing page load time. I tried it before posting the question.
– Aniket Shinde
Jul 27 '16 at 6:34
add a comment |
You can Set and get Customer session by using MagentoCustomerModelSession
protected $customerSession;
public function __construct(
MagentoCustomerModelSession $customerSession
)
$this->customerSession = $customerSession;
$this->customerSession->setMyValue('test');
$this->customerSession->getMyValue();
Or by object manager.
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerSession = $objectManager->create('MagentoCustomerModelSession');
$customerSession->setMyValue('test');
$customerSession->getMyValue();
Setting an information to the customer session:
$om = MagentoFrameworkAppObjectManager::getInstance(); $session =
$om->get('MagentoCustomerModelSession');
$session->setTestKey('test value');Getting an information from the customer session:
$om = MagentoFrameworkAppObjectManager::getInstance(); $session =
$om->get('MagentoCustomerModelSession');
echo $session->getTestKey();
Session will extends core class MagentoFrameworkSessionSessionManager to handle the session.
Hope this answer will help you.
You can Set and get Customer session by using MagentoCustomerModelSession
protected $customerSession;
public function __construct(
MagentoCustomerModelSession $customerSession
)
$this->customerSession = $customerSession;
$this->customerSession->setMyValue('test');
$this->customerSession->getMyValue();
Or by object manager.
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerSession = $objectManager->create('MagentoCustomerModelSession');
$customerSession->setMyValue('test');
$customerSession->getMyValue();
Setting an information to the customer session:
$om = MagentoFrameworkAppObjectManager::getInstance(); $session =
$om->get('MagentoCustomerModelSession');
$session->setTestKey('test value');Getting an information from the customer session:
$om = MagentoFrameworkAppObjectManager::getInstance(); $session =
$om->get('MagentoCustomerModelSession');
echo $session->getTestKey();
Session will extends core class MagentoFrameworkSessionSessionManager to handle the session.
Hope this answer will help you.
edited Feb 13 at 7:59
Giel Berkers
7,17924382
7,17924382
answered Jul 27 '16 at 6:14
Krishna ijjadaKrishna ijjada
5,92542356
5,92542356
I am getting error as "Call to a member function setMyValue() on null" with provided set and get session code.
– Aniket Shinde
Jul 27 '16 at 6:21
Please check modified answer added by object manager.
– Krishna ijjada
Jul 27 '16 at 6:27
Thanks for the help. It works with object manager, but it looks like it is increasing page load time. I tried it before posting the question.
– Aniket Shinde
Jul 27 '16 at 6:34
add a comment |
I am getting error as "Call to a member function setMyValue() on null" with provided set and get session code.
– Aniket Shinde
Jul 27 '16 at 6:21
Please check modified answer added by object manager.
– Krishna ijjada
Jul 27 '16 at 6:27
Thanks for the help. It works with object manager, but it looks like it is increasing page load time. I tried it before posting the question.
– Aniket Shinde
Jul 27 '16 at 6:34
I am getting error as "Call to a member function setMyValue() on null" with provided set and get session code.
– Aniket Shinde
Jul 27 '16 at 6:21
I am getting error as "Call to a member function setMyValue() on null" with provided set and get session code.
– Aniket Shinde
Jul 27 '16 at 6:21
Please check modified answer added by object manager.
– Krishna ijjada
Jul 27 '16 at 6:27
Please check modified answer added by object manager.
– Krishna ijjada
Jul 27 '16 at 6:27
Thanks for the help. It works with object manager, but it looks like it is increasing page load time. I tried it before posting the question.
– Aniket Shinde
Jul 27 '16 at 6:34
Thanks for the help. It works with object manager, but it looks like it is increasing page load time. I tried it before posting the question.
– Aniket Shinde
Jul 27 '16 at 6:34
add a comment |
You need to inject MagentoCustomerModelSession class for set and get data in customer session
Using Dependency Injection
protected $customerSession;
public function _construct(
...
MagentoCustomerModelSession $customerSession
...
)
...
$this->customerSession = $customerSession;
...
public function setValue()
return $this->customerSession->setMyValue('YourValue'); //set value in customer session
public function getValue()
return $this->customerSession->getMyValue(); //Get value from customer session
Using Object Manager
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerSession = $objectManager->get('MagentoCustomerModelSession');
$customerSession->setMyValue('YourValue'); //set value in customer session
echo $customerSession->getMyValue(); //Get value from customer session
add a comment |
You need to inject MagentoCustomerModelSession class for set and get data in customer session
Using Dependency Injection
protected $customerSession;
public function _construct(
...
MagentoCustomerModelSession $customerSession
...
)
...
$this->customerSession = $customerSession;
...
public function setValue()
return $this->customerSession->setMyValue('YourValue'); //set value in customer session
public function getValue()
return $this->customerSession->getMyValue(); //Get value from customer session
Using Object Manager
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerSession = $objectManager->get('MagentoCustomerModelSession');
$customerSession->setMyValue('YourValue'); //set value in customer session
echo $customerSession->getMyValue(); //Get value from customer session
add a comment |
You need to inject MagentoCustomerModelSession class for set and get data in customer session
Using Dependency Injection
protected $customerSession;
public function _construct(
...
MagentoCustomerModelSession $customerSession
...
)
...
$this->customerSession = $customerSession;
...
public function setValue()
return $this->customerSession->setMyValue('YourValue'); //set value in customer session
public function getValue()
return $this->customerSession->getMyValue(); //Get value from customer session
Using Object Manager
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerSession = $objectManager->get('MagentoCustomerModelSession');
$customerSession->setMyValue('YourValue'); //set value in customer session
echo $customerSession->getMyValue(); //Get value from customer session
You need to inject MagentoCustomerModelSession class for set and get data in customer session
Using Dependency Injection
protected $customerSession;
public function _construct(
...
MagentoCustomerModelSession $customerSession
...
)
...
$this->customerSession = $customerSession;
...
public function setValue()
return $this->customerSession->setMyValue('YourValue'); //set value in customer session
public function getValue()
return $this->customerSession->getMyValue(); //Get value from customer session
Using Object Manager
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerSession = $objectManager->get('MagentoCustomerModelSession');
$customerSession->setMyValue('YourValue'); //set value in customer session
echo $customerSession->getMyValue(); //Get value from customer session
edited 2 days ago
Mark Shust
544716
544716
answered Apr 25 '17 at 12:38
Prince PatelPrince Patel
14.6k65481
14.6k65481
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f127614%2fhow-to-set-and-get-customer-session-data-in-magento-2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown